十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶(hù) + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專(zhuān)業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
菜單欄( QMenu )下的動(dòng)作是由QAction來(lái)實(shí)現(xiàn)。有時(shí)需要對(duì)QAction的文字顯示做一些要求,如文字左對(duì)齊顯示、文字顯示在圖標(biāo)(QIcon)上下左右方位等。
查詢(xún)了QAction的方法,并沒(méi)有這樣的屬性實(shí)現(xiàn),而且QAction本身的圖標(biāo)具有強(qiáng)制性,不設(shè)置也占位置(自己測(cè)試如此,未發(fā)現(xiàn)消除圖標(biāo)所占位置的方法)。而網(wǎng)上很多對(duì)QAction文字和圖標(biāo)相關(guān)屬性設(shè)置的方法大多為工具欄( QToolBar )下的QAction,即對(duì)QToolBar的風(fēng)格設(shè)置方法:setToolButtonStyle(),并沒(méi)有對(duì)菜單欄( QMenu )。
而其實(shí)Qt給提供了實(shí)現(xiàn)自定制QAction風(fēng)格的類(lèi),即:QWidgetAction。他有一個(gè)指定QWidget的方法:

void setDefaultWidget(QWidget * widget);
這樣我們就可以自己繪制想要的效果窗口,然后設(shè)置給QAction。相對(duì)來(lái)說(shuō),自由度很高。
下面是我實(shí)現(xiàn)的一個(gè)例子,僅供參考。
#include
#include
#include
#include
QActionWidgetTest::QActionWidgetTest( QWidget *parent )
: QWidget( parent )
{
ui.setupUi(this);
resize( 600, 400 );
QMenuBar* pMenuBar = new QMenuBar( this );
QMenu* pMenu = new QMenu( "MenuTest" );
pMenu->setFixedWidth( 150 );
QWidgetAction* pActionLeft = new QWidgetAction( pMenu );
QWidgetAction* pActionCenter = new QWidgetAction( pMenu );
QWidgetAction* pActionRight = new QWidgetAction( pMenu );
QLabel* pLabLeft = new QLabel( "AlignLeft" );
QLabel* pLabCenter = new QLabel( "AlignCenter" );
QLabel* pLabRight = new QLabel( "AlignRight" );
pLabLeft->setAlignment( Qt::AlignLeft );
pLabCenter->setAlignment( Qt::AlignCenter );
pLabRight->setAlignment( Qt::AlignRight );
pActionLeft->setDefaultWidget( pLabLeft );
pActionCenter->setDefaultWidget( pLabCenter );
pActionRight->setDefaultWidget( pLabRight );
pMenu->addAction( pActionLeft );
pMenu->addAction( pActionCenter );
pMenu->addAction( pActionRight );
pMenuBar->addMenu( pMenu );
} 效果圖如下:

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。