Unsolved TrafficLight example as a widget in MainWindow
-
I have tried to connect it in main.cpp and in the constructor of mainwindow.cpp.
(This lines are not shown in the above code)
But it didn't work.I tried:
QObject::connect(&tlc, SIGNAL(lightChanged()), &tl, SLOT(repaint()));
QObject::connect(&tlc, SIGNAL(lightChanged()), &tl, SLOT(update()));
QObject::connect(&tlc, SIGNAL(lightChanged()), &tl, SLOT(show())); -
When you tried in constructor, did you remember to new the TrafficLightController also ?
if you did
TrafficLightController tlc;it would be deleted as soon as main window constructor was finished.
the code from main.cpp can not just be copied to main window.
The objects need to be NEW.ed. -
In the constructor of mainwindow I tried this version:
TrafficLightController * controller = new TrafficLightController();
connect(controller, SIGNAL(lightChanged()), this, SLOT(repaint()));
connect(controller, SIGNAL(lightChanged()), this, SLOT(update()));
connect(controller, SIGNAL(lightChanged()), this, SLOT(show())); -
but in original it was greenisOn(bool)
is that the same as some widget update ???
in original its from
TrafficLightController to the TrafficLightWidget ?
should that still not be the case???
-
Now I tried this versions in mainwindow constructor.
controller = new TrafficLightController();
connect(controller, SIGNAL(lightChanged()), trafficlight, SLOT(repaint()));
connect(controller, SIGNAL(lightChanged()), trafficlight, SLOT(update()));
connect(controller, SIGNAL(greenChanged(bool)), trafficlight, SLOT(greenisOn(bool)));green = new LightWidget(Qt::green); red = new LightWidget(Qt::red); yellow = new LightWidget(Qt::yellow); connect(controller, SIGNAL(lightChanged()), red, SLOT(repaint())); connect(controller, SIGNAL(lightChanged()), green, SLOT(update())); connect(controller, SIGNAL(lightChanged()), yellow, SLOT(greenisOn(bool)));
-
Hi
The only difference from main should be.
TrafficLightWidget tl = new TrafficLightWidget ; TrafficLightController tlc =new TrafficLightController; QObject::connect(tlc, SIGNAL(greenChanged(bool)), tl, SLOT(greenisOn(bool))); QObject::connect(tlc, SIGNAL(yellowChanged(bool)), tl, SLOT(yellowisOn(bool))); QObject::connect(tlc, SIGNAL(redChanged(bool)), &tl, SLOT(redisOn(bool)));
unless you change some other code. ??
If it could paint by itself in main.cpp
there should be no need to connect to update/repaint even if you
now do it in main window. It works the same regardless of what
it is put into. -
@Tobias89 I took traffic light example and using Qt Creator created default new project (Qt Widgets Application) and added individual Qt classes (LightWidget, TrafficLightWidget and TrafficLight, all based on QWidget). So far it should be pretty much what I imagine you have done.
Then I used Qt Creator to edit the only form in the project (mainwindow.ui) and added a Widget object from component palette (section Containers). The magic now is to promote that QWidget into the TrafficLight class, and that's it. No need to do anything else in main.cpp. Please see mine below, no references to any widget, as the TrafficLight component is encapsulated into QMainWindow (because you added it with the form designer)
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
You may have more info about Integrating a Custom Widget into Qt Designer here.
-
To promote is the correct keyword, I think.
But it still didn't refresh, when a light changes. -
-
@Pablo-J-Rogina
Thanks a lot!!
This works fantastic! :-)