[Qt-interest] (no subject)

Scott Aron Bloom Scott.Bloom at sabgroup.com
Fri Jan 9 18:42:40 CET 2009


First, this is new code, turn off QT3 support. 

That said, your code worked fine for me.  Compiled, linked (I had to
create a simple main) and it ran fine.

Scott
> -----Original Message-----
> From: qt-interest-bounces at trolltech.com [mailto:qt-interest-
> bounces at trolltech.com] On Behalf Of Neeraj Jhawar
> Sent: Friday, January 09, 2009 9:03 AM
> To: Qt-interest at trolltech.com
> Subject: [Qt-interest] (no subject)
> 
> The code:
> 
> this program tries to accomplish nothing but to make the signal and
> slot in different threads work:
> 
> this part of code is from file threadstuff.cpp
> 
> #include <qlayout.h>
> #include <qpushbutton.h>
> 
> #include <stdlib.h>
> #include <stdio.h>
> #include <iostream.h>
> 
> #include "threadstuff.h"
> 
> Thread::Thread()
> {
> 	stopped = false;
> }
> 
> void Thread::run()
> {
> 	static int aha = 0;
> 	while(!stopped)
> 	{
> 		cerr << messageStr.ascii();
> 		if (aha >=100)
> 		{
> 			emit wow();
> 			aha = 0;
> 		}
> 		aha++;
> 	}
> 	stopped = false;
> 	cerr << endl;
> }
> 
> void Thread::stop()
> {
> 	stopped = true;
> }
> 
> void Thread::setMessage(const QString &message)
> {
> 	messageStr = message;
> }
> 
> void Thread::startOrStopThreadAinthread()
> {
> }
> 
>
//======================================================================
=
> 
> ThreadForm::ThreadForm (QWidget *parent, const char *name) :
> QDialog(parent, name)
> {
> 	setCaption(tr("Threads"));
> 
> 	threadA.setMessage("A");
> 
> 	threadAButton = new QPushButton("Start A", this);
> 	quitButton = new QPushButton ("Quit", this);
> 	quitButton->setDefault(true);
> 
> 	connect(threadAButton, SIGNAL(clicked()), this,
> SLOT(startOrStopThreadA()));
> 	connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
> 
> 	connect(&threadA, SIGNAL(wow()), this,
> SLOT(startOrStopThreadAII()));
> 	//connect(threadA, SIGNAL(wow()), this,
> SLOT(startOrStopThreadAII()));
> 
> 	QHBoxLayout *layout = new QHBoxLayout(this);
> 	layout->addWidget(threadAButton);
> 	layout->addWidget(quitButton);
> }
> 
> 
> 
> void ThreadForm :: startOrStopThreadA()
> {
> 	if (threadA.running())
> 	{
> 		threadA.stop();
> 		threadAButton->setText(tr("Start A"));
> 	}
> 	else
> 	{
> 		threadA.start();
> 		threadAButton->setText(tr("Stop A"));
> 	}
> }
> 
> 
> void ThreadForm :: startOrStopThreadAII()
> {
> 	static int ahaII = 0;
> 	if (ahaII == 0)
> 	{
> 		ahaII++;
> 		quitButton->setEnabled(true);
> 	}
> 	else if (ahaII == 1)
> 	{
> 		ahaII = 0;
> 		quitButton->setEnabled(false);
> 	}
> }
> 
> 
> void ThreadForm::closeEvent (QCloseEvent *event)
> {
> 	threadA.stop();
> 	threadA.wait();
> 	event->accept();
> }
> 
> 
> =====================================
> 
> this part of the code is from threadstuff.h
> 
> 
> #ifndef THREADSTUFF_H
> 	#define THREADSTUFF_H
> 
> 	#include <qdialog.h>
> 	#include <qthread.h>
> 
> 	class Thread : public QThread
> 	{
> 	public:
> 		Thread();
> 
> 		void startOrStopThreadAinthread();
> 		void setMessage(const QString &message);
> 		void stop();
> 
> 	signals:
> 		void wow();
> 
> 	protected:
> 		void run();
> 
> 	private:
> 		QString messageStr;
> 		volatile bool stopped;
> 	};
> 
> 
> 	class ThreadForm : public QDialog
> 	{
> 		Q_OBJECT
> 	public:
> 		ThreadForm (QWidget *parent = 0, const char *name = 0);
> 
> 	protected:
> 		void closeEvent(QCloseEvent *event);
> 	private slots:
> 		void startOrStopThreadA();
> 		void startOrStopThreadAII();
> 	private:
> 		Thread threadA;
> 		QPushButton *threadAButton;
> 		QPushButton *quitButton;
> 	};
> 
> #endif
> 
> 
> 
> Thank you
> 
> 
> 
> --
> Neeraj Jhawar
> Senior Undergraduate
> Mechanical Engineering Department
> Punjab Engineering College
> Chandigarh.
> India
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest




More information about the Qt-interest-old mailing list