Qt纯代码实现菜单栏、工具栏、状态栏
目錄
菜單欄
工具欄?
狀態(tài)欄
總體效果
在QWidget中實現(xiàn)菜單欄、工具欄、狀態(tài)欄?
其他
子窗口獲取父窗口指針
QWidget阻塞模式
本篇演示的例子是在QMainWindow中進行的,在QWidget中可采取另外的方法,在文末會涉及。
菜單欄
步驟:創(chuàng)建菜單欄->創(chuàng)建菜單項->創(chuàng)建子項(動作QAction)->(QAction設(shè)置圖片->QAction設(shè)置快捷鍵->)將子項添加到菜單項->將菜單項添加到菜單欄
其中設(shè)置圖片時要先將圖片加載到qrc中,如果圖片較多可以單獨創(chuàng)建一個文件夾。
?代碼:
QMenuBar pMenuBar = menuBar();//菜單-文件 QMenu *pMenuBarFile= new QMenu(QStringLiteral("文件(&F)")); QAction *pActionOpen = new QAction(QStringLiteral("打開"), this); pActionOpen->setIcon(QIcon(":/ReplicationTool/png/open.png")); pActionOpen->setShortcut(Qt::CTRL | Qt::Key_O); pMenuBarFile->addAction(pActionOpen);pMenuBar->addMenu(pMenuBarFile);//... //繼續(xù)添加其他菜單項效果:
工具欄?
要實現(xiàn)帶圖片的工具欄圖標,并且文字要顯示圖標下面,我們選擇使用QToolButton。如果使用QAction則圖片和文字只能顯示一個。用QAction的步驟與創(chuàng)建菜單欄的一致。
步驟:創(chuàng)建工具欄->創(chuàng)建工具欄按鈕->添加圖片->設(shè)置文字->設(shè)置圖片與文字的位置->將工具欄按鈕放進工具欄
代碼:
QToolBar pToolBar = addToolBar("toolBar");QToolButton *pActionOpenBar = new QToolButton(this); pActionOpenBar->setIcon(QIcon(":/ReplicationTool/png/open.png")); pActionOpenBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); pActionOpenBar->setText(QStringLiteral("打開")); pToolBar->addWidget(pActionOpenBar);//添加其他按鈕... //添加分隔符 pToolBar->addSeparator();?效果:
另外設(shè)置圖片大小:
pActionOpenBar->setIconSize(QSize(50, 50));狀態(tài)欄
步驟:創(chuàng)建狀態(tài)欄->創(chuàng)建需要往上面添加的控件->將控件添加到狀態(tài)欄
下面創(chuàng)建一個狀態(tài)欄,上面顯示系統(tǒng)狀態(tài)、進度條和系統(tǒng)時間。
代碼:
QStatusBar *pStatusBar = statusBar();QLabel *pWelCome = new QLabel(QStringLiteral(" 就緒")); pStatusBar->addWidget(pWelCome);QProgressBar * pProgressBar = new QProgressBar(); pProgressBar->setRange(0,100); pProgressBar->setValue(20); pProgressBar->setMaximumWidth(200); pStatusBar->addPermanentWidget(pProgressBar); pTimeLabel = new QLabel(); pStatusBar->addPermanentWidget(pTimeLabel);QTimer *pTimer = new QTimer(this); timeUpdate_Slot(); pTimer->start(1000); connect(pTimer, SIGNAL(timeout()),this,SLOT(timeUpdate_Slot()));//更新時間的槽函數(shù)中的代碼 QDateTime currentTime = QDateTime::currentDateTime(); QString timeStr = currentTime.toString(QStringLiteral("yyyy年MM月dd日 hh:mm:ss"));pTimeLabel->setText(timeStr);效果:
總體效果
再加上兩個懸浮窗口 ,就可以組成一個經(jīng)典軟件界面。
?
在QWidget中實現(xiàn)菜單欄、工具欄、狀態(tài)欄?
由于Qwidget沒有menuBar(),addToolBar(),statusBar(),因此我們創(chuàng)建好后要給他指定位置。
一個好的方法就是先在界面上拖幾個QFrame到指定位置,分別放菜單欄、工具欄、狀態(tài)欄 ,如下
拖進去后適當調(diào)整一下高度,長可以在代碼中調(diào)整。?
然后分別以對應的QFrame為父窗口new出菜單欄、工具欄、狀態(tài)欄對象,下面以菜單欄為例:
//將菜單放入QFrame并設(shè)置與界面等寬 ui.frameMenu->resize(this->geometry().width(), ui.frameMenu->height()); QMenuBar *pMenuBar = new QMenuBar(ui.frameMenu); pMenuBar->setFixedSize(ui.frameMenu->width(), ui.frameMenu->height());//剩下操作與QMainWindow中一致 //略...其他
子窗口獲取父窗口指針
可采取如下安全強制轉(zhuǎn)換的方式?
MainDlg *pMainDlg = qobject_cast<MainDlg*>(this->parentWidget());然后再通過指針去調(diào)用父窗口方法,但該方法必須為public的。
或者直接發(fā)信號給父窗口,讓父窗口去調(diào)用其方法。
QWidget阻塞模式
QDialog可以直接用exec(),而QWidget的show()是非阻塞的。
解決方案是:
假設(shè)要在WidgetA中彈出阻塞的WidgetB,則
1 在WidgetA中創(chuàng)建WidgetB時以WidgetA為父窗口
2 在WidgetB的構(gòu)造函數(shù)里設(shè)置相關(guān)屬性
代碼:
//widgeta.cpp WidgetB *pWidgetB = new WidgetB(this); pWidgetB->show();//widgetb.cpp WidgetB::WidgetB(QWidget *parent): QWidget(parent) {ui.setupUi(this);this->setWindowFlags(Qt::Dialog);this->setWindowModality(Qt::WindowModal);//... }?
總結(jié)
以上是生活随笔為你收集整理的Qt纯代码实现菜单栏、工具栏、状态栏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小科普:什么是屏幕分辨率
- 下一篇: Day82_ELK(一)