[Qt-interest] Problems using postEvent ..plz help
Neeraj Jhawar
navderm at gmail.com
Sat Jan 10 17:40:33 CET 2009
I tried to implement the postEvent but this code is just not working.
Can any of you please help with the code...
I am trying to accomplish :
Take a number from the thread via postEvent and display is in the
textedit box every 1ms.
Please help me with the code...its been more than 2 days for this
small idea to work:
**************postevent.h*****************************
#ifndef POSTEVENT_H
#define POSTEVENT_H
#include <qdialog.h>
#include <qthread.h>
#include <qevent.h>
class QLineEdit;
class QPushButton;
class ProdEvent : public QCustomEvent
{
public:
ProdEvent(long s) : QCustomEvent (200), sz(s)
{;}
long numb() const {return sz;}
private:
long sz;
};
class ProdThread : public QThread
{
public:
ProdThread (int);
void stop();
void run();
private:
int printval;
bool stopped;
int finalval;
};
class ThreadForm : public QDialog
{
Q_OBJECT
public:
ThreadForm (QWidget *parent = 0, const char *name = 0);
void customEvent (QCustomEvent *e);
public slots:
void go();
protected:
private:
ProdThread threadA;
QPushButton *threadAButton;
QPushButton *quitButton;
QLineEdit *textedit;
};
#endif
******************************end of postevent.h*******************************
*****************************postevent.cpp**********************************
#include <qlayout.h>
#include <qpushbutton.h>
#include <qlineedit.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <iostream.h>
#include "postevent.h"
static float temp_f;
static char *temp_c;
//the definitions of the thread
ProdThread::ProdThread(int a) //the constructor
{
printval = 0;
stopped = 0;
finalval = a;
}
void ProdThread::stop()
{
stopped = 1;
}
void ProdThread::run()
{
// ProdEvent *pe = new ProdEvent(printval);
stopped = false;
while(!stopped)
{
while (printval < finalval)
{
usleep(1000);
//send the printval via postEvent
ProdEvent *pe = new ProdEvent(printval);
QApplication::postEvent(ThreadForm, pe);
printval++;
}
}
}
//the definitions of the main class
ThreadForm::ThreadForm (QWidget *parent, const char *name) :
QDialog(parent, name)
{
setCaption(tr("Threads"));
threadAButton = new QPushButton(tr("Start Thread"), this);
quitButton = new QPushButton (tr("Quit Program"), this);
quitButton->setDefault(true);
textedit = new QLineEdit(this);
connect(threadAButton, SIGNAL(clicked()), this, SLOT(go()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(textedit);
layout->addWidget(threadAButton);
layout->addWidget(quitButton);
}
void ThreadForm::go()
{
if (ProdThread.running())
{
threadA.stop();
threadAButton->setText(tr("Start Thread"));
}
else
{
threadA.start();
threadBButton->setText(tr("Stop Thread"));
}
}
void ThreadForm:: customEvent (QCustomEvent *e)
{
if (e->type() == 200)
{
ProdEvent* ce = (ProdEvent*)e;
temp_f = ce->numb();
sprintf(temp_c,"%f",temp_f);
textedit->setText(temp_c);
}
}
**************************end of
postevent.cpp************************************************
*************************main.cpp*******************************************
#include <qapplication.h>
#include "postevent.h"
int main (int argc, char *argv[])
{
QApplication app(argc, argv);
ThreadForm *dialog = new ThreadForm;
app.setMainWidget(dialog);
dialog->show();
return app.exec();
}
*********************************end of main.cpp**************************
Please help with the same
On 1/10/09, Neeraj Jhawar <navderm at gmail.com> wrote:
>>
>> Then why are you using a separate thread at all? If your GUI is supposed
>> to
>> block while you fetch the data just do the data-fetching inside the GUI
>> thread. That'll remove all needs for cross-thread-communication.
>>
>> Andreas
>>
>>
> This is because when I try to update the GUI at this rate, all the buttons
> hang. I just cant get them working smoothly enough. I am fetching the data
> at 100 ms period ( or 1/100 ms freq). at this rate when I try to update GUI
> , the buttons like "STOP OPERATION" and "EXIT" and "SAVE TO FILE" are numb
> and I just cant help them.
>
> Is there a way I could just do this without threads... i.e. take the updates
> from a GUI function and the buttons are still smooth enough??
>
>
>
> Neeraj Jhawar
> Senior Undergraduate
> Mechanical Engineering Department
> Punjab Engineering College
> Chandigarh.
> India
>
--
Neeraj Jhawar
Senior Undergraduate
Mechanical Engineering Department
Punjab Engineering College
Chandigarh.
India
More information about the Qt-interest-old
mailing list