简体   繁体   中英

How to keep background code running without create a thread?

  1. I am trying to build a simple UI with qt creator. Background code keep print a int flag and when user click on button, the flag changed, so the output changed.

UI like this:

在此输入图像描述

output like this: click 《output 1》, then change 0 to 1

在此输入图像描述

  1. How to keep background code running without create a thread in qt creator?

here is the cpp code, I didnt add .h files: all files are available here jump to all code in github

main.cpp:

    #include "MainWindow.h"
    #include <QApplication>
    #include <iostream>
    #include "deal.h"

    int main(int argc, char *argv[])
    {
      std::cout << argc << " " << argv[0] << std::endl;
    //  getchar();
      QApplication a(argc, argv);

      MainWindow w;
      w.show();


    //  getchar();
      pthread_t tid;   //Not want to create a thread to run this
      pthread_create(&tid, NULL, run, NULL); 
      //pthread_exit(&tid);
      a.exec();
      return 0;
    }

MainWindow.cpp

    #include "MainWindow.h"
    #include "ui_MainWindow.h"
    #include "deal.h"

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
      ui->setupUi(this);
      connect(ui->pbOriginSound, SIGNAL(clicked()), this, SLOT(change2_origin_sound()));
      connect(ui->pbVecSound, SIGNAL(clicked()), this, SLOT(change2_vec_sound()));
      connect(ui->pbVecNrSound, SIGNAL(clicked()), this, SLOT(change2_vec_nr_sound()));
    }

    void MainWindow::change2_origin_sound(){
      iFlag = 0;
      printf("%d", iFlag);
    }

    void MainWindow::change2_vec_sound(){
      iFlag = 1;
      printf("%d", iFlag);
    }

    void MainWindow::change2_vec_nr_sound(){
      iFlag = 2;
      printf("%d", iFlag);
    }

    MainWindow::~MainWindow()
    {
      delete ui;
    }

deal.cpp

    #include "deal.h"
    #include <iostream>
    #include <unistd.h>

    int iFlag;

    void *run(void *arg){

      while(1){
    //        sleep(1);
        printf("%d\n", iFlag);
        printf("%d%d\n", iFlag, iFlag);
      }
    }

One common way is to create a slot-method and a QTimer that will call that method every so-many milliseconds, eg

in MainWindow.h:

#include <QTimer>
#include <QMainWindow>

class MainWindow : public QMainWindow 
{
Q_OBJECT

   [...]

private slots:
   void MySlot();

private:
   QTimer myTimer;
};

in MainWindow.cpp:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    [...]

    connect(&myTimer, SIGNAL(timeout()), this, SLOT(MySlot()));
    myTimer.start(100);   // MySlot() will be called every 100mS
}

void MainWindow::MySlot()
{
    printf("%d\n", iFlag);
}

Note that this approach assumes that MySlot() will return quickly; if it doesn't, your GUI will freeze up until it returns, since MySlot() is still running in the GUI thread and is therefore holding off the handling of other GUI events. So be sure not to do anything inside MySlot() that takes a long time, unless you are willing to live with an not-very-responsive GUI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM