Add in qml combobox serial port name seach in C++



  • hello .
    I want to add in my combobox define in my qml file all serial port name available .
    Debug() function print correctely port names but combo box in app is empty..... please help...

    // C++
    int main(int argc, char *argv[])
    {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;

    QQmlComponent component(&engine, QUrl(QLatin1String("qrc:/main.qml")));
    QObject *object = component.create();
    
    QStringList lport;
    QList<QSerialPortInfo> ListPort;
    ListPort = QSerialPortInfo::availablePorts();
    
    qDebug() << "Number of serial ports:" << ListPort.count();
    for (int i=0; i < ListPort.size();i++)
    {
        qDebug() << "Names of serial ports:" << ListPort.at(i).portName();
        lport << ListPort.at(i).portName();
    }
    QObject * ComboPort = object->findChild<QObject*>("cb_port");
    if (ComboPort)
     {
            ComboPort->setProperty("cbItems",lport);
     }
    return app.exec();
    

    }

    // Qml

    ComboBox {
    id: cb_port
    objectName: "cb_port"
    x: 480
    y: 91
    anchors.bottom: parent.bottom
    anchors.bottomMargin: 519
    model: cbItems
    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.