QtCreator C++ create thread in main window
I'm building up an application, which receives data over the serial interface. So i implemented a class for the serial handling, which can successfully receive and send data. Now I try to move the data to the UI, to give it out to a console, but I need a thread for that and it seems more difficult than I expected.
So somehow I need to define a thread and start it at the beginning of the UserInterface creation and this thread should then poll a function for new data. I researched about creating a thread and connect it to a callback function, but it is always related to create a class, that inherits from QThread, which I cannot do for the Main UI.
How should I define a thread inside the Main UI, which I can use then to poll a function?
Edit: As recommended, a thread is not necessary here, but I don't know how to call a function inside a class without an object. In the mainWindow class, where all the UI stuff like labels and buttons sits, I created an object for serial communication. Inside this object, an interrupt is called, when new data is received. So I can for example queue this data inside this serial object, but still i need somehow to forward them.
Edit2: A first method that actually works was to implement a timer, which periodically calls an update function. But since the serial rx is interrupt driven, there must be a way for callback, such that I don't need to poll it.
qt qthread
add a comment |
I'm building up an application, which receives data over the serial interface. So i implemented a class for the serial handling, which can successfully receive and send data. Now I try to move the data to the UI, to give it out to a console, but I need a thread for that and it seems more difficult than I expected.
So somehow I need to define a thread and start it at the beginning of the UserInterface creation and this thread should then poll a function for new data. I researched about creating a thread and connect it to a callback function, but it is always related to create a class, that inherits from QThread, which I cannot do for the Main UI.
How should I define a thread inside the Main UI, which I can use then to poll a function?
Edit: As recommended, a thread is not necessary here, but I don't know how to call a function inside a class without an object. In the mainWindow class, where all the UI stuff like labels and buttons sits, I created an object for serial communication. Inside this object, an interrupt is called, when new data is received. So I can for example queue this data inside this serial object, but still i need somehow to forward them.
Edit2: A first method that actually works was to implement a timer, which periodically calls an update function. But since the serial rx is interrupt driven, there must be a way for callback, such that I don't need to poll it.
qt qthread
Having a thread is a good idea, but are you sure you need it? It's possible you can let this slide without a thread. You can read packets then display them with a timeout. Take a look here for QThread. There are examples there how to use a thread. Btw, there's nothing such as "thread in the main UI". You create a thread and you communicate with it in various ways. With Qt you'll initiate something like a pipeline.
– The Quantum Physicist
Nov 14 '18 at 14:24
1
Based on your brief description, probably you don't need a thread, and you don't even need to poll your serial communication class. The Qt way to do this to emit a signal from your serial communication class, which will be handled in a slot in your main window class.
– zgyarmati
Nov 14 '18 at 14:33
Hmm ok. At the moment I can receive data interrupt driven, but then I don't know how to forward the data to the mainWindow for plotting etc. I would need an object of the mainWindow Class, to forward my data to, right? I'm not very practiced in C++, normally I go with C for embedded systems.
– HansPeterLoft
Nov 14 '18 at 14:55
add a comment |
I'm building up an application, which receives data over the serial interface. So i implemented a class for the serial handling, which can successfully receive and send data. Now I try to move the data to the UI, to give it out to a console, but I need a thread for that and it seems more difficult than I expected.
So somehow I need to define a thread and start it at the beginning of the UserInterface creation and this thread should then poll a function for new data. I researched about creating a thread and connect it to a callback function, but it is always related to create a class, that inherits from QThread, which I cannot do for the Main UI.
How should I define a thread inside the Main UI, which I can use then to poll a function?
Edit: As recommended, a thread is not necessary here, but I don't know how to call a function inside a class without an object. In the mainWindow class, where all the UI stuff like labels and buttons sits, I created an object for serial communication. Inside this object, an interrupt is called, when new data is received. So I can for example queue this data inside this serial object, but still i need somehow to forward them.
Edit2: A first method that actually works was to implement a timer, which periodically calls an update function. But since the serial rx is interrupt driven, there must be a way for callback, such that I don't need to poll it.
qt qthread
I'm building up an application, which receives data over the serial interface. So i implemented a class for the serial handling, which can successfully receive and send data. Now I try to move the data to the UI, to give it out to a console, but I need a thread for that and it seems more difficult than I expected.
So somehow I need to define a thread and start it at the beginning of the UserInterface creation and this thread should then poll a function for new data. I researched about creating a thread and connect it to a callback function, but it is always related to create a class, that inherits from QThread, which I cannot do for the Main UI.
How should I define a thread inside the Main UI, which I can use then to poll a function?
Edit: As recommended, a thread is not necessary here, but I don't know how to call a function inside a class without an object. In the mainWindow class, where all the UI stuff like labels and buttons sits, I created an object for serial communication. Inside this object, an interrupt is called, when new data is received. So I can for example queue this data inside this serial object, but still i need somehow to forward them.
Edit2: A first method that actually works was to implement a timer, which periodically calls an update function. But since the serial rx is interrupt driven, there must be a way for callback, such that I don't need to poll it.
qt qthread
qt qthread
edited Nov 14 '18 at 15:17
HansPeterLoft
asked Nov 14 '18 at 14:16
HansPeterLoftHansPeterLoft
1101112
1101112
Having a thread is a good idea, but are you sure you need it? It's possible you can let this slide without a thread. You can read packets then display them with a timeout. Take a look here for QThread. There are examples there how to use a thread. Btw, there's nothing such as "thread in the main UI". You create a thread and you communicate with it in various ways. With Qt you'll initiate something like a pipeline.
– The Quantum Physicist
Nov 14 '18 at 14:24
1
Based on your brief description, probably you don't need a thread, and you don't even need to poll your serial communication class. The Qt way to do this to emit a signal from your serial communication class, which will be handled in a slot in your main window class.
– zgyarmati
Nov 14 '18 at 14:33
Hmm ok. At the moment I can receive data interrupt driven, but then I don't know how to forward the data to the mainWindow for plotting etc. I would need an object of the mainWindow Class, to forward my data to, right? I'm not very practiced in C++, normally I go with C for embedded systems.
– HansPeterLoft
Nov 14 '18 at 14:55
add a comment |
Having a thread is a good idea, but are you sure you need it? It's possible you can let this slide without a thread. You can read packets then display them with a timeout. Take a look here for QThread. There are examples there how to use a thread. Btw, there's nothing such as "thread in the main UI". You create a thread and you communicate with it in various ways. With Qt you'll initiate something like a pipeline.
– The Quantum Physicist
Nov 14 '18 at 14:24
1
Based on your brief description, probably you don't need a thread, and you don't even need to poll your serial communication class. The Qt way to do this to emit a signal from your serial communication class, which will be handled in a slot in your main window class.
– zgyarmati
Nov 14 '18 at 14:33
Hmm ok. At the moment I can receive data interrupt driven, but then I don't know how to forward the data to the mainWindow for plotting etc. I would need an object of the mainWindow Class, to forward my data to, right? I'm not very practiced in C++, normally I go with C for embedded systems.
– HansPeterLoft
Nov 14 '18 at 14:55
Having a thread is a good idea, but are you sure you need it? It's possible you can let this slide without a thread. You can read packets then display them with a timeout. Take a look here for QThread. There are examples there how to use a thread. Btw, there's nothing such as "thread in the main UI". You create a thread and you communicate with it in various ways. With Qt you'll initiate something like a pipeline.
– The Quantum Physicist
Nov 14 '18 at 14:24
Having a thread is a good idea, but are you sure you need it? It's possible you can let this slide without a thread. You can read packets then display them with a timeout. Take a look here for QThread. There are examples there how to use a thread. Btw, there's nothing such as "thread in the main UI". You create a thread and you communicate with it in various ways. With Qt you'll initiate something like a pipeline.
– The Quantum Physicist
Nov 14 '18 at 14:24
1
1
Based on your brief description, probably you don't need a thread, and you don't even need to poll your serial communication class. The Qt way to do this to emit a signal from your serial communication class, which will be handled in a slot in your main window class.
– zgyarmati
Nov 14 '18 at 14:33
Based on your brief description, probably you don't need a thread, and you don't even need to poll your serial communication class. The Qt way to do this to emit a signal from your serial communication class, which will be handled in a slot in your main window class.
– zgyarmati
Nov 14 '18 at 14:33
Hmm ok. At the moment I can receive data interrupt driven, but then I don't know how to forward the data to the mainWindow for plotting etc. I would need an object of the mainWindow Class, to forward my data to, right? I'm not very practiced in C++, normally I go with C for embedded systems.
– HansPeterLoft
Nov 14 '18 at 14:55
Hmm ok. At the moment I can receive data interrupt driven, but then I don't know how to forward the data to the mainWindow for plotting etc. I would need an object of the mainWindow Class, to forward my data to, right? I'm not very practiced in C++, normally I go with C for embedded systems.
– HansPeterLoft
Nov 14 '18 at 14:55
add a comment |
1 Answer
1
active
oldest
votes
As discussed in the comments, in this use-case it's preferable to not use threading, but exploit the Qt event loop and signal-slot mechanism. Here is the skeleton of the MainWindow and the SerialReciver classes, and how they are wired together in main.cpp. For simplicity, the SerialReceiver class just emits the signal every second with the current time, which will be appended to the editfield's content in the main window.
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPlainTextEdit>
class MainWindow : public QMainWindow
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void onSerialMessage(const QString &msg);
private:
QPlainTextEdit mTextField;
;
mainwindow.cpp:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
mTextField.setReadOnly(true);
setCentralWidget(&mTextField);
MainWindow::~MainWindow()
void
MainWindow::onSerialMessage(const QString &msg)
mTextField.appendPlainText(msg);
#endif // MAINWINDOW_H
serialreceiver.h:
#ifndef SERIALRECEIVER_H
#define SERIALRECEIVER_H
#include <QObject>
#include <QTimer>
class SerialReceiver : public QObject
Q_OBJECT
public:
explicit SerialReceiver(QObject *parent = nullptr);
signals:
void newMsg(const QString &msg);
public slots:
void onSerialReceived();
private:
QTimer mTimer;
;
#endif // SERIALRECEIVER_H
serialreceiver.cpp:
#include "serialreceiver.h"
#include <QDateTime>
SerialReceiver::SerialReceiver(QObject *parent) : QObject(parent)
mTimer.setInterval(1000);
mTimer.setSingleShot(false);
connect(&mTimer, &QTimer::timeout,this,&SerialReceiver::onSerialReceived);
mTimer.start();
void
SerialReceiver::onSerialReceived()
QDateTime now = QDateTime::currentDateTime();
emit newMsg(now.toString());
and main.cpp:
#include "mainwindow.h"
#include "serialreceiver.h"
#include <QApplication>
int main(int argc, char *argv)
QApplication a(argc, argv);
SerialReceiver receiver;
MainWindow w;
QObject::connect(&receiver, &SerialReceiver::newMsg,
&w,&MainWindow::onSerialMessage);
w.show();
return a.exec();
Thank you, exactly what I need.
– HansPeterLoft
Nov 14 '18 at 16:33
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302297%2fqtcreator-c-create-thread-in-main-window%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As discussed in the comments, in this use-case it's preferable to not use threading, but exploit the Qt event loop and signal-slot mechanism. Here is the skeleton of the MainWindow and the SerialReciver classes, and how they are wired together in main.cpp. For simplicity, the SerialReceiver class just emits the signal every second with the current time, which will be appended to the editfield's content in the main window.
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPlainTextEdit>
class MainWindow : public QMainWindow
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void onSerialMessage(const QString &msg);
private:
QPlainTextEdit mTextField;
;
mainwindow.cpp:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
mTextField.setReadOnly(true);
setCentralWidget(&mTextField);
MainWindow::~MainWindow()
void
MainWindow::onSerialMessage(const QString &msg)
mTextField.appendPlainText(msg);
#endif // MAINWINDOW_H
serialreceiver.h:
#ifndef SERIALRECEIVER_H
#define SERIALRECEIVER_H
#include <QObject>
#include <QTimer>
class SerialReceiver : public QObject
Q_OBJECT
public:
explicit SerialReceiver(QObject *parent = nullptr);
signals:
void newMsg(const QString &msg);
public slots:
void onSerialReceived();
private:
QTimer mTimer;
;
#endif // SERIALRECEIVER_H
serialreceiver.cpp:
#include "serialreceiver.h"
#include <QDateTime>
SerialReceiver::SerialReceiver(QObject *parent) : QObject(parent)
mTimer.setInterval(1000);
mTimer.setSingleShot(false);
connect(&mTimer, &QTimer::timeout,this,&SerialReceiver::onSerialReceived);
mTimer.start();
void
SerialReceiver::onSerialReceived()
QDateTime now = QDateTime::currentDateTime();
emit newMsg(now.toString());
and main.cpp:
#include "mainwindow.h"
#include "serialreceiver.h"
#include <QApplication>
int main(int argc, char *argv)
QApplication a(argc, argv);
SerialReceiver receiver;
MainWindow w;
QObject::connect(&receiver, &SerialReceiver::newMsg,
&w,&MainWindow::onSerialMessage);
w.show();
return a.exec();
Thank you, exactly what I need.
– HansPeterLoft
Nov 14 '18 at 16:33
add a comment |
As discussed in the comments, in this use-case it's preferable to not use threading, but exploit the Qt event loop and signal-slot mechanism. Here is the skeleton of the MainWindow and the SerialReciver classes, and how they are wired together in main.cpp. For simplicity, the SerialReceiver class just emits the signal every second with the current time, which will be appended to the editfield's content in the main window.
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPlainTextEdit>
class MainWindow : public QMainWindow
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void onSerialMessage(const QString &msg);
private:
QPlainTextEdit mTextField;
;
mainwindow.cpp:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
mTextField.setReadOnly(true);
setCentralWidget(&mTextField);
MainWindow::~MainWindow()
void
MainWindow::onSerialMessage(const QString &msg)
mTextField.appendPlainText(msg);
#endif // MAINWINDOW_H
serialreceiver.h:
#ifndef SERIALRECEIVER_H
#define SERIALRECEIVER_H
#include <QObject>
#include <QTimer>
class SerialReceiver : public QObject
Q_OBJECT
public:
explicit SerialReceiver(QObject *parent = nullptr);
signals:
void newMsg(const QString &msg);
public slots:
void onSerialReceived();
private:
QTimer mTimer;
;
#endif // SERIALRECEIVER_H
serialreceiver.cpp:
#include "serialreceiver.h"
#include <QDateTime>
SerialReceiver::SerialReceiver(QObject *parent) : QObject(parent)
mTimer.setInterval(1000);
mTimer.setSingleShot(false);
connect(&mTimer, &QTimer::timeout,this,&SerialReceiver::onSerialReceived);
mTimer.start();
void
SerialReceiver::onSerialReceived()
QDateTime now = QDateTime::currentDateTime();
emit newMsg(now.toString());
and main.cpp:
#include "mainwindow.h"
#include "serialreceiver.h"
#include <QApplication>
int main(int argc, char *argv)
QApplication a(argc, argv);
SerialReceiver receiver;
MainWindow w;
QObject::connect(&receiver, &SerialReceiver::newMsg,
&w,&MainWindow::onSerialMessage);
w.show();
return a.exec();
Thank you, exactly what I need.
– HansPeterLoft
Nov 14 '18 at 16:33
add a comment |
As discussed in the comments, in this use-case it's preferable to not use threading, but exploit the Qt event loop and signal-slot mechanism. Here is the skeleton of the MainWindow and the SerialReciver classes, and how they are wired together in main.cpp. For simplicity, the SerialReceiver class just emits the signal every second with the current time, which will be appended to the editfield's content in the main window.
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPlainTextEdit>
class MainWindow : public QMainWindow
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void onSerialMessage(const QString &msg);
private:
QPlainTextEdit mTextField;
;
mainwindow.cpp:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
mTextField.setReadOnly(true);
setCentralWidget(&mTextField);
MainWindow::~MainWindow()
void
MainWindow::onSerialMessage(const QString &msg)
mTextField.appendPlainText(msg);
#endif // MAINWINDOW_H
serialreceiver.h:
#ifndef SERIALRECEIVER_H
#define SERIALRECEIVER_H
#include <QObject>
#include <QTimer>
class SerialReceiver : public QObject
Q_OBJECT
public:
explicit SerialReceiver(QObject *parent = nullptr);
signals:
void newMsg(const QString &msg);
public slots:
void onSerialReceived();
private:
QTimer mTimer;
;
#endif // SERIALRECEIVER_H
serialreceiver.cpp:
#include "serialreceiver.h"
#include <QDateTime>
SerialReceiver::SerialReceiver(QObject *parent) : QObject(parent)
mTimer.setInterval(1000);
mTimer.setSingleShot(false);
connect(&mTimer, &QTimer::timeout,this,&SerialReceiver::onSerialReceived);
mTimer.start();
void
SerialReceiver::onSerialReceived()
QDateTime now = QDateTime::currentDateTime();
emit newMsg(now.toString());
and main.cpp:
#include "mainwindow.h"
#include "serialreceiver.h"
#include <QApplication>
int main(int argc, char *argv)
QApplication a(argc, argv);
SerialReceiver receiver;
MainWindow w;
QObject::connect(&receiver, &SerialReceiver::newMsg,
&w,&MainWindow::onSerialMessage);
w.show();
return a.exec();
As discussed in the comments, in this use-case it's preferable to not use threading, but exploit the Qt event loop and signal-slot mechanism. Here is the skeleton of the MainWindow and the SerialReciver classes, and how they are wired together in main.cpp. For simplicity, the SerialReceiver class just emits the signal every second with the current time, which will be appended to the editfield's content in the main window.
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPlainTextEdit>
class MainWindow : public QMainWindow
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void onSerialMessage(const QString &msg);
private:
QPlainTextEdit mTextField;
;
mainwindow.cpp:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
mTextField.setReadOnly(true);
setCentralWidget(&mTextField);
MainWindow::~MainWindow()
void
MainWindow::onSerialMessage(const QString &msg)
mTextField.appendPlainText(msg);
#endif // MAINWINDOW_H
serialreceiver.h:
#ifndef SERIALRECEIVER_H
#define SERIALRECEIVER_H
#include <QObject>
#include <QTimer>
class SerialReceiver : public QObject
Q_OBJECT
public:
explicit SerialReceiver(QObject *parent = nullptr);
signals:
void newMsg(const QString &msg);
public slots:
void onSerialReceived();
private:
QTimer mTimer;
;
#endif // SERIALRECEIVER_H
serialreceiver.cpp:
#include "serialreceiver.h"
#include <QDateTime>
SerialReceiver::SerialReceiver(QObject *parent) : QObject(parent)
mTimer.setInterval(1000);
mTimer.setSingleShot(false);
connect(&mTimer, &QTimer::timeout,this,&SerialReceiver::onSerialReceived);
mTimer.start();
void
SerialReceiver::onSerialReceived()
QDateTime now = QDateTime::currentDateTime();
emit newMsg(now.toString());
and main.cpp:
#include "mainwindow.h"
#include "serialreceiver.h"
#include <QApplication>
int main(int argc, char *argv)
QApplication a(argc, argv);
SerialReceiver receiver;
MainWindow w;
QObject::connect(&receiver, &SerialReceiver::newMsg,
&w,&MainWindow::onSerialMessage);
w.show();
return a.exec();
answered Nov 14 '18 at 16:13
zgyarmatizgyarmati
30729
30729
Thank you, exactly what I need.
– HansPeterLoft
Nov 14 '18 at 16:33
add a comment |
Thank you, exactly what I need.
– HansPeterLoft
Nov 14 '18 at 16:33
Thank you, exactly what I need.
– HansPeterLoft
Nov 14 '18 at 16:33
Thank you, exactly what I need.
– HansPeterLoft
Nov 14 '18 at 16:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302297%2fqtcreator-c-create-thread-in-main-window%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Having a thread is a good idea, but are you sure you need it? It's possible you can let this slide without a thread. You can read packets then display them with a timeout. Take a look here for QThread. There are examples there how to use a thread. Btw, there's nothing such as "thread in the main UI". You create a thread and you communicate with it in various ways. With Qt you'll initiate something like a pipeline.
– The Quantum Physicist
Nov 14 '18 at 14:24
1
Based on your brief description, probably you don't need a thread, and you don't even need to poll your serial communication class. The Qt way to do this to emit a signal from your serial communication class, which will be handled in a slot in your main window class.
– zgyarmati
Nov 14 '18 at 14:33
Hmm ok. At the moment I can receive data interrupt driven, but then I don't know how to forward the data to the mainWindow for plotting etc. I would need an object of the mainWindow Class, to forward my data to, right? I'm not very practiced in C++, normally I go with C for embedded systems.
– HansPeterLoft
Nov 14 '18 at 14:55