How can I handle events in the titlebar and change its color etc ?

The titlebar belongs to the OS and we don’t have control over that one. You can create your own titlebar, but note that this requires some work. In order to create your own titlebar then make a QWidget [doc.qt.nokia.com] subclass that contains three toolbuttons that handle the close, minimize and maximize events in addition to the moving of the window.

Then make a QFrame [doc.qt.nokia.com] subclass which does not have a titlebar provided via the window system. This is done by setting the Qt::FramelessWindowHint [doc.qt.nokia.com] window flag, however this will make it impossible to resize or move the window via the window system. What can be done is you can add your custom titlbar as a private member to the frame and add the it first to the frame’s vertical layout. The frame also needs a content widget which allows widgets to be added to it. Finally the QFrame subclass needs to reimplement the mouse events to handle the resizing and moving of the window.

The example below demonstrates how this can be achieved.

  1. #include <QtGui>
  2.  
  3. class TitleBar : public QWidget
  4. {
  5.     Q_OBJECT
  6. public:
  7.     TitleBar(QWidget *parent)
  8.     {
  9.         // Don't let this widget inherit the parent's backround color
  10.         setAutoFillBackground(true);
  11.         // Use a brush with a Highlight color role to render the background
  12.         setBackgroundRole(QPalette::Highlight);
  13.        
  14.         minimize = new QToolButton(this);
  15.         maximize = new QToolButton(this);
  16.         close= new QToolButton(this);
  17.        
  18.         // Use the style to set the button pixmaps
  19.         QPixmap pix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
  20.         close->setIcon(pix);
  21.        
  22.         maxPix = style()->standardPixmap(QStyle::SP_TitleBarMaxButton);
  23.         maximize->setIcon(maxPix);
  24.        
  25.         pix = style()->standardPixmap(QStyle::SP_TitleBarMinButton);
  26.         minimize->setIcon(pix);
  27.  
  28.         restorePix = style()->standardPixmap(QStyle::SP_TitleBarNormalButton);
  29.        
  30.         minimize->setMinimumHeight(20);
  31.         close->setMinimumHeight(20);
  32.         maximize->setMinimumHeight(20);
  33.        
  34.        
  35.         QLabel *label = new QLabel(this);
  36.         label->setText("Window Title");
  37.         parent->setWindowTitle("Window Title");
  38.        
  39.         QHBoxLayout *hbox = new QHBoxLayout(this);
  40.        
  41.         hbox->addWidget(label);
  42.         hbox->addWidget(minimize);
  43.         hbox->addWidget(maximize);
  44.         hbox->addWidget(close);
  45.        
  46.         hbox->insertStretch(1, 500);
  47.         hbox->setSpacing(0);
  48.         setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  49.        
  50.         maxNormal = false;
  51.        
  52.         connect(close, SIGNAL( clicked() ), parent, SLOT(close() ) );
  53.         connect(minimize, SIGNAL( clicked() ), this, SLOT(showSmall() ) );
  54.         connect(maximize, SIGNAL( clicked() ), this, SLOT(showMaxRestore() ) );
  55.     }
  56.    
  57. public slots:
  58.     void showSmall()
  59.     {
  60.         parentWidget()->showMinimized();
  61.     }
  62.    
  63.     void showMaxRestore()
  64.     {
  65.         if (maxNormal) {
  66.             parentWidget()->showNormal();
  67.             maxNormal = !maxNormal;
  68.             maximize->setIcon(maxPix);
  69.         } else {
  70.             parentWidget()->showMaximized();
  71.             maxNormal = !maxNormal;
  72.             maximize->setIcon(restorePix);
  73.         }
  74.     }
  75. protected:
  76.     void mousePressEvent(QMouseEvent *me)
  77.     {
  78.         startPos = me->globalPos();
  79.         clickPos = mapToParent(me->pos());
  80.     }
  81.     void mouseMoveEvent(QMouseEvent *me)
  82.     {
  83.         if (maxNormal)
  84.             return;
  85.         parentWidget()->move(me->globalPos() - clickPos);
  86.     }
  87.  
  88. private:
  89.     QToolButton *minimize;
  90.     QToolButton *maximize;
  91.     QToolButton *close;
  92.     QPixmap restorePix, maxPix;
  93.     bool maxNormal;
  94.     QPoint startPos;
  95.     QPoint clickPos;
  96. };
  97.  
  98. class Frame : public QFrame
  99. {
  100. public:
  101.    
  102.     Frame()
  103.     {
  104.         m_mouse_down = false;
  105.         setFrameShape(Panel);
  106.        
  107.         // Make this a borderless window which can't
  108.         // be resized or moved via the window system
  109.         setWindowFlags(Qt::FramelessWindowHint);
  110.         setMouseTracking(true);
  111.        
  112.         m_titleBar = new TitleBar(this);
  113.        
  114.         m_content = new QWidget(this);
  115.        
  116.         QVBoxLayout *vbox = new QVBoxLayout(this);
  117.         vbox->addWidget(m_titleBar);
  118.         vbox->setMargin(0);
  119.         vbox->setSpacing(0);
  120.        
  121.         QVBoxLayout *layout = new QVBoxLayout(this);
  122.         layout->addWidget(m_content);
  123.         layout->setMargin(5);
  124.         layout->setSpacing(0);
  125.         vbox->addLayout(layout);
  126.     }
  127.    
  128.     // Allows you to access the content area of the frame
  129.     // where widgets and layouts can be added
  130.     QWidget *contentWidget() const { return m_content; }
  131.    
  132.     TitleBar *titleBar() const { return m_titleBar; }
  133.    
  134.     void mousePressEvent(QMouseEvent *e)
  135.     {
  136.         m_old_pos = e->pos();
  137.         m_mouse_down = e->button() == Qt::LeftButton;
  138.     }
  139.    
  140.     void mouseMoveEvent(QMouseEvent *e)
  141.     {
  142.         int x = e->x();
  143.         int y = e->y();
  144.        
  145.         if (m_mouse_down) {
  146.             int dx = x - m_old_pos.x();
  147.             int dy = y - m_old_pos.y();
  148.            
  149.             QRect g = geometry();
  150.            
  151.             if (left)
  152.                 g.setLeft(g.left() + dx);
  153.             if (right)
  154.                 g.setRight(g.right() + dx);
  155.             if (bottom)
  156.                 g.setBottom(g.bottom() + dy);
  157.            
  158.             setGeometry(g);
  159.            
  160.             m_old_pos = QPoint(!left ? e->x() : m_old_pos.x(), e->y());
  161.         } else {
  162.             QRect r = rect();
  163.             left = qAbs(x - r.left()) <= 5;
  164.             right = qAbs(x - r.right()) <= 5;
  165.             bottom = qAbs(y - r.bottom()) <= 5;
  166.             bool hor = left | right;
  167.            
  168.             if (hor && bottom) {
  169.                 if (left)
  170.                     setCursor(Qt::SizeBDiagCursor);
  171.                 else
  172.                     setCursor(Qt::SizeFDiagCursor);
  173.             } else if (hor) {
  174.                 setCursor(Qt::SizeHorCursor);
  175.             } else if (bottom) {
  176.                 setCursor(Qt::SizeVerCursor);
  177.             } else {
  178.                 setCursor(Qt::ArrowCursor);
  179.             }
  180.         }
  181.     }
  182.    
  183.     void mouseReleaseEvent(QMouseEvent *e)
  184.     {
  185.         m_mouse_down = false;
  186.     }
  187.    
  188. private:
  189.     TitleBar *m_titleBar;
  190.     QWidget *m_content;
  191.     QPoint m_old_pos;
  192.     bool m_mouse_down;
  193.     bool left, right, bottom;
  194. };
  195.  
  196.  
  197. #include "main.moc"
  198.  
  199. int main(int argc, char **argv)
  200. {
  201.     QApplication app(argc, argv);
  202.    
  203.     Frame box;
  204.     box.move(0,0);
  205.    
  206.     QVBoxLayout *l = new QVBoxLayout(box.contentWidget());
  207.     l->setMargin(0);
  208.     QTextEdit *edit = new QTextEdit(box.contentWidget());
  209.     l->addWidget(edit);
  210.    
  211.     box.show();
  212.     return app.exec();    
  213. }

