Important: Please read the Qt Code of Conduct -

[Solved] Qt4: Connect toolbar buttons to the slot of a promoted widget

  • Hi, I am using Qt 4.8.5 with MSVC2010 compiler and windows 7.

    I've successfully promoted one of the QWidget to my custom class using designer in Qt creator.

    Suppose the custom class looks like this:
    @class CustomClass: public QWidget
    private slots:
    void mySlot();

    and suppose the QWidget that is promoted to CustomClass is named :

    Now I attempt to connect one of the toolbar button to this promoted widget.
    In my mainwindow.cpp, I wroted this code:

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "customclass.h"

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    connect(ui->actionToolBarButton, SIGNAL(triggered()),
            ui->PromotedQWidget, SLOT(mySlot()));


    delete ui;

    Although the auto-completion shows that mySlot exists behind ui->PromotedQWidget,
    I found this connect doe not work. When I clicked the actionToolBarButton, nothing really happened.

    I think there might be something wrong in this way of connection, but I don't know where to start.

    Could someone please give me a hint? Thanks in advance.

  • Lifetime Qt Champion


    If the connection fails, you should have an informative message on your console.

    Did you add Q_OBJECT to your CustomClass ?
    Also, why make the slot private since you want to connect it from outside ?

  • Thank you SGalst. I got this message: Object::connect: No such slot QCustomPlot::mySlot()

    Also I found it could be the problem of Q_OBJECT.
    My CustomClass doesn't have Q_OBJECT macro. Actually it inherits from another CustomBaseClass which has Q_OBJECT macro and I think it should be inherited too. The weird thing is, after I add Q_OBJECT to CustomClass, the compiler shows error lnk2001 unresolved external symbol.

    I'll look into it. Thanks for your response, it's very informative to me.

  • Lifetime Qt Champion

    You need to put the macro each time you are going to use signals and slots in a QObject derived class. It will allow to create the meta object data needed by Qt to do its magic.

    Is the slot implemented somewhere ?

  • The slot is declared in customclass.h, and implemented in customclass.cpp.
    I am still fixing the problem of Q_OBJECT. I cannot put the macro in my CustomClass , which is derived from CustomBaseClass, and the CustomBaseClass is derived from QWidget. In other words, the CustomBaseClass can use Q_OBJECT but the derived CustomClass cannot

  • Lifetime Qt Champion

    Why can't it ?

  • Thank you for the response, I've fixed it!

    Some other slots of the base class are implemented elsewhere. I am restructuring my porgram, its too painful for debugging!

Log in to reply