QStringListModel不是虚类,可以直接创建对象。比如在类的构造函数中做了如下处理:
m_spIdsModel = std::make_shared<QStringListModel>();
QStringList list;
list << "a" << "b" << "c";
m_spIdsModel->setStringList(list);
其中智能指针非必须。
我将m_spIdsModel所在的类对象注册到了qml,以便在qml能沟通直接调用类的接口。
接口如下:
StrListModelPtr CImageProvider::getIdsModel()
{
return m_spIdsModel;
}
另外,需要将QStringListModel类型注册到qml:
qmlRegisterType<QStringListModel>("pv.StrListModel", 1, 0, "StrListModel");
在qml下使用方式如下:
ComboBox{
id:cusComboBox
width:200
height:20
editable:false
background: Rectangle {
color: "blue"
border.width: 0
}
delegate: Rectangle {
border.color:"lightblue"
height: 25
width: parent.width
Text {
anchors.centerIn: parent
text: model.display //此处关键
}
}
model:imageShowView.getIdsModel()
}
关键在于显示文本使用model.display. 这种用法是从其它帖子看到的,查询帮助文档没有找到。