Solved Exposing a qml signal to a C++ slot for qserialport.
-
Hi Guys,
Thanks for looking firstly, im trying to expose a simple signal from qml to my C++ code and i thought i could do it the way ive added below, i dont get any errors but i also dont get any action.
Ive already got data being recieved from the serialport and updating a text object in qml so i know the serial works.
Im wondering if ive missed something crucial for it to work with Qserialport. Any help is much appreciated.
.hpublic slots: void clicked(const QByteArray &data = QByteArray("r255"));
main.cpp
qmlRegisterType<SerialPort>("SerialPortLib", 1, 0, "SerialPort");
.cpp
void SerialPort::clicked(const QByteArray &data) { arduino->write(data); qDebug() << data; }
qml
Window { id: root signal clicked RadioButton { id: radioButton text: qsTr("Radio Button") onClicked: root.clicked } }
-
@StevieGardiner hi,
in your qml where do you call "SerialPort::clicked(const QByteArray &data)" ?
LA -
@StevieGardiner it looks like although you've registered your SerialPort class, you're not using it in QML :-(
qml
import SerialPortLib 1.0 Window { id: root SerialPort { id: serial } RadioButton { id: radioButton text: qsTr("Radio Button") onClicked: serial.sendData("data from QML!") } }
I'm using sendData() as the name of the slot exposed in C++, as it seems more meaningful (the serial port was not 'clicked' itself...). Spoiler alert: code may not work as is, just trying to show the idea
-
@LeLev I show it here...
#include "serialport.h" #include <QDebug> SerialPort::SerialPort(QObject *parent):QObject(parent) { arduino = new QSerialPort(this); connect(arduino, &QSerialPort::readyRead, this, &SerialPort::onReadData); openDefault(); } SerialPort::~SerialPort() { delete arduino; } void SerialPort::set_oil_pressure_volt(double newValue) { if (mOil_pressure_volt == newValue) return; mOil_pressure_volt = newValue; emit oil_pressure_volt_Changed(mOil_pressure_volt); } void SerialPort::onReadData() { if(arduino->bytesAvailable()>0){ QByteArray data = arduino->readAll(); qDebug()<<QString(data).trimmed(); QString value = QString(data).trimmed(); bool ok; double val = value.toDouble(&ok); if(ok) set_oil_pressure_volt(val); } } void SerialPort::clicked(const QByteArray &data) { arduino->write(data); qDebug() << data; } void SerialPort::openDefault() { for(auto info: QSerialPortInfo::availablePorts()){ qDebug()<<info.portName()<<info.description()<<info.manufacturer(); if(!info.isBusy() && (info.description().contains("") || info.manufacturer().contains(""))){ portInfo = info; break; } } if(portInfo.isNull()){ return; } arduino->setPortName(portInfo.portName()); arduino->setBaudRate(QSerialPort::Baud115200); arduino->setDataBits(QSerialPort::Data8); arduino->setParity(QSerialPort::NoParity); arduino->setStopBits(QSerialPort::OneStop); arduino->setFlowControl(QSerialPort::NoFlowControl); if(arduino->open(QSerialPort::ReadWrite)) qDebug()<<"Connected to "<< portInfo.manufacturer()<< " on " << portInfo.portName(); else qCritical()<<"Serial Port error: " << arduino->errorString(); } double SerialPort::get_oil_pressure_volt() const { return mOil_pressure_volt; }
-
@Pablo-J.-Rogina This worked perfectly thank you, but there is about a second delay in the signal being sent d the program stalls, woulf you have any ideas on how i could get rid of the stalling?
-
@StevieGardiner by "stalling" you mean "freezing" or unresponsive. In that case I'm not sure, but my guess is that your serial communication is somehow acting synchronously and somehow affecting GUI response?
-
@Pablo-J.-Rogina Yes it just freezes for the slight second, so the program every second is recieving serial data, and upon sending the command down the serial on the button it freezes for a second.
-
Hi
To rule out anything with QSerialPort or the actual port could you maybe try with
http://doc.qt.io/qt-5/qtserialport-terminal-example.html
and see if it has delay too ?