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
Qt Code:
  1. qmlRegisterType<Module::Physical>("MyType", 1, 0, "SerialPort");
To copy to clipboard, switch view to plain text mode 
Main.qml
Qt Code:
  1. ApplicationWindow {
  2. SerialPort {
  3. id: module
  4. }
  5. }
To copy to clipboard, switch view to plain text mode 
SerialPort.qml
Qt Code:
  1. Button {
  2. text: qsTr("start")
  3. onClicked: {
  4. stackView.push(Qt.resolvedUrl("LoadingPage.qml"))
  5. module.find()
  6. }
  7. }
To copy to clipboard, switch view to plain text mode 
serialport.h
Qt Code:
  1. Q_INVOKABLE QVector<QString> find();
To copy to clipboard, switch view to plain text mode 
serialport.cpp
Qt Code:
  1. QVector<QString> Physical::find()
  2. {
  3. m_ports.clear();
  4.  
  5. foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
  6. bool hasError = false;
  7.  
  8. QSerialPort port;
  9. port.setPort(info);
  10.  
  11. if (port.open(QIODevice::ReadWrite)) {
  12. if (!hasError && !port.setBaudRate(serial::baudRate)) {
  13. emit error(tr("Can't set baud to %1, error %2")
  14. .arg(port.portName())
  15. .arg(port.error()));
  16. hasError |= true;
  17. }
  18. if (!hasError && !port.setDataBits(serial::dataBits)) {
  19. emit error(tr("Can't set data bits to %1, error %2")
  20. .arg(port.portName())
  21. .arg(port.error()));
  22. hasError |= true;
  23. }
  24.  
  25. if (!hasError && !port.setParity(serial::parity)) {
  26. emit error(tr("Can't set parity to %1, error %2")
  27. .arg(port.portName())
  28. .arg(port.error()));
  29. hasError |= true;
  30. }
  31. if (!hasError && !port.setStopBits(serial::stopBits)) {
  32. emit error(tr("Can't set stop bits to %1, error %2")
  33. .arg(port.portName())
  34. .arg(port.error()));
  35. hasError |= true;
  36. }
  37. if (!hasError && !port.setFlowControl(serial::flowCtrl)) {
  38. emit error(tr("Can't set flow control to %1, error %2")
  39. .arg(port.portName())
  40. .arg(port.error()));
  41. hasError |= true;
  42. }
  43. if (!hasError) {
  44. m_ports.append(port.portName());
  45. }
  46.  
  47. QByteArray data;
  48. data.resize(1);
  49. data[0] = ID_READ;
  50.  
  51. port.write(data);
  52. port.close();
  53. }
  54. }
  55.  
  56. return m_ports;
  57. }
To copy to clipboard, switch view to plain text mode