Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Paralel Thread do not work
Forum Updated to NodeBB v4.3 + New Features

Paralel Thread do not work

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 789 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • yczoY Offline
    yczoY Offline
    yczo
    wrote on last edited by A Former User
    #1

    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

    //main.cpp

    #include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
    

    }
    //*************************************************

    //hilo.h

    #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
    //*************************************************

    //hilo.cpp

    #include "hilo.h"
    #include <iostream>

    using namespace std;
    hilo::hilo()
    {

    }
    //**********************************************
    hilo::~hilo(){

    }
    //**********************************************
    void hilo::run(){
    for(;;){
    cerr << "show debug\n";
    }
    // emit xtring("cadena xtring");
    }
    //**********************************************

    //MainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    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;
    }

    1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by
      #2
      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.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        use pH->start(); instead of pH->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);

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2
        • yczoY Offline
          yczoY Offline
          yczo
          wrote on last edited by
          #4

          Thank you very much for your help.
          pH->start(); Did work

          I 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
          
          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            About the "this" pointer

            Normally 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.

            1 Reply Last reply
            1
            • yczoY Offline
              yczoY Offline
              yczo
              wrote on last edited by
              #6

              Thank you very much for your help

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved