la création de nouveaux thread à l'aide de Qthread-Qt5

je suis en train de créer un nouveau thread gpsthread qui doit s'exécuter en arrière-plan, et de stocker la valeur.

class gpsthread: public QThread{
    Q_OBJECT
private:nrega_status_t status2;

public: 
explicit gpsthread(QObject *parent = 0):QThread(parent) {
    //QTimer *t = new QTimer(this);
    //connect(t, SIGNAL(timeout()), this, SLOT(processgps()));
    //t->start(10000);
 }
 void run(){
    qDebug()<<"inside gps thread\n";
    QTimer *t = new QTimer(this);
     connect(t, SIGNAL(timeout()), this, SLOT(processgps()));
     t->start(10000);
    }

public slots:void processgps(){
    int status2;
    status2=gps_management();
}
};

Ma classe principale est quickview.

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

QString file = "qml/main.qml";
QApplication app(argc, argv);
TranslationTest myObj;
QuickView view;
subthread object;
gpsthread obj;
gprsthread gprs;
view.rootContext()->setContextProperty("rootItem", (QObject *)&myObj);

    obj.start();
//from subthread
QObject::connect(&object, SIGNAL(batterytoqml(QVariant,QVariant)),item, SLOT(frombattery(QVariant,QVariant)));
QObject::connect(&gprs, SIGNAL(gprstoqml(QVariant)),item, SLOT(fromgprs(QVariant)));
return app.exec();

}

j'ai essayé ce ainsi

class gpsthread: public QThread{
    Q_OBJECT
private:nrega_status_t status2;

public:QTimer* t; 
explicit gpsthread(QObject *parent = 0):QThread(parent) {
    //QTimer *t = new QTimer(this);
    //connect(t, SIGNAL(timeout()), this, SLOT(processgps()));
    //t->start(10000);
 }
 void run(){
    qDebug()<<"inside gps thread\n";
    t = new QTimer(this);
     connect(t, SIGNAL(timeout()), this, SLOT(processgps()));
     t->start(10000);
exec();    
}

public slots:void processgps(){
    int status2;
    status2=gps_management();
}
};

mais c'est de donner un message d'erreur indiquant que

 QObject: Cannot create children for a parent that is in a different thread

si je créer un objet dans le constructeur, puis il va aussi donner le même message d'erreur parce que l'objet sera créé dans le thread principal.
Comment résoudre ce problème?

OriginalL'auteur geek | 2014-07-16