hosseinyounesi
11th April 2009, 12:58
Hi every body, I'm trying to make a window and show a time in every 1 minutes. This is what what I wrote to do that:
MyThread .h file:
class MyThread : public QThread
{
Q_OBJECT
private:
MainWindow *w;
QTimer *timer;
public:
MyThread( QObject* parent );
void run();
public slots:
void ToDo();
};
main.cpp
MainWindow mw;
MyThread::MyThread( QObject* parent ) : QThread( parent )
{
// nothing to do !!!
}
void MyThread::run()
{
mw.show();
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()), this, SLOT(ToDo()));
timer->setInterval(2009);
timer->start();
return a.exec();
}
void MyThread::ToDo() {
long i=0,h=0,m=0,s=0;
while(i<3)
{
std::string str = "Time: ";
char msg[100];
Converter(gTime,h,m,s);
sprintf(msg,"%02ld:%02ld:%02ld",h,m,s);
str += msg;
w->setText(QString::fromUtf8(str.c_str(),str.size())) ;
sleep(1);
i++;
gTime--;
}
a.closeAllWindows();
}
int main()
{
MyThread mt(0);
mt.start();
mt.wait();
}
It is compiled but this is the error when I try to execute the program:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is MyThread(0xbfbc4cac), parent's thread is QThread(0x80843a0), current thread is MyThread(0xbfbc4cac)
Thanks for your consideration
MyThread .h file:
class MyThread : public QThread
{
Q_OBJECT
private:
MainWindow *w;
QTimer *timer;
public:
MyThread( QObject* parent );
void run();
public slots:
void ToDo();
};
main.cpp
MainWindow mw;
MyThread::MyThread( QObject* parent ) : QThread( parent )
{
// nothing to do !!!
}
void MyThread::run()
{
mw.show();
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()), this, SLOT(ToDo()));
timer->setInterval(2009);
timer->start();
return a.exec();
}
void MyThread::ToDo() {
long i=0,h=0,m=0,s=0;
while(i<3)
{
std::string str = "Time: ";
char msg[100];
Converter(gTime,h,m,s);
sprintf(msg,"%02ld:%02ld:%02ld",h,m,s);
str += msg;
w->setText(QString::fromUtf8(str.c_str(),str.size())) ;
sleep(1);
i++;
gTime--;
}
a.closeAllWindows();
}
int main()
{
MyThread mt(0);
mt.start();
mt.wait();
}
It is compiled but this is the error when I try to execute the program:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is MyThread(0xbfbc4cac), parent's thread is QThread(0x80843a0), current thread is MyThread(0xbfbc4cac)
Thanks for your consideration