Wie richten Sie QSerialport auf einem separaten Thread ein?C++

Programme in C++. Entwicklerforum
Anonymous
 Wie richten Sie QSerialport auf einem separaten Thread ein?

Post by Anonymous »

Befolgen

Code: Select all

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QThread *thread = new QThread;
Worker *worker= new Worker();

worker->moveToThread(thread);

//init connections

thread->start();
}
< /code>

Worker Constructor: < /p>

Worker::Worker(QObject *parent) :
QObject(parent)
{
serial = new QSerialPort(this);  //passing the parent, which should be the current thread
}
< /code>

NO -Kompilierfehler, aber wenn ich es ausführe, wirft es mir das aus: < /p>

QObject: Cannot create children for a parent that is in a different thread.
(Parent is QSerialPort(0x11bd1148), parent's thread is QThread(0x11bd2ef8), current thread is QThread(0x3e47b8)
< /code>

nämlich sagt mir, dass seriell < /code> als übergeordneter Haupt Thread und nicht den Thread, den ich erstellt habe, hat. < /p>

Das gleiche Ergebnis, wenn ich im Konstruktor nicht die Serie instanziiere, sondern im Hauptprozess, der ausgelöst wird, nachdem wir Thread-> start () < /code>: < /p>

genannt habenWorker::Worker(QObject *parent) :
QObject(parent)
{
}

Worker::doWork()
{
if(!serial)
serial= new QSerialPort(this);

//...
}
< /code>

Was fehlt mir?void Worker::send(const QByteArray &data)
{
serial->write(data);
if( serial->waitForBytesWritten(TIMEOUT) )
qDebug()

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post