0

I have the following piece of code, for some reason, from the UI window, MyActionDock inherited from QToolBar, it is displayed without any problem, when clicked on the button, the button color also changed, but the slots (a1ActionSlot(), and a2ActionSlot()) connected to the signals are never called, feel like the action is never triggered. I'm using Qt 4.7.2. What's wrong with it? Thanks a lot.

I believe the code used to work properly for Qt4.6 or earlier. Don't know when the problem happens.

MyActionDock::MyActionDock (QWidget *parent) :
   QToolBar (parent)
{
   setOrientation (Qt::Vertical);
   setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
   setFixedWidth(canvas()->toolsDockWidth());

   // ACTIONS

   QToolButton * a1btn= new QToolButton (this);

   a1btn->setText("Action 1");
   a1btn->setIcon(QIcon("a1.png"));
   a1btn->setToolTip ("Some action a1");
   a1btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

   QAction *a1Action = addWidget(a1btn);
   connect (a1Action , SIGNAL (triggered()), this, SLOT(a1ActionSlot()));
   addAction (a1Action);

   QToolButton * a2Btn = new QToolButton (this);

   a2Btn ->setText("A2");
   a2Btn ->setIcon(QIcon("a2.png"));
   a2Btn ->setToolTip ("something");

   QAction *a2Action= addWidget(a2Btn );
   connect (a2Action, SIGNAL (triggered()), this, SLOT(a2ActionSlot()));
   addAction (a2Action);

}

void MyActionDock::a1ActionSlot()
{
    //do something
}

void MyActionDock::a2ActionSlot()
{
   //do something
}

2 Answers2

3

As Jay suggested, directly connect to the QToolButton and don't addAction, then it works. Think this is a Qt upgrade related problem. The code used to work in Qt 4.6 or earlier, but it stopped working after 4.7. So for 4.7 if you want to use QToolButton, direct connect the button's signal.

   QToolButton * a2Btn = new QToolButton (this);

   a2Btn ->setText("A2");
   a2Btn ->setIcon(QIcon("a2.png"));
   a2Btn ->setToolTip ("something");

   addWidget(a2Btn );
   connect (a2Btn , SIGNAL (clicked()), this, SLOT(a2ActionSlot()));
1

The slot is in the wrong class.

You declare the slot a1ActionSlot is in the class MyActionDock here:

connect (a1Action , SIGNAL (triggered()), this, SLOT(a1ActionSlot()));

The third parameter is 'this' (which points to the MyActionDock class).

You instantiate the a1ActionSlot method in the class QtCanvasActionDock.

void QtCanvasActionDock::a1ActionSlot()
Jay
  • 13,803
  • 4
  • 42
  • 69
  • Sorry, my mistake, when changed the name, forgot to change those part. Now corrected. – nowgains nowpains Jan 25 '13 at 21:54
  • Did you declare the a1ActionSlot method as a slot? ( "public slots:" ) – Jay Jan 25 '13 at 21:55
  • Yes. The slots are declared in the header file. And the header is also a Q_OBJECT and compiled using moc. it used to work for older versions of Qt. – nowgains nowpains Jan 25 '13 at 21:58
  • The Actions will set up and connect the events for you. I think doing it manually then using an action are conflicting. Try just using a regular button and a connect() but no action. – Jay Jan 25 '13 at 22:00
  • I use addWidget( Button ); and connect( Button, SIGNAL( clicked() ), this, member ); but do not call AddAction() and it works for me – Jay Jan 25 '13 at 22:06