Qt自定义标题栏(qt自定义标题栏 开源)

看看资讯 / 100+人浏览
注意:免费节点订阅链接已更新至 2026-11-9,点击查看详情

建立标题栏类MainHeader

标题栏头文件:

class MainHeader : public QWidget { public:     MainHeader(QWidget *parent = nullptr);     void setUserName(const QString& userName);     QLabel *erpWinIcon; // 系统图标     QLabel *erpWinTitle; // 系统名称      QLabel *user; // 登录用户     QToolButton *erpWinLogout; // 退出登录     QToolButton *erpWinSizeMax; // 最大化窗口     QToolButton *erpWinSizeMin; // 最小化窗口     QToolButton *erpWinExit; // 退出系统    };

实现文件

MainHeader::MainHeader(QWidget *parent) : QWidget(parent) {     erpWinIcon = new QLabel;     QPixmap icon(":/images/images/erplogo.jpg");     QPixmap fixIcon = icon.scaled(16, 16, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);     erpWinIcon->setPixmap(fixIcon);     erpWinTitle = new QLabel("ErpSystem");      erpWinLogout = new QToolButton;     erpWinLogout->setIcon(QIcon(":/icons/icons/exit.png"));     erpWinLogout->setStyleSheet("background: transparent;");     erpWinSizeMin = new QToolButton;     erpWinSizeMin->setIcon(QIcon(":/icons/icons/winmin.png"));     erpWinSizeMin->setStyleSheet("background: transparent;");     erpWinSizeMax = new QToolButton;     erpWinSizeMax->setIcon(QIcon(":/icons/icons/winmax.png"));     erpWinSizeMax->setStyleSheet("background: transparent;");     erpWinExit = new QToolButton;     erpWinExit->setIcon(QIcon(":/icons/icons/close.png"));     erpWinExit->setStyleSheet("background: transparent;");      user = new QLabel(tr("admin"));      QHBoxLayout *headerLayout = new QHBoxLayout(this);     headerLayout->addWidget(erpWinIcon);     headerLayout->addWidget(erpWinTitle);     headerLayout->addStretch();     headerLayout->addWidget(user);     headerLayout->addWidget(erpWinLogout);     headerLayout->addWidget(erpWinSizeMin);     headerLayout->addWidget(erpWinSizeMax);     headerLayout->addWidget(erpWinExit);      this->setStyleSheet("background: #1c1b2d; border-bottom: 1px solid #1c1b2d;"); }  void MainHeader::setUserName(const QString& userName) {     user->setText(userName); }

使用标题栏时需要定义3按钮事件,我这里有4个,多了一个退出登录事件。还需要mousePressEvent和mouseMoveEvent实现窗口移动

比如MyWidget类是我的主窗口,则在头文件添加槽:

public slots:     void mousePressEvent(QMouseEvent *event) override;     void mouseMoveEvent(QMouseEvent *event) override;     void winExit();     void winSizeMax();     void winSizeMin();     void winLogout();

实现文件添加实现体

void ErpSys::winExit() {     this->close(); }  void ErpSys::winSizeMax() {     if (this->isMaximized()) {         this->showNormal();     } else {         this->showMaximized();     } }  void ErpSys::winSizeMin() {     this->showMinimized(); }  void ErpSys::winLogout() {  }  void ErpSys::mousePressEvent(QMouseEvent* event) {     // 获取到鼠标点击的位置     erpPos = event->pos(); } void ErpSys::mouseMoveEvent(QMouseEvent* event) {     if (event->buttons() & Qt::LeftButton) {         // 获取到移动的位置与初始点击位置的距离         QPoint diff = event->pos() - erpPos;         // 窗口当前位置 + 移动的位置与初始点位置的距离就是新的位置         QPoint newpos = this->pos() + diff;         this->move(newpos);     } } 

然后在初始化窗口时调用自定义标题栏

MainHeader *header = new MainHeader(this);          connect(header->erpWinSizeMin, SIGNAL(clicked()), this, SLOT(winSizeMin()) );     connect(header->erpWinSizeMax, SIGNAL(clicked()), this, SLOT(winSizeMax()));     connect(header->erpWinExit, SIGNAL(clicked()), this, SLOT(winExit()));

版权声明:

作者: FreeClashNode

链接: https://www.freeclashnode.com/news/article-2950.htm

来源: www.freeclashnode.com

文章版权归作者所有,未经允许请勿转载。

特别推荐

飞鸟加速
飞鸟加速

高速稳定的网络加速

畅享全球内容,访问 ChatGPT、TikTok、Google 等热门网站。 全平台支持 · 7×24 专业客服 · 采用军工级安全加密传输技术。

免费节点实时更新

最新文章