Visual Studio Qt tools - signal/slot mechanism?
-
Hello all,
I would like to kindly ask for help with Qt Tools for VS2015. My setup is:
VS Pro 2015, Version 14.0.25431.01 (update 3), Qt 5.12.1, Qt VS Tools v2.3.2, VS remote debugger 2015, deployment via {path_to_qt}/5.12.1/msvc2017_64/bin/windeployqt.exeI choosed the Qt extension for VS since I wasn't able to run remote debug (Win10 <-> Win10) with Qt Creator. After some fiddling around with the Qt VS Tools, the remote debug seems to work for me. However, I haven't found a way how to connect a signal comming from a GUI element to a defined slot function - the Qt creator has a few-click-solution for that, but there's no "Go to slot" option in the Qt designer in VS.
I have found following link mentioning the naming convention for user-defined slot functions:
https://stackoverflow.com/revisions/43918538/2Say I have a push button called pushButton and I want to trigger an event on its release. So in the main .cpp file (the one that calls
ui.setupUi(this);
I have implemented following:
void on_pushButton_released(void) { //... slot handling code here }
What seemed to be fine was that the compiler was not complaining about missing prototype of that function (I haven't declared it in the header), so I was expecting that some "background magic" handles that. But no matter what, when I press and release the pushButton while debugging, this function is not called. Declaring its prototype in the header doesn't help. Also, when I have grep-ed through all the project files, I haven't found any more references to that function...
Can anybody please explain how this shall be done, considering my setup?
Many thanks in advance
Jindra Sindelar -
@jindraSindelar
I'm not a C++ expert, but I don't think you have to declare a function in a header file just because it exists in the.cpp
, and you're not calling it anyway. So I don't think you'll get warnings if you've misspelt the proposed slot.You won't see any exact source references to
on_pushButton_released
because that will get generated viaQMetaObject::connectSlotsByName()
.I can see that if you don't get it quite right your proposed slot won't get connected to signal.
I think you should show the exact (relevant) source you are trying (including spelling of variable, what class it and your slot are inside), e.g. did you do the
slots
bit of:private slots: void on_pushButton_released();
-
@jindraSindelar May I suggest writing the code explicitly? This makes your code easier to follow.
class MyWidget : public QWidget { Q_OBJECT public slots: void mySlot() { //... slot handling code here } ... };
MyWidget::MyWidget(QWidget *parent) : QWidget(parent) { ui->setupUi(this); connect(ui->pushButton, &QPushButton::released, this, &MyClass::mySlot); ... }
Or, if you don't need the slot to be called from anywhere else, then you don't even need a separate function. Just write your slot handling code in a lambda expression:
MyWidget::MyWidget(QWidget *parent) : QWidget(parent) { ui->setupUi(this); connect(ui->pushButton, [=]() { //... slot handling code here }); ... }
-
Hello gents,
thank you for your inputs, I'm now stuck a bit on a more urgent project, but I will definitelly take your advices and hopefully make it running :)
Best regards
Jindra -
Okay, I got it running thanks to your help, I'll post my solution for the case anybody will run into similar issues:
"main.h":
#include <QtWidgets/QWidget> #include "ui_HwMonTool_app.h" #include "PresentLogic.h" class HwMonTool_app : public QWidget { Q_OBJECT public: HwMonTool_app(QWidget *parent = Q_NULLPTR); private: Ui::HwMonTool_appClass ui; CPresentLogic logic; };
"main.cpp":
#include "HwMonTool_app.h" HwMonTool_app::HwMonTool_app(QWidget *parent) : QWidget(parent) { ui.setupUi(this); // when adding more signal-slot connections, follow this syntax: connect(ui.pushButton, &QPushButton::released, &(this->logic), &CPresentLogic::buttonPressed); }
"PresentationLogic.h":
#include "qobject.h" class CPresentLogic : public QObject { Q_OBJECT public: CPresentLogic() {}; ~CPresentLogic() {}; public slots: void buttonPressed(void) { // put breakpoint here while (0); } };
When running this code in debugger and pressing (releasing) the pushButton, the buttonPressed() slot is called.
Thanks again for all the help, this topic can now be locked :)