QSerialPort and QtQuick animation freezing on list and open serial ports
Anyone could help me, please.
I wrote a C++ method to find all serial ports, open, write and close and use to Q_INVOKABLE to call this method from a QML.
The problem is a freezing on push LoadingPage.qml if there are many serial ports connected, the animation start and then immediately freezes, when the function find finish, the animation start again. [SerialPort.qml]
How is it the better way to solve that?
Thanks
main.cpp
Code:
qmlRegisterType<Module::Physical>("MyType", 1, 0, "SerialPort");
Main.qml
Code:
ApplicationWindow {
SerialPort {
id: module
}
}
SerialPort.qml
Code:
Button {
text: qsTr("start")
onClicked: {
stackView.push(Qt.resolvedUrl("LoadingPage.qml"))
module.find()
}
}
serialport.h
Code:
Q_INVOKABLE QVector<QString> find();
serialport.cpp
Code:
QVector<QString> Physical::find()
{
m_ports.clear();
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
bool hasError = false;
QSerialPort port;
port.setPort(info);
if (!hasError && !port.setBaudRate(serial::baudRate)) {
emit error(tr("Can't set baud to %1, error %2")
.arg(port.portName())
.arg(port.error()));
hasError |= true;
}
if (!hasError && !port.setDataBits(serial::dataBits)) {
emit error(tr("Can't set data bits to %1, error %2")
.arg(port.portName())
.arg(port.error()));
hasError |= true;
}
if (!hasError && !port.setParity(serial::parity)) {
emit error(tr("Can't set parity to %1, error %2")
.arg(port.portName())
.arg(port.error()));
hasError |= true;
}
if (!hasError && !port.setStopBits(serial::stopBits)) {
emit error(tr("Can't set stop bits to %1, error %2")
.arg(port.portName())
.arg(port.error()));
hasError |= true;
}
if (!hasError && !port.setFlowControl(serial::flowCtrl)) {
emit error(tr("Can't set flow control to %1, error %2")
.arg(port.portName())
.arg(port.error()));
hasError |= true;
}
if (!hasError) {
m_ports.append(port.portName());
}
data.resize(1);
data[0] = ID_READ;
port.write(data);
port.close();
}
}
return m_ports;
}
Re: QSerialPort and QtQuick animation freezing on list and open serial ports
From StackOverflow, answered by Kuba Ober, source: http://stackoverflow.com/questions/2...n-serial-ports
Quote:
Your code runs in the GUI thread, and since it blocks the GUI thread, the user interaction is stopped as well.
You need to perform the scan in a separate thread. The Qt Concurrent framework is perfect for this, since you're performing a self-contained action that can be done in any thread. Your find() method must be turned into a stand-alone function or a static method (since that's what it really is).
You'd then run it as follows:
Code:
class Physical {
QFuture<QStringList> m_future;
QFutureWatcher<QStringList> m_futureWatcher;
// A string list is a simpler type to type :)
...
}
Q_SLOT void findPortsFinished() {
// use the list of ports
}
public:
Physical() {
connect(&m_futureWatcher, SIGNAL(finished()), SLOT(findPortsFinished()));
m_futureWatcher.set(m_future);
...
}
Q_SLOT void findPorts() {
if (m_future.isRunning()) return;
m_future = QtConcurrent::run(doFindPorts);
}
};
Re: QSerialPort and QtQuick animation freezing on list and open serial ports
That or processing the serial ports one at a time.
Cheers,
_
Re: QSerialPort and QtQuick animation freezing on list and open serial ports