结合Qt中的工具栏和标题栏 [英] Combine toolbar and title bar in Qt

查看:690
本文介绍了结合Qt中的工具栏和标题栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在顶部栏中实现工具栏,例如Tiled完成了吗?

How can the toolbar be implemented in the top bar, like, for example, Tiled has it done?

通常,工具栏如下所示:

Normally, the toolbar looks like the follows:

示例代码的当前状态:

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr) {
        auto *tbar = new QToolBar();  
        tbar->addWidget(new QPushButton("Push Me"));
        this->addToolBar(tbar);
    }
};

推荐答案

如果仍在使用Qt 4.x,则可以使用QMainWindow中包含的setUnifiedTitleAndToolBarOnMac(bool set)函数:

If you are still using Qt 4.x, you can just use the setUnifiedTitleAndToolBarOnMac(bool set) function that is included in QMainWindow:

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr) {
        auto *tbar = new QToolBar();  
        tbar->addWidget(new QPushButton("Push Me"));
        this->addToolBar(tbar);
        this->setUnifiedTitleAndToolBarOnMac(true); // activate Mac-style toolbar
    }
};

另请参见: https://qt-project.org/doc/qt- 4.8/qmainwindow.html#unifiedTitleAndToolBarOnMac-prop

这篇关于结合Qt中的工具栏和标题栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