PDA

View Full Version : [help] How to start/stop updating widget members using QButton?



rayglide
26th April 2011, 06:43
Hi to forum member,

I'm really new to Qt C++ programming and need help on constructing a very basic concepts.

So here is my objectives:
I create a Qt GUI application consists of several widgets among them are simple QPushButton and QTextEdit widget. So the idea is, when I press the button I'll start updating the QTextEdit text using setText(). However, I need to delegate this process to separated threads/process/events (what is the best approach?) that will respond (start/stop) on the push button click signal.

Below is my code that I needed help to complete it:


void MyUI::ViewData(){ //this is a slot that is called upon clicked() signal on the pushButton
QString rawdata = "";

if(ui->pushButtonView->text() == "Start") {
ui->pushButtonView->setText("Stop");

//how to delegate starting update in this part ... ?

} else if (ui->pushButtonView->text() == "Stop") {
ui->pushButtonView->setText("Start");

//how to delegate stoping update in this part ... ?

ui->dataDisplay->setText("");
}
}

Thanks

high_flyer
27th April 2011, 11:13
However, I need to delegate this process to separated threads/process/events
Why is that?

I am not quite sure I follow on what you are trying to do:
you have a buttong with which you want to start a new thread?
And you want to update your QTextEdit from that thread?

If this is the case, you can just start/stop the thread in the slot connected to the button.
And you can connect a signal from the thread to the QTextEdit::setText() slot.

rayglide
28th April 2011, 19:21
Thanks for the reply and sorry I've just read it now.

Yes, thanks now I know that actually slot is running on separate thread from main gui thread. Previously I think that if I go indefinite loop in the slot connected to button signal then the gui will stuck. Anyway I used QTimer for my purpose, so I can change the interval of the update whenever I want.

Thanks.