Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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
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:
MainWindow::MainWindow(QWidget *parent) :
connect(ui->actionToolBarButton, SIGNAL(triggered()), ui->PromotedQWidget, SLOT(mySlot()));
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.
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.
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
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!