QToolbar first item not clickable?
-
Very very weird issue. I've got code for a GUI I'm working on, and whatever action I add as the first item in the QToolbar is not clickable like usual.
So, when I do this:
@
//----- Set up the top toolbar
QToolBar* pToolBar = addToolBar("Connections");
pToolBar->setObjectName("ConnectionBar");_pConnectAction = pToolBar->addAction("Connect");
connect(_pConnectAction, SIGNAL(triggered()),
this, SLOT(connectToServer()));
@I get a QAction, which I cannot click, like this:
!http://i.imgur.com/yhk87Lq.png()!
When I add another QAction in front, like this:
@
QToolBar* pToolBar = addToolBar("Connections");
pToolBar->setObjectName("ConnectionBar");pToolBar->addAction("foo you");
_pConnectAction = pToolBar->addAction("Connect");
connect(_pConnectAction, SIGNAL(triggered()),
this, SLOT(connectToServer()));
@I get this (notice that now Connect is clickable)
!http://i.imgur.com/AHNyhax.png()!
This is running in a QMainWindow subclass constructor.
What have I done wrong?
-
Why are you passing a string parameter to the addAction method? The method has the following signature:
void addAction(QAction *action);
You should create an action first, then add it to a toolbar. Try something like this:
@
QAction *deleteAction = new QAction(QIcon(":/images/delete.png"), tr("&Delete"), this);
deleteAction->setShortcut(tr("Delete"));
deleteAction->setStatusTip(tr("Delete item from diagram"));
connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));QToolBar *editToolBar = addToolBar(tr("Edit")); editToolBar->addAction(deleteAction);
@
-
Um, there is an overloaded version of addAction that makes the QAction for you?
http://qt-project.org/doc/qt-4.8/qtoolbar.html#addAction-2
Shouldn't have to make it first, then add it. The overload should work fine, and in fact does so for the second item.
-
Yep, you're right. Haven't seen it, sorry about that.
This is strange indeed. Which version are you using? I just ran this simple test and it worked perfectly, with Qt 5.0.1 on Linux.
@
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
MainWindow(QWidget *parent = 0);
virtual ~MainWindow();(...)
private slots:
void testPrintRun();
void testPrintStop();private:
void createToolBars();QToolBar *toolbar; QAction *run; QAction *stop; (...)
};
(...)
void MainWindow::createToolBars()
{
toolBar = addToolBar(tr("Run toolbar"));
toolBar->setObjectName("run_toolbar");run = toolBar->addAction("runAct"); stop = toolBar->addAction("stopAct"); connect(run, SIGNAL(triggered()), this, SLOT(testPrintRun())); connect(stop, SIGNAL(triggered()), this, SLOT(testPrintStop()));
}
void MainWindow::testPrintRun()
{
std::cout << "Run action clicked" << std::endl;
}void MainWindow::testPrintStop()
{
std::cout << "Stop action clicked" << std::endl;
}@