PDA

View Full Version : what shoud i write in main() ?



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?
}

high_flyer
17th November 2010, 15:26
Am... you should not start with threaded programming in C++ before you know basic C/C++.
Then you could start playing with Qt, but not threaded.
THEN learn what threaded programming is.
And only then you will be able to code Qt threaded apps.

nima
17th November 2010, 17:26
i know , but i should write this program.

i want to know how to use this class in main .

please help me :o

squidge
17th November 2010, 17:42
Look at Qt examples.

Particularly, where is your QApplication variable?

nima
17th November 2010, 17:52
i am reading c++GUI Programing with Qt4 . in chapter 14 i read this .
what should i do?
which variable?

SixDegrees
17th November 2010, 18:05
i am reading c++GUI Programing with Qt4 . in chapter 14 i read this .
what should i do?
which variable?

Did you work through all the example programs in chapters 1 through 13?

Seriously: if you don't understand what the main function does, or how to declare variables, you're skipping over way, way too much. You need to take several steps back and learn the basics first.

ChrisW67
18th November 2010, 01:41
Chapter 14 of the book assumes that you have read the explanation on the very first page of Chapter 1 and can work it out for yourself. Chapter 2 has an example almost perfectly suited. There are numerous other examples in the book before Ch 14, each with a similar pattern just to reinforce the lesson.