nima
17th November 2010, 15:16
I read qt book an want to write the example but after writing i don't know what should i write in main() function .
#include <QApplication>
#include <QLabel>
#include <QThread>
#include <QDataStream>
#include<QDialog>
#include <QPushButton>
#include <QCloseEvent>
#include <iostream>
class Thread :public QThread
{
Q_OBJECT
public:
Thread();
void setMessage(const QString &message);
void stop();
protected:
void run();
private:
QString messageStr;
volatile bool stopped;
};
Thread::Thread()
{
stopped=false;
}
void Thread::run()
{
while (!stopped)
std::cerr<<qPrintable(messageStr);
stopped=false;
std::cerr<<std::endl;
}
void Thread::stop()
{
stopped=true;
}
class ThreadDialog:public QDialog
{
Q_OBJECT
public:
ThreadDialog(QWidget *parent=0);
protected:
void closeEvent(QCloseEvent *event);
private slots:
void startOrStopThreadA();
void startOrStopThreadB();
private:
Thread threadA;
Thread threadB;
QPushButton *threadAButton;
QPushButton *threadBButton;
QPushButton *quitButton;
};
ThreadDialog::ThreadDialog(QWidget *parent)
:QDialog(parent)
{
threadA.setMessage("A");
threadB.setMessage("B");
threadAButton=new QPushButton(tr("Start A"));
threadBButton=new QPushButton(tr("Start B"));
quitButton=new QPushButton(tr("Quit"));
quitButton->setDefault(true);
connect (threadAButton,SIGNAL(clicked()),this,SLOT(startOr StopThreadA()));
connect (threadBButton,SIGNAL(clicked()),this,SLOT(startOr StopThreadB()));
}
void ThreadDialog::startOrStopThreadA()
{
if(threadA.isRunning()){
threadA.stop();
threadAButton->setText(tr("Start A"));
}else{
threadA.start();
threadAButton->setText(tr("Stop A"));
}
}
void ThreadDialog::startOrStopThreadB()
{
if (threadB.isRunning()){
threadB.stop();
threadBButton->setText(tr("Start B"));
}else{
threadB.start();
threadBButton->setText(tr("Stop B"));
}
}
void ThreadDialog::closeEvent(QCloseEvent *event)
{
threadA.stop();
threadB.stop();
threadA.wait();
threadB.wait();
event->accept();
}
int main(int argc,char *argv[]){
// what shoul i write here for using this program?
}
#include <QApplication>
#include <QLabel>
#include <QThread>
#include <QDataStream>
#include<QDialog>
#include <QPushButton>
#include <QCloseEvent>
#include <iostream>
class Thread :public QThread
{
Q_OBJECT
public:
Thread();
void setMessage(const QString &message);
void stop();
protected:
void run();
private:
QString messageStr;
volatile bool stopped;
};
Thread::Thread()
{
stopped=false;
}
void Thread::run()
{
while (!stopped)
std::cerr<<qPrintable(messageStr);
stopped=false;
std::cerr<<std::endl;
}
void Thread::stop()
{
stopped=true;
}
class ThreadDialog:public QDialog
{
Q_OBJECT
public:
ThreadDialog(QWidget *parent=0);
protected:
void closeEvent(QCloseEvent *event);
private slots:
void startOrStopThreadA();
void startOrStopThreadB();
private:
Thread threadA;
Thread threadB;
QPushButton *threadAButton;
QPushButton *threadBButton;
QPushButton *quitButton;
};
ThreadDialog::ThreadDialog(QWidget *parent)
:QDialog(parent)
{
threadA.setMessage("A");
threadB.setMessage("B");
threadAButton=new QPushButton(tr("Start A"));
threadBButton=new QPushButton(tr("Start B"));
quitButton=new QPushButton(tr("Quit"));
quitButton->setDefault(true);
connect (threadAButton,SIGNAL(clicked()),this,SLOT(startOr StopThreadA()));
connect (threadBButton,SIGNAL(clicked()),this,SLOT(startOr StopThreadB()));
}
void ThreadDialog::startOrStopThreadA()
{
if(threadA.isRunning()){
threadA.stop();
threadAButton->setText(tr("Start A"));
}else{
threadA.start();
threadAButton->setText(tr("Stop A"));
}
}
void ThreadDialog::startOrStopThreadB()
{
if (threadB.isRunning()){
threadB.stop();
threadBButton->setText(tr("Start B"));
}else{
threadB.start();
threadBButton->setText(tr("Stop B"));
}
}
void ThreadDialog::closeEvent(QCloseEvent *event)
{
threadA.stop();
threadB.stop();
threadA.wait();
threadB.wait();
event->accept();
}
int main(int argc,char *argv[]){
// what shoul i write here for using this program?
}