如何从QModelList中设置/获取信息?

问题描述

我有一个可以获取设备信息的应用程序。我列出了姓名和地址等项目。当应用程序配置设备时,您将获得一个IP地址。当我获得IP地址时,将在列表中添加此信息。我将使用以下代码行进行此操作:networkManager.bluetoothDeviceInfo.ipAddress = networkManager.manager.currentConnection.hostAddress;

此后,我将在最后一个屏幕上显示所有设备和设备的IP地址。

//Part of main.qml where send the @R_101_4045@ion
            Item {
                id: authenticationView
                width: swipeview.width
                height: swipeview.height
                ColumnLayout {
                    anchors.centerIn: parent
                    anchors.verticalCenterOffset: - swipeview.height / 4
                    width: app.iconSize * 8
                    spacing: app.margins
                    Label {
                        Layout.fillWidth: true
                        text: qsTr("Name")
                        visible: !d.currentAP
                    }

                    TextField {
                        id: ssidTextField
                        Layout.fillWidth: true
                        visible: !d.currentAP
                        maximumLength: 32
                        onAccepted: {
                            passwordTextField.focus = true
                        }
                    }

                    Label {
                        Layout.fillWidth: true
                        text: qsTr("Password")
                    }

                    RowLayout {
                        TextField {
                            id: passwordTextField
                            Layout.fillWidth: true
                            maximumLength: 64
                            property bool showPassword: false
                            echoMode: showPassword ? TextInput.normal : TextInput.Password
                            onAccepted: {
                                okButton.clicked()
                            }
                        }

                        ColorIcon {
                            Layout.preferredHeight: app.iconSize
                            Layout.preferredWidth: app.iconSize
                            name: "../images/eye.svg"
                            color: passwordTextField.showPassword ? colorTint : keyColor
                            MouseArea {
                                anchors.fill: parent
                                onClicked: passwordTextField.showPassword = !passwordTextField.showPassword
                            }
                        }
                    }

                    Button {
                        id: okButton
                        Layout.fillWidth: true
                        text: qsTr("OK")
                        enabled: passwordTextField.displayText.length >= 8
                        onClicked: {
                            if (d.currentAP) {
                                connectingToWiFiView.text = qsTr("Connecting the W160x module to %1").arg(d.currentAP.ssid);
                                networkManager.manager.connectWirelessNetwork(d.currentAP.ssid,passwordTextField.text)
                                sidd = d.currentAP.ssid;
                                password = passwordTextField.text;
                            } else {
                                connectingToWiFiView.text = qsTr("opening access point \"%1\" on the W160x module.").arg(ssidTextField.text);
                                networkManager.manager.connectWirelessNetwork(ssidTextField.text,passwordTextField.text)
                                sidd = ssidTextField.text;
                                password = passwordTextField.text;
                            }

                            connectingToWiFiView.buttonText = "";
                            connectingToWiFiView.running = true


                            if(d.currentAP)
                            {
                                if (networkManager.manager.currentConnection.hostAddress.length != 0){
                                    networkManager.bluetoothDeviceInfo.ipAddress = networkManager.manager.currentConnection.hostAddress;
                                    
                            }
                            swipeview.currentIndex++
                        }
                    }
                }
            }

// Part of main.qml where get the @R_101_4045@ion of the list
            Item {
                id: resultsView
                height: swipeview.height
                width: swipeview.width

                ColumnLayout {
                    anchors.fill: parent

                    ListView {
                        id: connectView
                        height: swipeview.height
                        width: swipeview.width
                        model: discovery.deviceInfos
                        clip: true
                        ColorIcon {
                            Layout.preferredHeight: app.iconSize
                            Layout.preferredWidth: app.iconSize
                        }
                        delegate: BerryLanItemDelegate {
                            width: parent.width
                            text: name
                            iconSource: ipaddress != "" ? "../images/green.svg" : "../images/red.svg"
                        }
                    }                    
                }
            }

所以我也有C ++文件和标头: bluetoothdeviceinfos.ccp

    QVariant BluetoothDeviceInfos::data(const QModelIndex &index,int role) const
{
    if (index.row() < 0 || index.row() >= m_deviceInfos.count())
        return QVariant();

BluetoothDeviceInfo *deviceInfo = m_deviceInfos.at(index.row());
if (role == BluetoothDeviceInfoRoleName) {
    return deviceInfo->name();
} else if (role == BluetoothDeviceInfoRoleAddress) {
    return deviceInfo->address();
} else if (role == BluetoothDeviceInfoRoleLe) {
    return deviceInfo->isLowEnergy();
} else if(role == BluetoothDeviceInfoRoleSelected){
    return deviceInfo->selected();
} else if(role == NetwerkInfoIPAddress){
    return deviceInfo->ipAddress();
}

return QVariant();
}
bool BluetoothDeviceInfos::setData(const QModelIndex &index,const QVariant &value,int role)
{
    qDebug() << "Stap 1";
    if (role == Qt::EditRole) {
        if (!checkIndex(index))
            return false;
        //save value from editor to member m_gridData
        BluetoothDeviceInfo *deviceInfo = m_deviceInfos.at(index.row());
        //for presentation purposes only: build and emit a joined string
        if(role == BluetoothDeviceInfoRoleSelected){
            qDebug() << "Stap 3";
            deviceInfo->setSelected(value.toBool());
        } else if(role == NetwerkInfoIPAddress){
            qDebug() << "Stap 4";
            deviceInfo->setIpAddress(value.toString());
        }
        //emit editCompleted();
        emit dataChanged();
        return true;
    }
    return false;
}

bluettohdeviceinfos.h

#ifndef BLUetoOTHDEVICEINFOS_H
#define BLUetoOTHDEVICEINFOS_H

#include <QObject>
#include <QAbstractListModel>

#include "bluetoothdeviceinfo.h"

class BluetoothDeviceInfos : public QAbstractListModel
{
    Q_OBJECT
    Q_PROPERTY(int count READ rowCount NOTIFY countChanged)

public:
    enum BluetoothDeviceInfoRole {
        BluetoothDeviceInfoRoleName = Qt::displayRole,BluetoothDeviceInfoRoleAddress,BluetoothDeviceInfoRoleLe,BluetoothDeviceInfoRoleSelected,NetwerkInfoIPAddress
    };

    explicit BluetoothDeviceInfos(QObject *parent = nullptr);

    QList<BluetoothDeviceInfo *> deviceInfos();

    int rowCount(const QModelIndex & parent = QModelIndex()) const override;
    QVariant data(const QModelIndex & index,int role = Qt::displayRole) const override;
    bool setData(const QModelIndex &index,int role = Qt::EditRole) override;

    int count() const;
    Q_INVOKABLE BluetoothDeviceInfo *get(int index) const;
    Q_INVOKABLE BluetoothDeviceInfo *set(int index,bool selected);

    void addBluetoothDeviceInfo(BluetoothDeviceInfo *deviceInfo);
    Q_INVOKABLE void removeBluetoothDeviceInfo(int index);
    Q_INVOKABLE void clearModel();

signals:
    void countChanged();
    void dataChanged();

protected:
    QHash<int,QByteArray> roleNames() const override;

private:
    QList<BluetoothDeviceInfo *> m_deviceInfos;

};

#endif // BLUetoOTHDEVICEINFOS_H

现在是我没有从列表中获取信息的问题

我希望这将是足够的信息。有关更多信息,请查看页面https://github.com/scottkoekkoek/ConfigurateApp

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)