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. Qt thread not emiting signal or Main Window not responding to it.
Forum Updated to NodeBB v4.3 + New Features

Qt thread not emiting signal or Main Window not responding to it.

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 572 Views 1 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.
  • M Offline
    M Offline
    Manu_NaFu
    wrote on last edited by
    #1

    Hello, I am new to Qt and I was developing an application where I needed to update an image constantly (or every x seconds), so I created a thread with Qrunnable, as specified in many forum threads, to send a signal to the mainWindow.

    The thread is created and the connection between both the thread and the mainWindow works just fine.

    The problem is that, for any reason, the image is only updated sometimes, and only when I click the button to update it several times whitin a shot period of time.

    I do not know if the problem is the thread, or the problem is the connection or if it is any other problem.

    Thanks in advance for the help, here you can see the code.

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include "parameters.h"
    #include <QThreadPool>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        parameters *param;
        QThreadPool *pool;
        ~MainWindow();
    
    private slots:
        void on_b_new_map_clicked();
    
        void btnaction();
    
        void print_img(char *s);
    
    private:
        Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        connect(ui->b_set_parameters,SIGNAL(clicked()),this,SLOT(btnaction())); // creating connections
        pool = QThreadPool::globalInstance();
    }
    
    MainWindow::~MainWindow()
    {
        pool->waitForDone();
        delete ui;
    }
    
    void MainWindow::on_b_new_map_clicked()
    {
        Work work;
        work.setAutoDelete(false);
        connect(&work, &Work::print_img,this, &MainWindow::print_img);
        work.running = true;
        pool->start(&work);
    
    }
    
    void MainWindow::print_img(char *s)
    {
        cv::Mat mat = cv::imread(s);
    
        if(!mat.empty())
        {
            QSize sz = ui->map->frameSize();
            cv::resize(mat, mat, cv::Size(sz.width(), sz.height()));
    
            const uchar *qImage = (const uchar*)mat.data;
    
            QImage img(qImage, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
    
            ui->map->setPixmap(QPixmap::fromImage(img.rgbSwapped()));
        }
    }
    void MainWindow::btnaction()
    {
        param->show();
        param->setAttribute( Qt::WA_QuitOnClose, false ); //When the main window is closed, the window param will also close.
    }
    

    work.h

    #ifndef WORK_H
    #define WORK_H
    
    #include<QObject>
    #include<QRunnable>
    #include<unistd.h>
    
    
    class Work : public QObject, public QRunnable
    {
        Q_OBJECT
    public:
        bool running;
        void run();
    
    signals:
        void print_img(char *s);
    };
    
    #endif // WORK_H
    

    work.cpp

    #include "work.h"
    
    void Work::run()
    {
        emit print_img("sample.png");
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Manu_NaFu said in Qt thread not emiting signal or Main Window not responding to it.:

      void MainWindow::on_b_new_map_clicked()
      {
      Work work;

      C++ Basics - how long do you think does this object live?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      M 2 Replies Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        @Manu_NaFu said in Qt thread not emiting signal or Main Window not responding to it.:

        void MainWindow::on_b_new_map_clicked()
        {
        Work work;

        C++ Basics - how long do you think does this object live?

        M Offline
        M Offline
        Manu_NaFu
        wrote on last edited by
        #3

        @Christian-Ehrlicher

        That's true, should I create a work class inside the mainWindow class?

        1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          @Manu_NaFu said in Qt thread not emiting signal or Main Window not responding to it.:

          void MainWindow::on_b_new_map_clicked()
          {
          Work work;

          C++ Basics - how long do you think does this object live?

          M Offline
          M Offline
          Manu_NaFu
          wrote on last edited by
          #4

          @Christian-Ehrlicher I changed it as described in my other comment and now it works perfect. Thanks for the advice, I did not count on that.

          Christian EhrlicherC 1 Reply Last reply
          0
          • M Manu_NaFu

            @Christian-Ehrlicher I changed it as described in my other comment and now it works perfect. Thanks for the advice, I did not count on that.

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Manu_NaFu Then please mark this topic as solved, thx.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0

            • Login

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