【Qt】自定义标题栏并实现鼠标拖拽移动

本文介绍了如何在Qt环境下自定义标题栏并实现窗口的鼠标拖拽移动。首先,通过设置属性隐藏默认标题栏,然后创建自定义组件作为标题栏。利用frameRect().contains判断鼠标位置,并通过move()方法移动窗口。最后,确保事件可以继续传递以处理其他控件交互。
摘要由CSDN通过智能技术生成

1.Qt在windows下变成,标题栏归系统管理器管理。想要自定义就只能把原来的隐藏掉,然后自己添加组件,自己做;

2.首先设置属性,隐藏掉原来的标题栏: 

    /* 标题栏样式 */
    this->setWindowFlags(Qt::FramelessWindowHint |
                         Qt::WindowSystemMenuHint |
                         Qt::WindowMinMaxButtonsHint);
3.重写鼠标的三个事件,分别是:

    /* custom title bar */
    void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
4.代码如下:

void MainWindow::mousePressEvent(QMouseEvent *event)
{

    if( event->button() == Qt::LeftButton &&
            ui->frame_title_bar->frameRect().contains(event->globalPos() - this->frameGeometry().topLeft())){
        m_Press &
  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用 Qt 的 QWidget 类来实现自定义标题栏,并且让鼠标可以拖动窗口。 以下是一个简单的示例代码: ```cpp #include <QtWidgets> class CustomTitleBar : public QWidget { public: CustomTitleBar(QWidget *parent = nullptr) : QWidget(parent) { // 设置标题栏大小和背景色 setFixedHeight(30); setStyleSheet("background-color: gray;"); // 添加标题栏上的标题 titleLabel = new QLabel("Custom Title Bar", this); titleLabel->setAlignment(Qt::AlignCenter); titleLabel->setStyleSheet("color: white;"); // 添加关闭按钮 closeButton = new QPushButton("X", this); closeButton->setFixedSize(20, 20); closeButton->setStyleSheet("background-color: red; color: white;"); // 将标题和关闭按钮布局到标题栏 QHBoxLayout *layout = new QHBoxLayout(this); layout->addWidget(titleLabel); layout->addStretch(); layout->addWidget(closeButton); layout->setContentsMargins(5, 0, 5, 0); } // 重写鼠标按下事件,实现窗口拖动 void mousePressEvent(QMouseEvent *event) override { if (event->button() == Qt::LeftButton) { isDragging = true; lastMousePos = event->globalPos(); event->accept(); } } // 重写鼠标移动事件,实现窗口拖动 void mouseMoveEvent(QMouseEvent *event) override { if (isDragging) { QPoint delta = event->globalPos() - lastMousePos; lastMousePos = event->globalPos(); parentWidget()->move(parentWidget()->pos() + delta); event->accept(); } } // 重写鼠标释放事件,实现窗口拖动 void mouseReleaseEvent(QMouseEvent *event) override { if (event->button() == Qt::LeftButton) { isDragging = false; event->accept(); } } private: QLabel *titleLabel; QPushButton *closeButton; bool isDragging = false; QPoint lastMousePos; }; int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建窗口并设置标题栏自定义标题栏 QWidget window; window.setWindowFlags(Qt::FramelessWindowHint); CustomTitleBar *titleBar = new CustomTitleBar(&window); QVBoxLayout *layout = new QVBoxLayout(&window); layout->addWidget(titleBar); layout->addWidget(new QLabel("Hello World!", &window)); window.setLayout(layout); window.resize(300, 200); window.show(); return app.exec(); } ``` 在上述代码中,首先创建了一个名为 `CustomTitleBar` 的自定义窗口部件,它包括一个标题和一个关闭按钮。然后,在 `CustomTitleBar` 类中重写了鼠标按下、移动和释放事件,实现窗口拖动。 在 `main` 函数中,创建了一个窗口并设置标题栏自定义标题栏,然后将自定义标题栏和一个标签添加到窗口中。最后,设置窗口大小并显示窗口。 需要注意的是,为了实现窗口拖动,需要将窗口的边框隐藏,可以使用 `setWindowFlags(Qt::FramelessWindowHint)` 来实现
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值