C++ 我想在qt中创建一个自定义标题栏

C++ 我想在qt中创建一个自定义标题栏,c++,qt,qwidget,C++,Qt,Qwidget,我想在qt中创建一个自定义标题栏 所以我查阅了一些例子,并遵循它们。 下面是应用该示例的代码 小部件头文件: #include <QWidget> #include <QMouseEvent> class KcWdTitlebar :public QWidget { private: QWidget *m_parent; QPoint m_pCursor; public: KcWdTitlebar( QWidget *parent) ; pr

我想在qt中创建一个自定义标题栏

所以我查阅了一些例子,并遵循它们。 下面是应用该示例的代码

小部件头文件:

#include <QWidget>
#include <QMouseEvent>

class KcWdTitlebar :public QWidget
{
private:
    QWidget *m_parent;
    QPoint m_pCursor;

public:
    KcWdTitlebar( QWidget *parent) ;

protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
};
KcWdTitlebar::KcWdTitlebar(QWidget *parent ) :m_parent(parent)
{
    QLabel *title = new QLabel(parent->windowTitle());
    QPushButton *pPB = new QPushButton ("x");

    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->addWidget(title);
    layout->addWidget(pPB);

    connect(pPB,SIGNAL(clicked()),parent,SLOT(close()));
}

void KcWdTitlebar::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        m_pCursor = event->globalPos() - geometry().topLeft();
        event->accept();
    }
}

void KcWdTitlebar::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        m_parent->move(event->globalPos() - m_pCursor);
        event->accept();
    }
}
#include <QMainWindow>
#include "KcWdTitlebar.h"

namespace Ui {
class mainwindow;
}

class mainwindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit mainwindow(QWidget *parent = 0);
    ~mainwindow();

private:
    KcWdTitlebar *m_title;
    Ui::mainwindow *ui;
};
mainwindow::mainwindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::mainwindow)
{
    ui->setupUi(this);
    m_title = new KcWdTitlebar(this);
    ui->verticalLayout->addWidget(m_title);

}
主窗口标题:

#include <QWidget>
#include <QMouseEvent>

class KcWdTitlebar :public QWidget
{
private:
    QWidget *m_parent;
    QPoint m_pCursor;

public:
    KcWdTitlebar( QWidget *parent) ;

protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
};
KcWdTitlebar::KcWdTitlebar(QWidget *parent ) :m_parent(parent)
{
    QLabel *title = new QLabel(parent->windowTitle());
    QPushButton *pPB = new QPushButton ("x");

    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->addWidget(title);
    layout->addWidget(pPB);

    connect(pPB,SIGNAL(clicked()),parent,SLOT(close()));
}

void KcWdTitlebar::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        m_pCursor = event->globalPos() - geometry().topLeft();
        event->accept();
    }
}

void KcWdTitlebar::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        m_parent->move(event->globalPos() - m_pCursor);
        event->accept();
    }
}
#include <QMainWindow>
#include "KcWdTitlebar.h"

namespace Ui {
class mainwindow;
}

class mainwindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit mainwindow(QWidget *parent = 0);
    ~mainwindow();

private:
    KcWdTitlebar *m_title;
    Ui::mainwindow *ui;
};
mainwindow::mainwindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::mainwindow)
{
    ui->setupUi(this);
    m_title = new KcWdTitlebar(this);
    ui->verticalLayout->addWidget(m_title);

}
当我运行这个代码时, 单击并拖动KcWdTitle部分将导致主窗口跟随我单击的点

我应该修复代码的哪些部分


我希望每个人都能理解我的英语。

您需要更改
mousePressEvent()
以减去
主窗口的几何图形,而不是标题栏的几何图形

更改:

m_pCursor = event->globalPos() - geometry().topLeft();
为此:

m_pCursor = event->globalPos() - m_parent->geometry().topLeft();

尝试更改:
void KcWdTitlebar::MousePresseEvent(QMouseEvent*事件)
void KcWdTitlebar::MouseEvent(QMouseEvent*事件)
并查看行为中是否有更改。如果是这样的话,那么这就是你应该改变的部分。Ui::mainwindow的完整定义在哪里?@DavidGrayson Ui::mainwindow中没有太多内容。我刚刚在框架顶部添加了KcWdTitlebar。