Combine toolbar and title bar in Qt

拟墨画扇 提交于 2019-12-23 10:09:48

问题


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

Normally, the toolbar looks like the follows:

Example code how it is currently:

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

回答1:


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

See also: https://qt-project.org/doc/qt-4.8/qmainwindow.html#unifiedTitleAndToolBarOnMac-prop




回答2:


If you are using Qt5, check out QtMacExtras http://qt.gitorious.org/qt/qtmacextras

You can give your QToolBar a native Mac look and feel using QtMacExtras::setNativeToolBar




回答3:


If you subclass QMainWindow and/or QToolBar and change the kind of frame they load/have, you may be able to get the effect you want.

http://qt-project.org/doc/qt-4.8/qframe.html#details

http://qt-project.org/doc/qt-4.8/stylesheet-examples.html#customizing-qframe

http://qt-project.org/doc/qt-4.8/stylesheet-customizing.html#the-box-model

Another way to maybe achieve this result, but is kind of hacky, you could put another frameless widget of the right color over the part of the frames that are showing. Using Qt:Tool and Qt::WindowStaysOnTopHint and Qt::FramelessWindowHint, you should be able to get your coverup widget to hide the joint.

Hope that helps.




回答4:


You could try to remove the frame border using a stylesheet.

tbar->setStyleSheet("QToolBar { border: 0px }");


来源:https://stackoverflow.com/questions/16681965/combine-toolbar-and-title-bar-in-qt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!