Я создал более простую версию этой программы (и прошу прощения за то, что не сделал этого). это раньше), чтобы любой мог лучше изучить этот вопрос.
Код: Выделить всё
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include "listeningthread.h"
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void closeEvent(QCloseEvent *event);
private slots:
void on_listeningthread_started();
void on_listeningthread_stopped();
void on_listeningthread_failopen();
void on_listeningthread_notfoundport();
void on_listeningthread_resultListened(QByteArray resultByteArray);
private:
Ui::MainWindow *ui;
ListeningThread m_listeningThread;
};
#endif // MAINWINDOW_H
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&m_listeningThread, &ListeningThread::failOpenPort, this, &MainWindow::on_listeningthread_failopen);
connect(&m_listeningThread, &ListeningThread::listeningActive, this, &MainWindow::on_listeningthread_started);
connect(&m_listeningThread, &ListeningThread::notFoundPort, this, &MainWindow::on_listeningthread_notfoundport);
connect(&m_listeningThread, &ListeningThread::listenStop, this, &MainWindow::on_listeningthread_stopped);
connect(&m_listeningThread, &ListeningThread::resultReceived, this, &MainWindow::on_listeningthread_resultListened);
m_listeningThread.OpenPort();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_listeningthread_started()
{
m_listeningThread.start(); //lo start vero e proprio devo darlo esplicitamente
ui->label_statolistening->setText(tr("Waiting for result..."));
}
void MainWindow::on_listeningthread_stopped()
{
ui->label_statolistening->setText(tr("No listening"));
}
void MainWindow::on_listeningthread_failopen()
{
ui->label_statolistening->setText(tr("Failed opening communication")+"\n"+tr("Problems opening COM port: "));
}
void MainWindow::on_listeningthread_notfoundport()
{
ui->label_statolistening->setText(tr("Failed opening communication")+"\n"+tr("Change communication port or try results download"));
}
void MainWindow::on_listeningthread_resultListened(QByteArray resultByteArray)
{
ui->label_statolistening->setText(resultByteArray);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
m_listeningThread.ClosePort();
while(m_listeningThread.m_bActive);
/*if(m_listeningThread.isRunning())
m_listeningThread.terminate();*/
m_listeningThread.wait(5000);
}
Код: Выделить всё
#ifndef LISTENINGTHREAD_H
#define LISTENINGTHREAD_H
#include
#include
#include "serialconversation.h"
class ListeningThread: public QThread
{
Q_OBJECT
public:
ListeningThread();
SerialConversation m_Conversation;
QSerialPortInfo m_currentPortInfo;
volatile bool m_bActive;
void OpenPort();
void ClosePort();
QByteArray m_GlobalReceiver;
bool bGetResultPacket(int msTimeOut);
signals:
void failOpenPort();
void listeningActive();
void notFoundPort();
void listenStop();
void resultReceived(QByteArray resultByteArray);
// QThread interface
protected:
void run();
};
#endif // LISTENINGTHREAD_H
ListeningThread::ListeningThread()
{
m_bActive = false;
}
void ListeningThread::run()
{
while(m_bActive)
{
if(!bGetResultPacket(1000))
continue;
else
{
int index_dollaro = m_GlobalReceiver.indexOf('$');
if(index_dollaro >= 0)
{
QByteArray arrayRisultato, rimanenza;
int gip=0;
for(gip=0;gip
Подробнее здесь: [url]https://stackoverflow.com/questions/79346235/sigsegv-error-in-qt-multithread-application-when-closing[/url]
Мобильная версия