You are not logged in.

1

Thursday, May 10th 2012, 1:21pm

Newbee question

I'm new to Qt and I can't figure out how to refresh widgets after they have been displayed. I read on the Internet that one way to refresh is to call "QCoreApplication::processEvents()", but the following code creates a label that shows "Hello" even after calling "setText" and attempting to refresh.

#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();
}

2

Thursday, May 10th 2012, 3:02pm

You are doing it the wrong way. Totally the wrong way.

I suggest you take a look at some Qt examples before moving along.

Jan

P.S. I think the main problem may be that you still execute code after app.exec()

Edit: And please, use code tags then posting code

3

Thursday, May 10th 2012, 3:15pm

I went through the entire Cannon tutorial and I still cannot figure out how to change the state of widgets. Obviously I wouldn't have posted if I thought I could figure it out for myself.

I take it from your response (although I'm not sure) that the only way to change the state of a widget is through interaction with buttons etc. (via the signal-slot mechanism). However, I have external code from which I want to change the state of widgets, so I thought I could add functions to my widget subclasses that allows me, for example, to set the text of a label after the window has already been displayed.

I've never posted in a programming forum so I have no idea what code tags are.

Junior

Professional

  • "Junior" is male

Posts: 1,613

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

4

Thursday, May 10th 2012, 5:06pm

Try:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <QtGui/QApplication>
#include <QtGui/QLabel>

int main(int argc, char * argv[])
{
    QApplication app(argc, argv);
    QLabel label;
    label.show();
    app.processEvents();

    label.setText( "Hello" );
    sleep( 5 );
    app.processEvents();

    label.setText( "Good-bye" );
    sleep( 5 );
    app.processEvents();

    return app.exec();
} 

5

Thursday, May 10th 2012, 5:39pm


I've never posted in a programming forum so I have no idea what code tags are.



When you join a new board it's polite to read the FAQ/guidlines
QtForum.org Guidelines
:evil:


here's a tip - newbies should never need processEvents.

here's another tip - any code after exec() is useless. The qt app and event loop as exited.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

6

Thursday, May 10th 2012, 8:59pm

Sorry if I maybe was a little unpolite, but as you used code after the exec() command it looked like you hadn't read much in the tutorials.

Jan

Similar threads