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. How to use 2 LCDnumber widget ?
Forum Updated to NodeBB v4.3 + New Features

How to use 2 LCDnumber widget ?

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

    Hi all,

    Using an Arduino which send two values, I would like to process them in C++ throught Qt.

    I know how to show and declare one LCDNumber widget, but I am not sure how to do with two.
    Actually, I show data on the LCD like this:

    @void MainWindow::serialReceived()
    {
    QByteArray valeur;
    valeur = serial->readAll();
    float nb = valeur.toFloat();
    ui->lcdNumber->display(nb);
    }
    @

    How can I declare a second LCD ?

    Any help will be appreciate.

    Regards,

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Drag and drop the second LCD to your widget in the designer. By default it will be given name "lcdNumber_2" but you can change it to whatever you want in the property editor (the window on the right side of the designer). It's the objectName property at the beginning of the list.

      Then you can access it from code by its name like any other widget e.g.
      @
      ui->lcdNumber_2-> ... //do whatever
      @

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cherault
        wrote on last edited by
        #3

        Hi Chris,

        Thanks for you help.
        I will try it...it make sense ;-)

        Regards,

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cherault
          wrote on last edited by
          #4

          Hi all,

          Ok, I did what Chris said...it's fine, thanks a lot.

          Now my small challenge is the next:

          An Arduino send two values over the serial port.
          How can I do in Qt to separate and read those two values ?
          (ie. Val_1, Val_2 on Arduino side)

          Thanks for your help.

          Regards,

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cherault
            wrote on last edited by
            #5

            Hi all,

            This my main code to read the two values coming from an Arduino through the serial port.

            @
            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            #include <QSerialPort>

            QSerialPort *serial;

            MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
            {
            ui->setupUi(this);

            serial=new QSerialPort(this);
            serial->setPortName("/dev/ttyACM0");
            serial->setBaudRate(QSerialPort::Baud9600);
            serial->setDataBits(QSerialPort::Data8);
            serial->setParity(QSerialPort::NoParity);
            serial->setStopBits(QSerialPort::OneStop);
            serial->setFlowControl(QSerialPort::NoFlowControl);
            serial->open(QIODevice::ReadOnly);//mettre un bouton d'activation du port
            
            connect(serial, SIGNAL(readyRead()),this,SLOT(analogRead0()));
            connect(serial, SIGNAL(readyRead()),this,SLOT(analogRead2()));
            

            }

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

            void MainWindow::analogRead0()
            {
            QByteArray val0;
            val0 = serial->readLine();
            float nb0 = val0.toFloat();
            ui->lcdNumber->display(nb0);
            }

            void MainWindow::analogRead2()
            {
            QByteArray val2;
            val2 = serial->readLine();
            float nb2 = val2.toFloat();
            ui->lcdNumber_2->display(nb2);
            }
            @

            The connection works fine, no problem.
            But if my Arduino send for example:

            analogRead(0)
            253
            235
            256
            ...

            and the other one
            analoRead(2)
            241
            214
            236
            ...

            I just would like to show the two outputs on the LCD.
            Just only one works...why ?

            Thanks for your help and support.

            Regards and happy new year to all of you !!!

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @
              connect(serial, SIGNAL(readyRead()),this,SLOT(analogRead0()));
              connect(serial, SIGNAL(readyRead()),this,SLOT(analogRead2()));
              @
              This connects two slots to the same signal. If the device sends a single number both of these will still be called.

              @
              QByteArray val2;
              val2 = serial->readLine();
              @
              Does that even compile? According to docs "readLine":http://doc.qt.io/qt-5/qiodevice.html#readLine returns a qint64 indicating how much data was read. A number is not convertible to a byte array.

              1 Reply Last reply
              0
              • C Offline
                C Offline
                cherault
                wrote on last edited by
                #7

                Hi Chris,

                Thanks for your help.

                Yes it compile without problem.

                So, could you help me to show through 2 lcd the two values I've got over the serial port ?
                I really don't know how to do it.

                Thanks a lot.

                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