Paralel Thread do not work
-
Hello, I am trying to make a thread app to read serial port and connect through signals to a text browser. I have an object created for these purpose, that in theory, in paralel with the main window, must a debug text in console show, but i dont know what happens, that the main window do not run, like if the the Thread do not work.
I will appreciate any help, thanks in avance
#include "mainwindow.h"
#include <QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();return a.exec();
}
//*************************************************#ifndef HILO_H
#define HILO_H#include<QThread>
class hilo : public QThread{
Q_OBJECT
public:hilo(); ~hilo(); void run();
signals:
void xtring(QString xtr);
};#endif // HILO_H
//*************************************************#include "hilo.h"
#include <iostream>using namespace std;
hilo::hilo()
{}
//**********************************************
hilo::~hilo(){}
//**********************************************
void hilo::run(){
for(;;){
cerr << "show debug\n";
}
// emit xtring("cadena xtring");
}
//**********************************************#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private slots:
void on_MainWindow_customContextMenuRequested(const QPoint &pos);private:
Ui::MainWindow *ui;
};#endif // MAINWINDOW_H
//**************************************************
//MainWindow.cpp#include "mainwindow.h"
#include "ui_mainwindow.h"#include <hilo.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// hilo h();
hilo *pH = new hilo();
pH->run();
}MainWindow::~MainWindow()
{
delete ui;
} -
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // hilo h(); hilo *pH = new hilo(); pH->run(); }
The call to
pH->run()
never returns, therefore your constructor never returns, thus leading to everything frozen. -
use
pH->start();
instead ofpH->run();
run should be declared protected and virtual
change hilo constructor to call the base class
hilo::hilo(QObject * parent) :QThread(parent) { }
also you have a memory leak there if you do not give a parent to ph :
hilo *pH = new hilo(this);
-
Thank you very much for your help.
pH->start(); Did workI don't understand the another variant and why must the pointer this use (I am total newbie), If somebody want explain a little more, will be well received
Greetings and thank you again
hilo::hilo(QObject * parent) :QThread(parent) { } also you have a memory leak there if you do not give a parent to ph : hilo *pH = new hilo(this);//your code here
-
Hi
About the "this" pointerNormally if you do
ClassX * mine= new ClassX;you will need to call
delete mine;to clean up .
In Qt we have a parent/child system. The parents will also delete/clean up all childs it
owns.so when you do
hilo *pH = new hilo(this);You are indirectly saying,
"I transfer ownership of pH to the parent" ( parent = this)So when "this" is deleted it will also delete pH as you gave it as child.