本人最近使用QML进行开发,在开发中遇到一个VisualDataModel的问题,请各位帮帮忙看看问题究竟出现在哪里,问题如下:将Qt中QStringList赋给VisualDataModel的model,然后显示出来,问题就在于我在Qt中改变VisualDataModel的model时,QML中显示出来的结果跟想象的不一样,而且结果是没规律,不知道大家有没有遇到过这样的问题(不知道大家有没有在用QML开发,如果有的话可以留下QQ号码大交流交流,QQ号:305938847),急用,源码如下:
testVisualDataModel.qml:
import Qt 4.7
Rectangle {
width: 1024; height: 600;
color: "black";
VisualDataModel {
id: visualModel
model: testVisualData.listModel;
delegate: Rectangle {
height: 25; width: 100
Text { text: "Name: " + modelData }
}
onCountChanged: console.log("onCountChanged: " + count);
}
ListView {
x: 0; y: 0; width: 200; height: 200;
model: visualModel
}
Rectangle { //清空visualModel的model
x: 500; y: 10; width: 100; height: 30;
color: "black"; border.width: 2; border.color: "white";
Text {
anchors.centerIn: parent;
color: "white"; text: "Clear"; font.pointSize: 20;
}
MouseArea {
anchors.fill: parent;
onReleased: testVisualData.qmlClear();
}
}
Rectangle { // 删除visualModel的model中的最后一个元素
x: 500; y: 50; width: 100; height: 30;
color: "black"; border.width: 2; border.color: "white";
Text {
anchors.centerIn: parent;
color: "white"; text: "DelLast"; font.pointSize: 20;
}
MouseArea {
anchors.fill: parent;
onReleased: testVisualData.qmlDel();
}
}
Rectangle { // visualModel的model中添加一个元素
x: 500; y: 90; width: 100; height: 30;
color: "black"; border.width: 2; border.color: "white";
Text {
anchors.centerIn: parent;
color: "white"; text: "Add"; font.pointSize: 20;
}
MouseArea {
anchors.fill: parent;
onReleased: testVisualData.qmlAdd();
}
}
Rectangle { // 改变visualModel的model
x: 500; y: 130; width: 100; height: 30;
color: "black"; border.width: 2; border.color: "white";
Text {
anchors.centerIn: parent;
color: "white"; text: "Change"; font.pointSize: 20;
}
MouseArea {
anchors.fill: parent;
onReleased: testVisualData.qmlChange();
}
}
}
testVisualDataModel.h:
class CTestVisualDataModel : public QObject
{
Q_OBJECT
Q_PROPERTY(QStringList listModel READ getListModel NOTIFY sgListModelChanged)public:
CTestVisualDataModel(QObject *parent = 0);
~CTestVisualDataModel();
protected:
Q_INVOKABLE void qmlClear();
Q_INVOKABLE void qmlDel();
Q_INVOKABLE void qmlAdd();
Q_INVOKABLE void qmlChange(); void setListModel(QStringList& listModel);
QStringList getListModel() const;
private:
QStringList m_slListModel;signals:
void sgListModelChanged();
};
testvisualdatamodel.cpp:
#include "testvisualdatamodel.h"CTestVisualDataModel::CTestVisualDataModel(QObject *parent)
: QObject(parent)
{
for (int i=0; i<10; i++)
{
this->m_slListModel << QString::number(i);
}
}void CTestVisualDataModel::qmlClear()
{
this->m_slListModel.clear();
emit this->sgListModelChanged();
}void CTestVisualDataModel::qmlDel()
{
if (this->m_slListModel.count() > 0)
{
this->m_slListModel.removeLast();
emit this->sgListModelChanged();
}
}void CTestVisualDataModel::qmlAdd()
{
this->m_slListModel.push_back("test");
emit this->sgListModelChanged();
}void CTestVisualDataModel::qmlChange()
{
QStringList listModel;
listModel << "100" << "101" << "102" << "103" << "104" << "105";
this->setListModel(listModel);
}void CTestVisualDataModel::setListModel(QStringList& listModel)
{ this->m_slListModel = listModel;
emit this->sgListModelChanged();
}QStringList CTestVisualDataModel::getListModel() const
{
return this->m_slListModel;
}CTestVisualDataModel::~CTestVisualDataModel()
{}