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

前端 未结 1 1513
忘了有多久
忘了有多久 2021-02-06 15:57

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

1条回答
  •  别跟我提以往
    2021-02-06 16:04

    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);
        }
    };
    

    0 讨论(0)
提交回复
热议问题