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. I want to know how to assign a value to a dial
Qt 6.11 is out! See what's new in the release blog

I want to know how to assign a value to a dial

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 4.8k Views
  • 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.
  • S Offline
    S Offline
    segtteee
    wrote on last edited by segtteee
    #1

    I am receiving the value from the equipment while doing serial communication. like
    QByteArray device_data(mSerialport->readAll());
    ui->data_show->setText(device_data);
    i want recive data at Qdial , but ui->dial-> " ??" What commands should be entered in ?"?
    (data_show is qlabel)

    jsulmJ 1 Reply Last reply
    0
    • S segtteee

      I am receiving the value from the equipment while doing serial communication. like
      QByteArray device_data(mSerialport->readAll());
      ui->data_show->setText(device_data);
      i want recive data at Qdial , but ui->dial-> " ??" What commands should be entered in ?"?
      (data_show is qlabel)

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @segtteee In your other thread I already said what you should use...
      I guess the value is an int?

      ui->dial->setValue(device_data.toInt());
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      1
      • jsulmJ jsulm

        @segtteee In your other thread I already said what you should use...
        I guess the value is an int?

        ui->dial->setValue(device_data.toInt());
        
        S Offline
        S Offline
        segtteee
        wrote on last edited by
        #3

        @jsulm
        I wrote it as you told me, but the dial does not move Do I need any other settings?

        jsulmJ 1 Reply Last reply
        0
        • S segtteee

          @jsulm
          I wrote it as you told me, but the dial does not move Do I need any other settings?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @segtteee Are you sure this code was executed? What was the value of device_data?
          You can check the value like this:

          qDebug() << device_data;
          ui->dial->setValue(device_data.toInt());
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply
          3
          • jsulmJ jsulm

            @segtteee Are you sure this code was executed? What was the value of device_data?
            You can check the value like this:

            qDebug() << device_data;
            ui->dial->setValue(device_data.toInt());
            
            S Offline
            S Offline
            segtteee
            wrote on last edited by
            #5

            @jsulm
            These values ​​appear consecutively in application output.
            below is my code.

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
                mSerialport = new QSerialPort (this);
                connect(mSerialport,SIGNAL(readyRead()),this,SLOT(read_device_data()));
            
                connect(ui->radioButton_on,SIGNAL(clicked(bool)),this,SLOT(on_radioButton_on_clicked()));
            
                connect(ui->radioButton_off,SIGNAL(clicked(bool)),this,SLOT(on_radioButton_off_clicked()));
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            
            void MainWindow::on_pushButton_connect_clicked()
            {
                mSerialport->setPortName(ui->comboBox_port->currentText());
            
                if (ui->comboBox_baudrate->currentText() == "9600 bps")
                    mSerialport->setBaudRate(QSerialPort::Baud9600);
                else if (ui->comboBox_baudrate->currentText() == "19200 bps")
                    mSerialport->setBaudRate(QSerialPort::Baud19200);
                else if (ui->comboBox_baudrate->currentText() == "38400 bps")
                    mSerialport->setBaudRate(QSerialPort::Baud38400);
                else if (ui->comboBox_baudrate->currentText() == "57600 bps")
                    mSerialport->setBaudRate(QSerialPort::Baud57600);
                else if (ui->comboBox_baudrate->currentText() == "115200 bps")
                    mSerialport->setBaudRate(QSerialPort::Baud115200);
            
                mSerialport->setDataBits(QSerialPort::Data8);
                mSerialport->setStopBits(QSerialPort::OneStop);
            
                if (mSerialport->open(QIODevice::ReadWrite))
                {
                    QMessageBox::information(this,tr("connect"),"Serial communication start");
                }
                else
                {
                    QMessageBox::critical(this,tr("error"),mSerialport->errorString());
                }
            }
            
            void MainWindow::on_pushButton_disconnect_clicked()
            {
                QMessageBox::information(this,tr("disconnect"),"Serial communication end");
                mSerialport->close();
            }
            
            void MainWindow::read_device_data()
            {
                QByteArray device_data(mSerialport->readAll());
                ui->data_show->setText(device_data);
            
                ui->dial->setRange(0,3.5);
                qDebug() << device_data;
                ui->dial->setValue(device_data.toInt());
            }
            
            void MainWindow::on_radioButton_on_clicked()
            {
                qDebug() << "written->" << mSerialport->write("mon 1\n");
            }
            
            void MainWindow::on_radioButton_off_clicked()
            {
                qDebug() << "written->" << mSerialport->write("mon 0\n");
            }
            
            
            
            jsulmJ 1 Reply Last reply
            0
            • S segtteee

              @jsulm
              These values ​​appear consecutively in application output.
              below is my code.

              #include "mainwindow.h"
              #include "ui_mainwindow.h"
              
              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
                  mSerialport = new QSerialPort (this);
                  connect(mSerialport,SIGNAL(readyRead()),this,SLOT(read_device_data()));
              
                  connect(ui->radioButton_on,SIGNAL(clicked(bool)),this,SLOT(on_radioButton_on_clicked()));
              
                  connect(ui->radioButton_off,SIGNAL(clicked(bool)),this,SLOT(on_radioButton_off_clicked()));
              }
              
              MainWindow::~MainWindow()
              {
                  delete ui;
              }
              
              void MainWindow::on_pushButton_connect_clicked()
              {
                  mSerialport->setPortName(ui->comboBox_port->currentText());
              
                  if (ui->comboBox_baudrate->currentText() == "9600 bps")
                      mSerialport->setBaudRate(QSerialPort::Baud9600);
                  else if (ui->comboBox_baudrate->currentText() == "19200 bps")
                      mSerialport->setBaudRate(QSerialPort::Baud19200);
                  else if (ui->comboBox_baudrate->currentText() == "38400 bps")
                      mSerialport->setBaudRate(QSerialPort::Baud38400);
                  else if (ui->comboBox_baudrate->currentText() == "57600 bps")
                      mSerialport->setBaudRate(QSerialPort::Baud57600);
                  else if (ui->comboBox_baudrate->currentText() == "115200 bps")
                      mSerialport->setBaudRate(QSerialPort::Baud115200);
              
                  mSerialport->setDataBits(QSerialPort::Data8);
                  mSerialport->setStopBits(QSerialPort::OneStop);
              
                  if (mSerialport->open(QIODevice::ReadWrite))
                  {
                      QMessageBox::information(this,tr("connect"),"Serial communication start");
                  }
                  else
                  {
                      QMessageBox::critical(this,tr("error"),mSerialport->errorString());
                  }
              }
              
              void MainWindow::on_pushButton_disconnect_clicked()
              {
                  QMessageBox::information(this,tr("disconnect"),"Serial communication end");
                  mSerialport->close();
              }
              
              void MainWindow::read_device_data()
              {
                  QByteArray device_data(mSerialport->readAll());
                  ui->data_show->setText(device_data);
              
                  ui->dial->setRange(0,3.5);
                  qDebug() << device_data;
                  ui->dial->setValue(device_data.toInt());
              }
              
              void MainWindow::on_radioButton_on_clicked()
              {
                  qDebug() << "written->" << mSerialport->write("mon 1\n");
              }
              
              void MainWindow::on_radioButton_off_clicked()
              {
                  qDebug() << "written->" << mSerialport->write("mon 0\n");
              }
              
              
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by jsulm
              #6

              @segtteee said in I want to know how to assign a value to a dial:

              These values ​​appear consecutively in application output

              What are these values?
              Can you show what you see in application output?
              Also it looks like you want to use floating point numbers, not integers. What does your device send to you exactly? What is the format of that numbers?
              Also you should not set the range in read_device_data() - it is enough to do it once in the constructor.
              And keep in mind that readAll() will not necessarily give you everything your device sent to you. Because the data can be split into several pieces. In this case you would need to buffer the data until you got the whole packet.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              S 1 Reply Last reply
              2
              • jsulmJ jsulm

                @segtteee said in I want to know how to assign a value to a dial:

                These values ​​appear consecutively in application output

                What are these values?
                Can you show what you see in application output?
                Also it looks like you want to use floating point numbers, not integers. What does your device send to you exactly? What is the format of that numbers?
                Also you should not set the range in read_device_data() - it is enough to do it once in the constructor.
                And keep in mind that readAll() will not necessarily give you everything your device sent to you. Because the data can be split into several pieces. In this case you would need to buffer the data until you got the whole packet.

                S Offline
                S Offline
                segtteee
                wrote on last edited by
                #7

                @jsulm
                1_1506410817057_nv.png
                This is the data I sent from the equipment I saw in the project.

                0_1506410849275_ao.png
                This is due to qdebug in the application output

                What I want is that this number applies to qdial. I want qdial to change when the data sent by the device changes.

                jsulmJ 1 Reply Last reply
                0
                • S segtteee

                  @jsulm
                  1_1506410817057_nv.png
                  This is the data I sent from the equipment I saw in the project.

                  0_1506410849275_ao.png
                  This is due to qdebug in the application output

                  What I want is that this number applies to qdial. I want qdial to change when the data sent by the device changes.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by jsulm
                  #8

                  @segtteee Good to know that the data is actually a string containing a floating point number and a newline...
                  You should do it this way:

                  • Set range to 0-350 (because QDial only accepts integer numbers, see its documentation)
                  • Remove new line from each number string
                  • Convert to double
                  • Multiply by 100 and set it in dial
                  double number = QString(device_data).remove('\n').toDouble() * 100;
                  qDebug() << number; // Make sure conversion to double worked
                  ui->dial->setValue(number);
                  

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  S 1 Reply Last reply
                  4
                  • jsulmJ jsulm

                    @segtteee Good to know that the data is actually a string containing a floating point number and a newline...
                    You should do it this way:

                    • Set range to 0-350 (because QDial only accepts integer numbers, see its documentation)
                    • Remove new line from each number string
                    • Convert to double
                    • Multiply by 100 and set it in dial
                    double number = QString(device_data).remove('\n').toDouble() * 100;
                    qDebug() << number; // Make sure conversion to double worked
                    ui->dial->setValue(number);
                    
                    S Offline
                    S Offline
                    segtteee
                    wrote on last edited by
                    #9

                    @jsulm
                    Thank you very much, the dial works normally. I will probably learn to get rid of "\ n" and dial accept only integers .

                    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