Inhalt von TextLabel(QLabel in QDialog) pro Sekunde updaten ?
-
Hallo alle,
ich möchte fragen, wie man den Inhalt von TextLabel(QLabel in QDialog) pro Sekunde updaten kann.
Ich habe ein Test-Programm geschrieben: ein Dialog(form), wo zwei TextLabel und ein PushButton enthalten sind. Wenn man auf den Button klickt, soll der Text in TextLabel schreiben und pro sekunde updaten. Aber er tut anders. Solange das Programm die while-schleife nicht verlässt, setzt er keinen Text im Label, sondern setzt den Text im Label nur ein einziges mal, und zwar bei der letzten Iteration. Ich würde eigentlich erwarten, dass er 10mal - textLabel->setText( "Hallo Label" ) - setzt.
Wie funktioniert überhaupt solch ein Mechamismus bei QT?
Ich arbeite unter SUSE Linux (virtuelle Maschine) und QT3.3.7.
code:
//slot: void textForm::showData( ) { int i=0; while( i<10 ) { i++; cout << i << endl; textLabel1->setText( "hallo Label 1 " ); textLabel2->setText( "hallo Label 2 " ); sleep(1); } }
Vielen Dank im Voraus
Ruyi
-
Du blockierst dein Programm mit dem sleep(1). Während ein slot ausgeführt wird, wird ja die GUI nicht geupdated.
Der richtige Weg wäre:
1. Wenn man auf den Knopf klickt, wird ein QTimer gestartet, der jede Sekunde ein Signal sendet.
2. Dieses Signal verbindest du mit einem Slot, der das QLabel updatet.
3. Am Ende wird der QTimer gestoppt.
-
Danke für den Antwort.
Theoretisch bin ich schon klar. Wie und wo schreibe ich die Code in Programm? Ich benutze Qt-Designer.
Allgemeine fragen, wie man die Objecte, die nicht Widgets sind(hier Z.B. Object von QTimer), create bzw. mit anderen Objekten connect werden können und in welches File? wenn man Qt-Designer benutzt?
vielen Danke im Voraus
RuyiupdateForm.h
#include "form.h" class UpdateForm : public Form { Q_OBJECT public: UpdateForm( QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags fl = 0 ); ~UpdateForm(); QTimer *myTimer; public slots: void showData(); private: };
updateForm.cpp
#include "updateForm.h" #include <qlabel.h> #include <iostream.h> #include <qtimer.h> UpdateForm::UpdateForm( QWidget* parent, const char* name, bool modal, WFlags fl ) : Form( parent, name, modal, fl ) { myTimer = new QTimer(this); } UpdateForm::~UpdateForm( ) { /* NOOP */ } void UpdateForm::showData() { int i=0; while( i<10 ) { i++; cout << i << endl; textLabel1->setText( "hallo Label 1 " ); textLabel2->setText( "hallo Label 2 " ); //sleep(1); } }
main.cpp#include <qapplication.h>
#include "updateForm.h" int main( int argc, char ** argv ) { QApplication a( argc, argv ); UpdateForm w; w.show(); a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) ); return a.exec(); }
form.ui
<!DOCTYPE UI><UI version="3.3" stdsetdef="1"> <class>Form</class> <widget class="QDialog"> <property name="name"> <cstring>Form</cstring> </property> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>460</width> <height>271</height> </rect> </property> <property name="caption"> <string>Form</string> </property> <widget class="QLabel"> <property name="name"> <cstring>textLabel2</cstring> </property> <property name="geometry"> <rect> <x>260</x> <y>70</y> <width>120</width> <height>30</height> </rect> </property> <property name="text"> <string>textLabel2</string> </property> </widget> <widget class="QLabel"> <property name="name"> <cstring>textLabel1</cstring> </property> <property name="geometry"> <rect> <x>60</x> <y>70</y> <width>140</width> <height>40</height> </rect> </property> <property name="text"> <string>textLabel1</string> </property> </widget> <widget class="QPushButton"> <property name="name"> <cstring>pushButton</cstring> </property> <property name="geometry"> <rect> <x>90</x> <y>170</y> <width>151</width> <height>51</height> </rect> </property> <property name="text"> <string>pushButton</string> </property> </widget> </widget> <connections> <connection> <sender>pushButton</sender> <signal>clicked()</signal> <receiver>Form</receiver> <slot>showData()</slot> </connection> </connections> <includes> <include location="local" impldecl="in implementation">form.ui.h</include> </includes> <slots> <slot>showData()</slot> </slots> <pixmapinproject/> <layoutdefaults spacing="6" margin="11"/> </UI>
-
Schau dir mal folgendes für deine Form an:
updateForm.h:
#include <qtimer.h> #include "form.h" class UpdateForm : public Form { Q_OBJECT public: UpdateForm(QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags fl = 0 ); ~UpdateForm(); public slots: void showData(); void timerDone(); private: QTimer timer; int value; bool running; };
updateForm.cpp
#include <qlabel.h> #include "updateForm.h" UpdateForm::UpdateForm(QWidget* parent, const char* name, bool modal, WFlags fl) : Form( parent, name, modal, fl ), timer(this), value(0), running(false) { connect(&timer, SIGNAL(timeout()), this, SLOT(timerDone())); } UpdateForm::~UpdateForm( ) { /* NOOP */ } void UpdateForm::timerDone() { ++value; textLabel1->setText("Timer running"); textLabel2->setText(QString("Value: %1").arg(value)); } void UpdateForm::showData() { if (not running) { running = true; timer.start(1000); textLabel1->setText("Timer started"); textLabel2->setText(QString("Value: %1").arg(value)); } else { running = false; timer.stop(); textLabel1->setText("Timer stopped"); } }