2009-09-04 41 views
0

我正在開發一個應用程序,我已經添加了一個QToolBar對象,並添加了QToolButton對象,我也連接了clicked()事件,但問題是鼠標點擊事件不能在QToolButton上工作,但是當我使用Tab時注意到那個時候,那麼空格按鈕工作正常,但我想用鼠標點擊它..任何想法?這裏是代碼。在Linux上QToolButton事件處理的問題

pToolBar = new QToolBar(this); 

pToolBar->setAllowedAreas(Qt::NoToolBarArea);//NoToolBarAreaAllToolBarAreas 
pToolBar->setFloatable(false); 
pToolBar->setGeometry(300,0,160,30); 

QToolButton *playButton=new QToolButton(pToolBar); 

playButton->setIcon(QIcon("/images/play.png")); 

playButton->setGeometry(10,0,40,30); 

playButton->setToolTip("Play/Pause"); 

connect(playButton, SIGNAL(clicked()),SLOT(playButtonClicked())); 
+0

當你點擊按鈕,會發生什麼?你的插槽是否被調用? – Thomi 2009-09-04 07:24:36

+0

不,它沒有被稱爲..按鈕不像一個按鈕,它看起來像沒有點擊..但是當我按空格鍵插槽被稱爲,它工作正常 – 2009-09-04 07:29:32

回答

0

嘗試明確地將工具按鈕添加到工具欄。下面的代碼工作完美的我:

QToolBar *pToolBar = new QToolBar(this); 

QToolButton *playButton=new QToolButton(pToolBar); 
playButton->setIcon(QIcon("/images/play.png")); 
playButton->setText("Play"); 
playButton->setToolTip("Play/Pause"); 
playButton->setGeometry(10,0,40,30); 

QAction *a = pToolBar->addWidget(playButton); 
a->setVisible(true); 

connect(playButton, SIGNAL(clicked()),SLOT(playButtonClicked())); 

你或許應該保存的QAction指針的地方,因爲它是指定鍵盤快捷鍵,開啓/關閉按鈕等讓我知道這對你的作品的最簡單的方法。如果沒有,那麼在這裏發佈一個完整的可編譯示例將幫助我們幫助您。你應該能夠得到一個小的演示程序,在一個或兩個文件中顯示你的問題。

乾杯,

1

工具按鈕,當新的QAction實例與在QToolBar ::的addAction()創建或者現有的動作被添加到工具欄與在QToolBar ::的addAction()通常創建。

例子:

QAction *newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this); 
newAct->setShortcut(tr("Ctrl+N")); 
newAct->setStatusTip(tr("Create a new file")); 
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile())); 
fileToolBar = addToolBar(tr("File")); 
fileToolBar->addAction(newAct); 

您可以使用觸發信號,觸發定的動作時,這個信號被髮射。

你舉的例子:

QToolButton *playButton=new QToolButton(pToolBar); 
connect(playButton, SIGNAL(triggered()),SLOT(playButtonClicked())); 
+0

感謝您的幫助,讓我試試看。 .. – 2009-09-08 06:16:04

+0

很高興幫助你! – jordenysp 2009-09-10 15:49:44

0

由於jordenysp間接地解釋,該API的QAction中心