Can you create slots in main.cpp file?
-
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...