tags:

views:

319

answers:

3

I am developing an application, I have added a QToolBar object in that, and have added the QToolButton object on that, I have also connect the clicked() event with that but the problem is that the mouse click event don't work on QToolButton but when I bring focus on that using Tab, then space button works fine, but I want it with mouse click.. any idea? here is the code.

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()));
A: 

Try explicitly adding the toolbutton to the toolbar. The following code works perfectly for me:

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()));

You should probably save the QAction pointer somewhere, since it's the easiest way to assign keyboard shortcuts, enable / disable the button etc. Let me know if this works for you. If it doesn't, perhaps posting a complete compilable example here will help us help you. You should be able to get a small demo program that shows your problem within one or two files.

Cheers,

Thomi
+1  A: 

The Tool buttons are normally created when new QAction instances are created with QToolBar::addAction() or existing actions are added to a toolbar with QToolBar::addAction().

Example:

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);

You can use triggered signal, This signal is emitted when the given action is triggered.

Your example:

QToolButton *playButton=new QToolButton(pToolBar);
connect(playButton, SIGNAL(triggered()),SLOT(playButtonClicked()));
jordenysp
Thanks for your help, let me try it...
Ummar
Is a pleasure to help you!.
jordenysp
A: 

As jordenysp indirectly explains, the API is QAction centric

Henrik Hartz