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. [SOLVED] displaying QSlider value in QDebug() using QThread
Qt 6.11 is out! See what's new in the release blog

[SOLVED] displaying QSlider value in QDebug() using QThread

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.8k 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.
  • B Offline
    B Offline
    bladepopper
    wrote on last edited by
    #1

    I am implementing a simple function where slider value is constantly displayed on label and qDebug(). I already got the label updated using signal/slots, but somehow the qDebug() thread is not working properly. I expected to see the console flooded with the value of the slider. Below is my code:
    SliderThread.h:
    @class HorizontalSliderThread : public QThread {
    Q_OBJECT
    public:
    HorizontalSliderThread(Ui::MainWindow *ui);//in order to call slider value in HorizontalSliderThread class
    ~HorizontalSliderThread();
    public slots:
    void process();
    private:
    };@
    SliderThread.cpp:
    @HorizontalSliderThread::HorizontalSliderThread(Ui::MainWindow *ui){
    ui_global = *ui;
    }
    void HorizontalSliderThread::process(){
    qDebug("Test Thread");
    int value = ui_global.horizontalSlider_windowSize->value();
    QObject::connect(ui_global.horizontalSlider_windowSize,SIGNAL(valueChanged(int)),ui_global.label_SliderWindowSize,SLOT(setNum(int)));//update value to label
    qDebug()<<value; //update value in console

    }@
    mainwindow.h move Ui::MainWindow *ui; from private to public.

    mainwindow.cpp:
    @MainWindow::MainWindow(QWidget parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    QThread
    thread = new QThread;
    HorizontalSliderThread* slider = new HorizontalSliderThread(ui);
    slider->moveToThread(thread);
    connect(thread, SIGNAL(started()), slider, SLOT(process()));
    thread->start();
    }@

    Current Output: qDebug() displays the value of slider once, label is updated constantly.
    Expected Output: qDebug() displays the value of slider continuously, label is updated constantly.

    Since label is updated when the slider is moved, then the signal/slot for this function is working, which means my thread should be working. Don't know what I'm doing wrong.

    QThread implementation is reference from: http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

    I am fairly new to this, especially QThread, so if there is a better way to implement this function, please let me know!

    Thanks a lot.

    SOLUTION: change @connect(thread, SIGNAL(started()), slider, SLOT(process()));@
    to @connect(ui->horizontalSlider_windowSize,SIGNAL(valueChanged(int)), slider, SLOT(process()));@

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      Hi, your QThread make non-sense in your code.

      @
      MainWindow::MainWindow(QWidget parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
      {
      ui->setupUi(this);
      QThread
      thread = new QThread;
      HorizontalSliderThread* slider = new HorizontalSliderThread(ui);
      connect(thread, SIGNAL(started()), slider, SLOT(process()));
      thread->start();
      }
      @

      equals

      @
      MainWindow::MainWindow(QWidget parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
      {
      ui->setupUi(this);
      HorizontalSliderThread
      slider = new HorizontalSliderThread(ui);
      slider.process();
      }
      @

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bladepopper
        wrote on last edited by
        #3

        Hi, I added slider->moveToThread(thread); in the mainwindow.cpp. Hope it clears things up.

        Thanks.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dbzhang800
          wrote on last edited by
          #4

          Hi, please note that, QWidget and all of its subclass can only be used in main thread.

          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