PDA

View Full Version : Thread problem



plopes21
18th June 2012, 12:31
Good morning people,

I'm with a problem threading.

I want to start multiple threads at program start and I am not getting.

In mainwindow.ccp have the following.

MainWindow.cpp

* The alarm;
a.run ();


Class Alarm


class Alarm : public QThread
{
Q_OBJECT
public:
Alarm(QObject* = 0);
void iniciarValoresAlarme();
void run();

signals:
void itsTime();

};
#endif



Alarm.cpp



Alarm::Alarm(QObject* parent) :
QThread(parent)
{}

void Alarm::run(){
exec();
}
void Alarm::iniciarValoresAlarme()
{
QDateTime today = QDateTime::currentDateTime();
year = today.date().year();
month = today.date().month();
day = today.date().day();
// hours = 0;
// minutes = 0;
seconds = 0;
timeToAlarm = 0;


timer = new QTimer(this);

connect(timer, SIGNAL(timeout()), this, SLOT(finalizarAlarm()));
connect(this,SIGNAL(itsTime()),this,SLOT(launchAla rm()));
iniciarAlarme();
start();
qDebug()<<"Depois do start antes do wait";
wait();

}


this code don´t work.

What the problem?

Lesiok
18th June 2012, 12:44
1. Show us full method in MainWindow which creates this threads.
2. What is "this code don't work" ?

plopes21
18th June 2012, 12:48
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Alarm a;
a.run();
}



The main window is not shown.

Lesiok
18th June 2012, 12:54
Lines 6 and 7 should be :
Alarm *a = new Alarm(this);
a->run();Basics of C/C++

amleto
18th June 2012, 13:26
"The main window is not shown."

That is what show() is for.

wysota
18th June 2012, 17:37
QThread::run() does not start a new thread. QThread::start() does.

plopes21
19th June 2012, 13:10
Thanks my friends. Already works.

It took just put
Alarm *a = new Alarm(this);
a->run();

wysota
19th June 2012, 13:34
Still, you have no extra thread here.