ich benutze Qt 4.6.0. Ich habe eine Anwendung, in der aus dem Gui nicht-GUI-Threads gestartet werden. Ich möchte aber, dass immer nur ein Thread gleichzeitig läuft.
Code: Alles auswählen
#include <QtGui>
#include "myqtapp.h"
myQtApp::myQtApp(QWidget *parent)
{
setupUi(this); // this sets up GUI
// signals/slots mechanism in action
connect( pushButton_lmain, SIGNAL( clicked() ), this, SLOT( launch_in_main() ) );
connect( pushButton_lnew, SIGNAL( clicked() ), this, SLOT( launch_in_new() ) );
}
void myQtApp::launch_in_main()
{
qDebug() << "Executing in main thread, GUI is now blocked";
for(int i=0;i<10;i++)
{
qDebug() << "Time to wait: " << 10-i;
int t=1;
// some OS specific stuff
// mingw (3.4.2) sleep on windows is called _sleep and uses microseconds
#ifdef Q_OS_WIN32
t = t * 1000;
_sleep(t);
#else
sleep(t);
#endif
}
qDebug() << "Now you can operate with GUI";
}
void myQtApp::launch_in_new()
{
// create new thread (on heap) and start it
thread = new MyThread(this);
thread->start(); // after this, thread's run() method starts
}
Code: Alles auswählen
#include "mythread.h"
#include <QWriteLocker>
MyThread::MyThread(QObject *parent)
: QThread(parent)
{
}
void MyThread::run()
{
//lock.lockForWrite();
QWriteLocker locker(&lock);
qDebug() << "Executing in new independant thread, GUI is NOT blocked";
for(int i=0;i<10;i++)
{
qDebug() << "Time: " << 10-i;
int t=1;
// some OS specific stuff
// mingw (3.4.2) sleep on windows is called _sleep and uses microseconds
#ifdef Q_OS_WIN32
t = t * 1000;
_sleep(t);
#else
sleep(t);
#endif
}
//lock.unlock();
qDebug() << "Execution done";
exec();
}
Code: Alles auswählen
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QtGui>
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread(QObject *parent);
void run(); // this is virtual method, we must implement it in our subclass of QThread
private:
QReadWriteLock lock;
};
#endif
Könnt ihr mir bitte helfen?
Danke