PDA

View Full Version : External calls that change the state of widgets



wisdompoet
10th May 2012, 15:42
I'm new to Qt, and I'm trying to figure out how to change the state of widgets (text of labels, contents of comboboxes etc.), other than the signal-slot mechanism. In particular I want to implement the Observer interface and include a "notify" method in my main Qt application class, that changes the state of some widgets according to the subject's state. However, after I execute the "exec()" method, no changes made to widgets appear on the screen (even after executing "QCoreApplication::processEvents()" which I read is supposed to update the state of the widgets). I've used Java Swing a lot, where changes to components are displayed accordingly on the screen.

ChrisW67
11th May 2012, 01:30
:confused: Signals and slots are the observer pattern.

You can change the text on labels using direct calls. As long as your event loop is running the widget will update. Since you say this is not the case then you will need to post a small, compilable example that demonstrates the problem.

wisdompoet
11th May 2012, 08:52
Let me be a bit more specific. I am teaching a class and Qt is too advanced for my students. I therefore want to encapsulate a Qt interface in a class (for various reasons I cannot run Qt from main) and have my students implement the Observer pattern in other classes that call methods (predefined by me) for updating the interface. In other words, I don't necessarily want the signal-slot mechanism since external calls will determine what is displayed.

This is the smallest example of code I can think of that I thought would work:

#include <QApplication>
#include <QLabel>

int main(int argc, char * argv[])
{
QApplication app(argc, argv);
QLabel label("Hello");
label.show();
app.exec();
label.setText("Goodbye");
QCoreApplication::processEvents();
}

ChrisW67
11th May 2012, 11:07
Once you call app.exec() the program is blocked until you close the window that opened with the label. Only then does your setText() runs on the now-hidden label and the program terminates.