qt-interest@trolltech.com
[Top] [All Lists]

QThread questions

Subject: QThread questions
From: Ezequiel Solares
Date: Tue, 1 Nov 2005 00:11:55 +0000 UTC
Hi

I'm doing a non-gui thread. But i don't know how to do one thing.
I create the Thread from the main thread (gui thread). So, the class 
QThSerial:public QThread is owned by the main thread.
I want to have a QT main loop inside the QThSerial so i can use 
QIODevice, QTimer, signal/slot inside the thread.
In the QThSerial i have a public slot. But its owned by the main thread, 
so, if i connect a SIGNAL created in the thread, its tell me this :

ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to 
objects owned by a different thread. Current thread 12fe64. Receiver '' 
(of type 'QThSerial') was created in thread 8b72b8", file kernel
\qcoreapplication.cpp, line 275

The main thread (gui) is 8b72b8
the child thread (non-gui) created with QThread is 12fe64

Thanks in advance

Ramiro

this is the code.

-------------------
main.cpp
------------------

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    myDebug myDeb;
        
    QThSerial mySerial; mySerial.start(QThread::HighestPriority);

    qt_das2 w;
    w.show();
    a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    return a.exec();
}

----------------------------
QThread class (header)
---------------------

#ifndef QTHSERIAL_H
#define QTHSERIAL_H

#include <QThread>
#include <QMutex>
#include <QTime>

class QThSerial;

class QThSerial : public QThread
{
    Q_OBJECT

public:
    QThSerial(QObject *parent = 0);
    ~QThSerial();

        int getData();

        static QThSerial *QThSerialApi() { return myQThThreadApp; };

protected:
        void run();

private slots:
        void newPoint();

private:
        QTime t1, t2;
        static QThSerial *myQThThreadApp;
        int c;
        QMutex mutex;
};

#endif // QTHSERIAL_H


------------------
QThread .cpp
------------------


#include <QTimer>

#include "qthserial.h"

QThSerial *QThSerial::myQThThreadApp = 0;

QThSerial::QThSerial(QObject *parent)
    : QThread(parent)
{
        myQThThreadApp = this;
        c = 0;
}

QThSerial::~QThSerial()
{
}

int QThSerial::getData()
{
        int data;
        mutex.lock();
        data = c;
        c = 0;
        mutex.unlock();
        return data;
}

void QThSerial::newPoint()
{
        mutex.lock();
        c++;
        mutex.unlock();
//      qDebug("Tiempo fue de: %d ms", t2.elapsed());
        t2.restart();
}

void QThSerial::run()
{
        t2.start();

        QTimer *timer2 = new QTimer(this);
        connect(timer2, SIGNAL(timeout()), this, SLOT(newPoint()));
        timer2->start(50);

        exec();

/*      t2.start();
        forever {
                t2.restart();
                mutex.lock();
                c++;
                mutex.unlock();
                msleep(50);
                qDebug("Tiempo fue de: %d ms", t2.elapsed());
        }*/
}


--
To unsubscribe - send "unsubscribe" in the body to 
qt-interest-request@xxxxxxxxxxxxx
List archive and information: http://lists.trolltech.com/qt-interest/

<Prev in Thread] Current Thread [Next in Thread>
  • QThread questions, Ezequiel Solares <=