Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine toolbar and title bar in Qt

Tags:

macos

qt

qt5

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

Tiled Normally, the toolbar looks like the follows:

enter image description here

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);
    }
};
like image 759
Appleshell Avatar asked May 22 '13 01:05

Appleshell


3 Answers

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

like image 51
Jens Avatar answered Oct 12 '22 17:10

Jens


If you are using Qt5, check out QtMacExtras (https://github.com/qt/qtmacextras)

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

like image 30
user1024992 Avatar answered Oct 12 '22 17:10

user1024992


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.

like image 1
phyatt Avatar answered Oct 12 '22 18:10

phyatt