Can you create slots in main.cpp file?
-
I have a project that all the widgets are created in the main.cpp file main() function. I have a worker thread that is emitting a signal to the main thread to change the background color of the widget created by main function. Here is an example
QObject::connect(serialThread, SIGNAL(setReady(QString)), statusWidget, SLOT(setStyleSheet("background-color:gray")));
When running the program I get an error that says the connection could not be made with the above code.
I have also created connections to change text:
QObject::connect(serialThread, SIGNAL(setReady(QString)), myStatusText, SLOT(setText(QString)));
Neither of these are working. I do know the signals are probably being emitted because I have other signals fired at the same time which is going to another worker thread and I can see those working. I have tried creating a main.h file and adding slots to that and the main.cpp file and just having the connections fire the SLOTS but the compilers says I can't declare the slots after the main() function in the main.cpp file. Is there any way I can achieve what I am trying to do?
-
@ples76 said in Can you create slots in main.cpp file?:
SLOT(setStyleSheet("background-color:gray")
You can't pass parameters to slots this way with the old signal/slots syntax.
QObject::connect(serialThread, SIGNAL(setReady(QString)), myStatusText, SLOT(setText(QString)));
what error do you get here?
Use the new signal/slot syntax to get errors during compile time instead runtime: https://wiki.qt.io/New_Signal_Slot_Syntax
-
Thank you for reply. I shouldn't have mentioned the second connection. I will try to figure that out. I really need to try to get a working solution for the first one. I think I understand now why the first code setStyleSheet does not work. This is because the parameters dont match. Can I use lambda expression to change the background color of the widget? I tried changing the connection to
QObject::connect(serialThread, SIGNAL(setReady()), [](){statusWidget->setStyleSheet("background-color:gray");});
but compiler throws an error saying that statusWidget was not captured in lambda expression. I am using QT version 4.8.7. As you can tell I am new to QT and cpp. Any help would be greatly appreciated
-
@ples76 said in Can you create slots in main.cpp file?:
error saying that statusWidget was not captured in lambda expression
This is exactly the issue: you do not capture anything, change to:
QObject::connect(serialThread, SIGNAL(setReady()), [this](){statusWidget->setStyleSheet("background-color:gray");});
-
int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget w; w.resize(500,50); w.show(); QTimer t; QObject::connect(&t, &QTimer::timeout, &w, [&w]()->void{w.setStyleSheet("background-color:red;");}); t.start(5000); return app.exec(); }
-
Thank you for your responses. My widget is created as a pointer like this:
QWidget *statusWidget = new QWidget;
So I created connection based on your response like this:
QObject::connect(serialThread, SIGNAL(setReady()),statusWidget, [statusWidget]()->void{statusWidget->setStyleSheet("background-color:gray;");});
Compiler throws the following error:
main.cpp: In function ‘int main(int, char**)’:
main.cpp:101:144: error: no matching function for call to ‘QObject::connect(LeptonSerial*&, const char [12], QWidget*&, main(int, char**)::<lambda()>)’
QObject::connect(serialThread, SIGNAL(setReady()),statusWidget, statusWidget->void{statusWidget->setStyleSheet("background-color:gray;");}); -
@ples76 said in Can you create slots in main.cpp file?:
I have a project that all the widgets are created in the main.cpp file main() function
Did you inherit the project with such way of creating Qt widgets, or just you started it that way?
QObject::connect(serialThread, SIGNAL(setReady()),statusWidget, statusWidget->void{statusWidget->setStyleSheet("background-color:gray;");});
As @Christian-Ehrlicher already suggested, please use the new syntax for signal & slots...
-
If you can't use qt5 you have to define a custom slot and call the function with the correct parameter there since you can't mix old style connect and lambdas.
-
Christian,
I was afraid that was the case. Do you know where I could locate an example of this. The problem I am having is I have found several examples of creating custom slots but they are all in classes outside of main. I have the issue of the widgets being created in the main function. Is it possible to create a custom slot that can be used in main and thus have the widget in scope?
This is my first project in qt and I am trying to find the easiest solution to this since I am under the gun to get this done asap. -
A slot must be in a class, you have no other chance.
-
Thank you for your patience with my small amount of knowledge on the subject. I appreciate the help and clarification on my issue. I think my best choice of action is to try and move my code out of main and maybe create a QMainWindow class and see if everything will work that way. Do you think this is my best choice of action or will I possibly run into issues in a QMainWindow class also. I am not speaking about other code in main but specifically about the connections mentioned in this thread. Basically I am asking if am able to move the code to a QMainWindow Class will I then be able to create custom slots to achieve my goal?
-
@ples76 said in Can you create slots in main.cpp file?:
move my code out of main and maybe create a QMainWindow class
I was about to suggest something similar, but to subclass QApplication. So to move all the widgets instantiation there, and since your MySuperDuperQApplication class is an QObject you should be able to work with custom slots as suggested.
I have the issue of the widgets being created in the main function
Just in case, could it it be possible you show the source code for the main() ?
-
All this stuff should not be in main - this is really bad style. This should all go into the ctor of 'window' since this is the place where this all should happen.
-
This should all go into the ctor of 'window' since this is the place where this all should happen.
Yes, I agree with @Christian-Ehrlicher suggestion. Although you inherit the project that way, it seems there's no reason to continue that bad approach.
Your main() code should be reduced to something like this:
int main(int argc, char **argv) { QApplication a(argc, argv); QWidget window; window.show(); return a.exec(); }
-
I have solved the problem with all of your help Specifically LeLev who gave me the final working solution through PM. I created a QWidget class for my statusWidget. I added the following in the StatusWidget.h file:
#ifndef STATUSWIDGET_H #define STATUSWIDGET_H #include <QtCore> #include <QWidget> #include <QLabel> class StatusWidget : public QWidget { Q_OBJECT; public: StatusWidget(QWidget *parent = 0); ~StatusWidget(); public slots: void setBgColor(QString); }; #endif
And the StatusWidget.cpp file:
#include <ctime> #include <stdint.h> #include "StatusWidget.h" #include <QtCore> #include <QWidget> StatusWidget::StatusWidget(QWidget *parent) : QWidget(parent) { } StatusWidget::~StatusWidget() { } void StatusWidget::setBgColor(QString color) { this->setStyleSheet("background-color:" + color + ";"); }
I then changed my connections to the following:
QObject::connect(serialThread, SIGNAL(setReady(QString)),statusWidget, SLOT(setBgColor(QString))); QObject::connect(serialThread, SIGNAL(setReading(QString)), statusWidget, SLOT(setBgColor(QString))); QObject::connect(serialThread, SIGNAL(setReReading(QString)), statusWidget, SLOT(setBgColor(QString))); QObject::connect(serialThread, SIGNAL(setPass(QString)), statusWidget, SLOT(setBgColor(QString))); QObject::connect(serialThread, SIGNAL(setFail(QString)), statusWidget, SLOT(setBgColor(QString)));
I can now change my background color accordingly. Thank you all for your help!!!
-
@ples76 said in Can you create slots in main.cpp file?:
I have solved the problem with all of your help
Great, so please don't forget to mark your post as solved!
I am trying to find the easiest solution to this since I am under the gun to get this done asap
Although you find a solution now, you may want to take into account that having such a main() function is not a good idea as @Christian-Ehrlicher pointed out.
So time (and stakeholders) permitting, you might want to look at refactoring your code...