[Qt-interest] (no subject)

Neeraj Jhawar navderm at gmail.com
Fri Jan 9 18:03:09 CET 2009


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



More information about the Qt-interest-old mailing list