变化C++ QAbstractListModel从设为Qml

问题描述:

值,这是我的代码变化C++ QAbstractListModel从设为Qml

devicemodel.h

class Device 
{ 
public: 
    Device(const int &nodeId ,const QString &type, const int &lampVoltage); 
//![0] 

    QString type() const; 
    int lampVoltage() const; 
    int nodeId() const; 
public: 
    QString m_type; 
    int m_lampVoltage; 
    int m_nodeId; 
//![1] 


}; 

class DeviceModel : public QAbstractListModel 
{ 
    Q_OBJECT 
public: 
    enum DeviceRoles { 
     NodeIdRole = Qt::UserRole + 1, 
     TypeRole , 
     LampVoltageRole 

    }; 

    DeviceModel(QObject *parent = 0); 
//![1] 

    void addDevice(const Device &Device); 

    int rowCount(const QModelIndex & parent = QModelIndex()) const; 

    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; 

protected: 
    QHash<int, QByteArray> roleNames() const; 
private: 
    QList<Device> m_Devices; 
//![2] 

public slots : 
    void callFromQml(int index); 

}; 

devicemodel.cpp

Device::Device(const int &nodeId ,const QString &type, const int &lampVoltage) 
    : m_nodeId(nodeId) , m_type(type), m_lampVoltage(lampVoltage) 
{ 
} 

QString Device::type() const 
{ 
    return m_type; 
} 

int Device::nodeId() const 
{ 
    return m_nodeId; 
} 

int Device::lampVoltage() const 
{ 
    return m_lampVoltage; 
} 

DeviceModel::DeviceModel(QObject *parent) 
    : QAbstractListModel(parent) 
{ 
} 



void DeviceModel::callFromQml(int index){ 

    Device k= m_Devices.at(index); 
    qDebug() << k.type() << k.lampVoltage(); 
    k.m_lampVoltage = 5; 
    emit dataChanged(createIndex(index,0), createIndex(index,0), {0,1,2}); 
} 

void DeviceModel::addDevice(const Device &Device) 
{ 
    beginInsertRows(QModelIndex(), rowCount(), rowCount()); 
    m_Devices << Device; 
    endInsertRows(); 
} 

int DeviceModel::rowCount(const QModelIndex & parent) const { 
    Q_UNUSED(parent); 
    return m_Devices.count(); 
} 

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

    const Device &Device = m_Devices[index.row()]; 
    if (role == NodeIdRole) 
     return Device.nodeId(); 
    else if (role == TypeRole) 
     return Device.type(); 
    else if (role == LampVoltageRole) 
     return Device.lampVoltage(); 
    return QVariant(); 
} 

//![0] 
QHash<int, QByteArray> DeviceModel::roleNames() const { 
    QHash<int, QByteArray> roles; 
    roles[NodeIdRole] = "nodeId"; 
    roles[TypeRole] = "type"; 
    roles[LampVoltageRole] = "lampVoltage"; 
    return roles; 
} 

main.qml

ListView { 
    width: 200; height: 250 
    spacing: 10 
    model: myModel 
    delegate: 
     Text { text: "Animal: " + type + ", " + lampVoltage 

     MouseArea { 
      anchors.fill: parent 
      onClicked: { 
       console.log("egwegweg"); 
       myModel.callFromQml(index); 
       //lampVoltage = 10; 

       // myModel.setData(index , 10 , 2); 
      } 
     } 
    } 
} 

的main.cpp

QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); 
    QGuiApplication app(argc, argv); 
    QQmlApplicationEngine engine; 
    DeviceModel model; 
    model.addDevice(Device(1, "Medium" , 200)); 
    model.addDevice(Device(1, "Medium" , 200)); 
    model.addDevice(Device(1, "Medium" , 200)); 
    QQmlContext *ctxt = engine.rootContext(); 
    ctxt->setContextProperty("myModel", &model); 
    engine.load(QUrl(QLatin1String("qrc:/main.qml"))); 

    model.addDevice(Device(1, "Medium" , 200)); 
    model.addDevice(Device(1, "Medium" , 200)); 
    model.addDevice(Device(1, "Medium" , 200)); 

    return app.exec(); 

我想从设为Qml 修改我的项目值我写了一个插槽命名callFromQml和QML通过项目的索引C++代码,并希望从中 更新值,但不能做到这一点 我不不知道发射信号是否正常工作,不知道传递索引是否正确或不合格

+0

不是一个真正的答案,但一些能够为您节省大量的时间和精力:https://*.com/a/35161903/991484 – dtech

+0

你可以只添加一个'Q_INVOKABLE',需要一个整数索引,角色名称和值。该方法在内部将整数索引转换成一个'ModelIndex',并用这个调用['setData(...)'](http://doc.qt.io/qt-5/qabstractitemmodel.html#setData)。或者,我认为,如果你写给'模型。someRole = someNewValue'它会通过一些魔法自动回写。 – derM

+0

我无法理解你如何正确地提供我的代码片段? – mehran

感谢您对@MarkCh指出,你没有重新实现setData()-method

的签名是:

bool QAbstractItemModel::setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) 

它返回true如果一些数据被成功地设置,并且false如果不是。 此外,如果成功,在返回之前,有必要emit dataChanged(...),以便任何视图都能意识到更改。

让我们开始吧:

  1. 添加相应的声明头文件
  2. 添加实施到.cpp -file。这可能是这样的:

bool QAbstractItemModel::setData(const QModelIndex &index, const QVariant &value, int role) 
{ 
    if (index.row() < 0 || index.row() >= m_Devices.count()) 
     return false; 

    const Device &Device = m_Devices[index.row()]; 
    if (role == NodeIdRole) { 
     Device.m_nodeId = value.toInt(); 
     emit dataChanged(index, index); 
     return true; 
    } 
    else if (role == TypeRole) { 
     Device.m_type = value.toString(); 
     emit dataChanged(index, index); 
     return true; 
    } 
    else if (role == LampVoltageRole) { 
     Device.m_lampVoltage = value.toInt(); 
     emit dataChanged(index, index); 
     return true; 
    } 
    return false; 
} 
  1. 使DeviceModelDevice一个friend或改变私有字段来干将的使用访问。随你便。

  2. 现在你应该可以设置角色的代表,如果他们的属性...喜欢:

    onClicked: model.nodeId = 100; 
    

    上面的代码没有进行测试,而应包括最相关的细节。

开始=>