-
Unlimited personalization: The appearance, layout and controls of the title bar can be designed freely to achieve a distinctive style. -
Cross platform unification: under different operating systems, the title bar interface remains consistent, improving the integrity of the application. -
Strong function scalability: new functions such as user-defined buttons and search boxes can be easily added to improve the user experience.
-
Title text: used to display the application name or the current window title -
Window control buttons: minimize, maximize/restore, close buttons -
Drag area: used to drag the entire window -
Other function buttons: such as menu button, search box, etc
QWidget *window = new QWidget; window->setWindowFlags(Qt::FramelessWindowHint); // Set Borderless
#titlebar { background-color: #323232; } #titlebar QLabel { color: white; font-weight: bold; }
//Title Text auto titleLabel = new QLabel(this); titleLabel->setText("My App"); //Minimize button auto minimizeBtn = new QPushButton(this); minimizeBtn->setIcon(style->standardIcon(QStyle::SP_TitleBarMinButton)); connect(minimizeBtn, &QPushButton::clicked, this, &CustomTitleBar::showMinimized); //More Buttons
//Record the initial position in mousePressEvent m_startPos = event->globalPos(); //Adjust the window position according to the displacement in mouseMoveEvent move(geometry().topLeft() + event->globalPos() - m_startPos); //Reset status in mouseReleaseEvent
void CustomTitleBar::mouseDoubleClickEvent(QMouseEvent *event) { if(window()->isMaximized()) window()->showNormal(); else window()->showMaximized(); }
#ifndef TITLEBAR_H #define TITLEBAR_H #include <QWidget> #include <QHBoxLayout> #include <QLabel> #include <QPushButton> class TitleBar : public QWidget { Q_OBJECT public: explicit TitleBar(QWidget *parent = nullptr); void setWindowTitle(const QString &title); void setWindowIcon(const QIcon &icon); protected: void mousePressEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; void mouseDoubleClickEvent(QMouseEvent *event) override; private slots: void onButtonClicked(); private: QLabel *m_titleLabel; QPushButton *m_minimizeButton; QPushButton *m_maximizeButton; QPushButton *m_closeButton; QPoint m_startPos; bool m_isMousePressed; }; #endif // TITLEBAR_H
#include "titlebar.h" #include <QHBoxLayout> #include <QMouseEvent> #include <QApplication> #include <QStyle> TitleBar::TitleBar(QWidget *parent) : QWidget(parent) , m_isMousePressed(false) { //Title Text m_titleLabel = new QLabel(this); m_titleLabel->setStyleSheet("color: white; font-weight: bold; "); //Minimize button m_minimizeButton = new QPushButton(this); m_minimizeButton->setIcon(style()->standardIcon(QStyle::SP_TitleBarMinButton)); connect(m_minimizeButton, &QPushButton::clicked, this, &TitleBar::onButtonClicked); //Maximize button m_maximizeButton = new QPushButton(this); m_maximizeButton->setIcon(style()->standardIcon(QStyle::SP_TitleBarMaxButton)); connect(m_maximizeButton, &QPushButton::clicked, this, &TitleBar::onButtonClicked); //Close button m_closeButton = new QPushButton(this); m_closeButton->setIcon(style()->standardIcon(QStyle::SP_TitleBarCloseButton)); connect(m_closeButton, &QPushButton::clicked, this, &TitleBar::onButtonClicked); //Layout QHBoxLayout *layout = new QHBoxLayout(this); layout->addWidget(m_titleLabel); layout->addWidget(new QWidget(), 1); // Used to occupy the middle space layout->addWidget(m_minimizeButton); layout->addWidget(m_maximizeButton); layout->addWidget(m_closeButton); layout->setContentsMargins(5, 5, 5, 5); //Style Title Block setStyleSheet("background-color: #323232;"); } void TitleBar::setWindowTitle(const QString &title) { m_titleLabel->setText(title); } void TitleBar::setWindowIcon(const QIcon &icon) { m_titleLabel->setPixmap(icon.pixmap(16, 16)); } void TitleBar::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_isMousePressed = true; m_startPos = event->globalPos(); } } void TitleBar::mouseMoveEvent(QMouseEvent *event) { if (m_isMousePressed) { QPoint delta = event->globalPos() - m_startPos; window()->move(window()->pos() + delta); } } void TitleBar::mouseReleaseEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_isMousePressed = false; } } void TitleBar::mouseDoubleClickEvent(QMouseEvent *event) { Q_UNUSED(event) if (window()->isMaximized()) { window()->showNormal(); } else { window()->showMaximized(); } } void TitleBar::onButtonClicked() { QPushButton *button = qobject_cast<QPushButton *>(sender()); QWidget *window = this->window(); if (button == m_minimizeButton) { window->showMinimized(); } else if (button == m_maximizeButton) { if (window->isMaximized()) { window->showNormal(); } else { window->showMaximized(); } } else if (button == m_closeButton) { window->close(); } }
#include "titlebar.h" #include <QVBoxLayout> int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget window; window.setWindowFlags(Qt::FramelessWindowHint); // Set Borderless TitleBar *titleBar = new TitleBar(&window); titleBar->setWindowTitle("My Application"); titleBar->setWindowIcon(QIcon(":/icons/app.ico")); QWidget *centralWidget = new QWidget; //Add your main interface component QVBoxLayout *layout = new QVBoxLayout(&window); layout->addWidget(titleBar); layout->addWidget(centralWidget); window.show(); return app.exec(); }
epilogue