发送信号后如何刷新QML中的组合框

时间:2019-07-05 14:31:12

标签: qt qml loader qqmlcomponent

基本上,我有一个使用QStringList填充的qml组合框。但是,我无法刷新组合框(重新加载)以显示列表已更改。我考虑使用Loader进行此操作,但无法弄清楚。有人可以指导我如何做。

network.qml

Popup{
    contentItem: Rectangle{

        LabelValueList {
            id: list1

            row1: LabelValue {
                id: row1

            row2: LabelValue {
                id: row2

                value: ComboBox {
                    id: combobox

                    model: ListModel {
                        id: comboModel

                        Component.onCompleted: {
                            //..
                            }
                        }
                    }
                }
            }
        }
    }
}

network.h

class Network : public QObject{
    Q_OBJECT
    Q_PROPERTY(QStringList listOfNetworks READ m_listOfNetworks NOTIFY updateNetworks)

    private:
        QStringList m_listOfNetworks;

    public:
        explicit Network(QObject *parent = 0);
        QStringList listOfNetworks() const;

    public slots:
        void slot_scanNetworks();

    signals:
        void updateNetworks();
};

network.cpp

Network::Network(QObject *parent) : QObject (parent){
}

void Network::slot_scanNetworks(){

    QFile SSIDsFile("/home/root/networking/listOfWifiNetworks.txt");

    m_listOfNetworks.clear();
    if (!SSIDsFile.open(QIODevice::ReadOnly | QIODevice::Text)){
    //
    }
    else{
        QTextStream scanNetworkStream(&SSIDsFile);

        while (!scanNetworkStream.atEnd()){
            QString line = scanNetworkStream.readLine();

            if (line.size() != 0){
                QStringList lineSplit = line.split(' ');
                m_listOfNetworks.append(lineSplit[1]);
            }
        }
    }
    SSIDsFile.close();
    emit updateNetworks();

}

如何重新加载row2的组合框以刷新列表?它只在开始时获得列表,但是当我发出信号updateNetworks()时,我想更新下拉列表(组合框)。我尝试使用Loader并将其source.Component设置为row2的ID,但始终收到错误“错误:无法将QObject *分配给QQmlComponent”。有帮助吗?

1 个答案:

答案 0 :(得分:1)

我不是专业人士,但也许可以帮助您。 您可以在您的应用程序上使用计时器并将其设置为1以刷新它并创建一个变量,并在调用信号调用此变量时 例子:

// make a variable type of bool by value = false
property bool refreshMyBoxNow : false 


//add this timer to ur project 
    Timer
    {
        id: timeToRefreshBox 
        //interval = time to tick . (1000) = 1sec 
        interval: 1; running: true; repeat: true
        onTriggered:
        {
             //your code here for refresh box
        }
    }