Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I draw the close, minimize, and maximize buttons in Qt?

Tags:

c++

qt

I created a this->setWindowFlags(Qt::FramelessWindowHint); and so there is no title bar. Therefore, I am implementing my own. I wanted to know, however, before I continue whether there is a standard way to add the close, minimize, and maximize buttons in a native-os looking way (i.e. on windows it should look like the windows close buttons and the same for osx and linux).

like image 836
chacham15 Avatar asked Dec 22 '12 01:12

chacham15


People also ask

What do you call the bar with minimize maximize and close button?

Apple calls them Title Bar Buttons. Note that they're named Close, Minimize and Zoom.


1 Answers

QStyle take a lot of standard icons base on OS style. You can get this icon from current OS style and then draw it by your self.

This is a simple implementation for reference.

class TitleBar : public QWidget
{
    Q_OBJECT
public:
    explicit TitleBar(QWidget *parent = 0)
        :QWidget(parent)
    {
        QStyle *style = qApp->style();
        QIcon closeIcon = style->standardIcon(QStyle::SP_TitleBarCloseButton);
        QIcon maxIcon = style->standardIcon(QStyle::SP_TitleBarMaxButton);
        QIcon minIcon = style->standardIcon(QStyle::SP_TitleBarMinButton);

        QPushButton *min = new QPushButton(this);
        QPushButton *max = new QPushButton(this);
        QPushButton *close = new QPushButton(this);
        min->setIcon(minIcon);
        max->setIcon(maxIcon);
        close->setIcon(closeIcon);

        QHBoxLayout *layout = new QHBoxLayout(this);
        layout->setSpacing(0);
        layout->addWidget(min);
        layout->addWidget(max);
        layout->addWidget(close);
        setLayout(layout);
    }
};
like image 50
liuyi.luo Avatar answered Oct 13 '22 01:10

liuyi.luo