Sharethefun
Sharethefun

Reputation: 824

Signals and Slots with Qtoolbutton

I have created a ToolButton with my qt designer and Im trying to connect it to a slot. I wrote this

connect(ui->toolButton_addfiles, SIGNAL(triggered()), this, SLOT(changeDirectory()));

Im able to run the program but when I press the button I see the following log into my qt Application Output :

   Object::connect: No such signal QToolButton::triggered() in ../RightDoneIt/rightdoneit.cpp:10
    Object::connect:  (sender name:   'toolButton_addfiles')
    Object::connect:  (receiver name: 'RightDoneIt')

How can I make this connection work ?

Upvotes: 4

Views: 9279

Answers (5)

dams
dams

Reputation: 17

triggered is more for actions, not for buttons, but in Qt Creator, you can connect your button to an action on pressed(), if that action is connected with a trigger as well it will trigger the action.

Example:
myAction (connect)>> triggered >> myMethod()
myButton (connect)>> pressed >> myAction
Result:
myButton (pressed) >> myMethod().

The advantage then is you can reuse your action for several buttons then, and using QtCreator you can do that with 0 line of code.

Upvotes: 0

J. Hughes
J. Hughes

Reputation: 11

QToolButton() has the signal method triggered(QAction *), this signal is to be addressed if triggering of the related QAction connected to the QToolbutton is of interest. At the same time QToolButton is inherited from QAbstractButton(), which has the toggled(bool checked) signal. If you want to catch the signal triggered by pressing/unpressing the tool button, you may do as following:

   auto toolbutton = new QToolButton(this);
     connect(toolbutton , &QAbstractButton::toggled, this, []() { // your code });

Alternatively (I have not checked if this solution works), you may define explicitly, what signal to be caught

    connect(toolbutton , qOverload<bool>(&QToolButton::toggled), this, [](bool val) { // your code});

Upvotes: 1

Patrice Bernassola
Patrice Bernassola

Reputation: 14416

As the error says, there's no signal triggered() but triggered(QAction*) in the QToolButton.

Edit In the connect function you must have the signal signature like triggered(QAction*) since QToolButton class has no signal triggered() (with no parameter) declared

Upvotes: 4

Andy M
Andy M

Reputation: 6065

You could use the auto-connection process of Qt.

In the class referencing your UI, create a slot called :

on_toolButton_addfiles_clicked();

Exemple :

See : A Dialog With Auto-Connect

class ImageDialog : public QDialog, private Ui::ImageDialog
{
    Q_OBJECT

public:
    ImageDialog(QWidget *parent = 0);

private slots:
    void on_okButton_clicked();
};

Hope this helps !

Edit : No triggered signals in qAbstractButton. See http://doc.qt.nokia.com/4.7/qabstractbutton.html

Upvotes: 2

asdfjklqwer
asdfjklqwer

Reputation: 3594

I'm guessing you're creating a QAction, adding it to the QToolButton and trying to connect it to a slot in your own class?

You can connect your slot to either the QToolButton::triggered(QAction*) signal or directly to the QAction::triggered() signal. Either way, the QAction must be added to the QToolButton through QWidget::addAction(QAction*), the slot's method signature must match the signal's signature, and the connect invocation must include the signal/slot parameters, not just the names of the signal and slot.

Upvotes: 0

Related Questions