How to use 2 LCDnumber widget ?
-
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,
-
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
@ -
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,
-
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 !!!
-
@
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.