Note that some strange behavior may be seen when resizing the window really
small horizonally to the right on Windows (the window will start moving
instead of resizing). This is due to limitations on Windows and there is
nothing we can do about this unfortunately.

4 comments

January 10, 2011

Picture of daugoh daugoh

Lab Rat

Nice !
But what happen if one add it into a QMdiArea and click on minimize ?
I think the window disappear …

May 19, 2011

Picture of sigrid sigrid

Lab Rat

Possibly. This is just an example that is meant to give some tips on how you can create a custom titlebar in general. You need to tweak it to your specific use case, in order to get the behavior that is needed for you though.

April 10, 2012

Picture of gsmember gsmember

Lab Rat

Thanks for the example, that was very useful.
But now i have a small problem:
When I press down a button in the titlebar and move the mouse the entire frame moves also. I tried the following code in the “mousePressEvent(QMouseEvent *me)” method:

if(ui->pushButtonClose->isDown()||ui->pushButtonMinimize->isDown()){ return;
}
parentWidget()->move(me->globalPos()-clickPos);

But that dowsn’t work. The method can’t access to the “ui->pushButtonClose->isDown()”.
How can that be achieved?

Best regards

September 6, 2012

Picture of hdeldar hdeldar

Lab Rat

In TitleBar class change this functions:

  1.  bool titleBarPress;
  2.   void mousePressEvent(QMouseEvent *me)
  3.     {
  4.         startPos = me->globalPos();
  5.         clickPos = mapToParent(me->pos());
  6.         titleBarPress = true;
  7.     }
  8.     void mouseMoveEvent(QMouseEvent *me)
  9.     {
  10.         if (maxNormal || !titleBarPress)
  11.             return;
  12.         parentWidget()->move(me->globalPos() - clickPos);
  13.     }
  14.     void mouseReleaseEvent(QMouseEvent *)
  15.     {
  16.         titleBarPress = false;
  17.     }

Write a comment

Sorry, you must be logged in to post a comment.