2013-05-22 46 views
5

工具栏如何在顶栏中实现,例如,Tiled是否已完成?在Qt中组合工具栏和标题栏

Tiled 通常情况下,工具栏看起来像如下:它

enter image description here

示例代码如何目前:

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

你为什么不看瓷砖的来源? –

回答

6

.x,您可以使用QMainWindow中包含的setUnifiedTitleAndToolBarOnMac(bool set)函数:

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

+0

请不要张贴仅链接的答案。链接可以是对答案的有用补充,但答案在没有链接的情况下应该是可读的。在你的情况下,它将不再包含任何信息。 – Sumurai8

+0

虽然这个链接可能回答这个问题,但最好在这里包含答案的重要部分,并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 – Kjuly

+0

这也适用于QT 5.2+。 – Appleshell

1

如果你继承的QMainWindow和/或在QToolBar,改变那种他们加载/拥有的框架,你可能能够得到你想要的效果。

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

另一种方式,也许达到这个结果,而是一种哈克,你可以把正确的颜色的另一种无框部件在框架的一部分正在展示。使用Qt:ToolQt::WindowStaysOnTopHintQt::FramelessWindowHint,你应该能够让你的掩护小部件隐藏关节。

希望有所帮助。

0

您可以尝试使用样式表去除框架边框。

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