[Qt-interest] Problems using postEvent ..plz help

Neeraj Jhawar navderm at gmail.com
Sat Jan 10 18:07:31 CET 2009


PS: THE TIMING 1ms NEED NOT BE ACCURATE HERE. ITS JUST A TRIAL

On 1/11/09, Neeraj Jhawar <navderm at gmail.com> wrote:
> 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.
>
> There are 3 files
> 1. main.cpp
> 2.postevent.h
> 3. postevent.cpp
> ****************************THE ERROR MESSAGE******************************
>
> g++ -c -pipe -Wall -W -O2  -DQT_NO_DEBUG -DQT_SHARED
> -DQT_THREAD_SUPPORT -I/usr/local/qt/mkspecs/default -I.
> -I. -I/usr/local/qt/include -o postevent.o postevent.cpp
> In file included from /usr/include/c++/3.2.2/backward/iostream.h:31,
>                  from postevent.cpp:8:
> /usr/include/c++/3.2.2/backward/backward_warning.h:32:2: warning:
> #warning This file includes at least one deprecated or antiquated
> header. Please consider using one of the 32 headers found in section
> 17.4.1.2 of the C++ standard. Examples include substituting the <X>
> header for the <X.h> header for C++ includes, or <sstream> instead of
> the deprecated header <strstream.h>. To disable this warning use
> -Wno-deprecated.
> postevent.cpp: In member function `virtual void ProdThread::run()':
> postevent.cpp:42: parse error before `,' token
> postevent.cpp:41: warning: unused variable `ProdEvent*pe'
> make: *** [postevent.o] Error 1
>
> *************************END OF ERROR MESSAGE*********************
>
> (LINE 41 AND 42 HAVE BEEN SHOWN BY @@@ SIGN)
>
> ******************************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 s() const {return sz;}
> 		
> 	private:
> 		long sz;
> 	};
>
> /********************************************************************************/
> 	
> 	class ProdThread : public QThread
> 	{
> 	public:
> 		ProdThread ();
> 		void stop(bool);
> 		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.H********************************************
> #include <qlayout.h>
> #include <qpushbutton.h>
> #include <qlineedit.h>
>
> #include <stdlib.h>
> #include <stdio.h>
> #include <math.h>
> #include <iostream.h>
>
>
> #include "postevent.h"
>
> float temp_f;
> char *temp_c;
>
>
> //the definitions of the thread
>
> 	ProdThread::ProdThread()			//the constructor
> 	{
> 		printval = 0;
> 		stopped = 0;
> 		finalval = 1000;
> 	}
> 	
> 	void ProdThread::stop(bool a)
> 	{
> 		stopped = a;
> 	}
> 	
> 	void ProdThread::run()
> 	{
> 		//		ProdEvent *pe = new ProdEvent(printval);
> 		stopped = false;
> 		while(!stopped)
> 		{
> 			while (printval < finalval)
> 			{
> 				usleep(1000);
>
> @@@				ProdEvent *pe = new ProdEvent((long)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 (threadA.running())
> 	{
> 		threadA.stop(1);
> 		threadAButton->setText(tr("Start Thread"));
> 	}
> 	else
> 	{
> 		threadA.stop(0);
> 		threadAButton->setText(tr("Stop Thread"));
> 	}
>
> }
>
> void ThreadForm:: customEvent (QCustomEvent *e)
> {
> 	if (e->type() == 200)
> 	{
> 		ProdEvent* ce = (ProdEvent*)e;
> 		temp_f = ce->s();
> 		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
>


-- 
Neeraj Jhawar
Senior Undergraduate
Mechanical Engineering Department
Punjab Engineering College
Chandigarh.
India



More information about the Qt-interest-old mailing list