刷新插槽中的QML列表视图
Refresh QML Listview from Slot
我在QML中刷新Listview时遇到了问题。我看了很多解决方案,但我不想从事件"onClicked"刷新所有Listview中得到任何具体的东西,但如何做到这一点?
源代码:http://s000.tinyupload.com/?file_id=86538244635919176055
main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QDebug>
#include "message.h"
#include "dataobject.h"
int main(int argc, char *argv[]) {
QList<QObject*> dataList;
dataList.append(new DataObject("1"));
dataList.append(new DataObject("2"));
dataList.append(new DataObject("3"));
QApplication app(argc, argv);
QQmlApplicationEngine engine;
Message msg;
msg.setListInstance(&dataList);
auto root_context = engine.rootContext();
root_context->setContextProperty("message",&msg);
//root_context->setContextProperty("myModel", QVariant::fromValue(dataList));
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QQmlContext *ctxt = new QQmlContext(engine.rootContext());
//ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
return app.exec();
}
message.h
#ifndef MESSAGE_H
#define MESSAGE_H
#include <QQmlListProperty>
#include <QObject>
class Message : public QObject {
Q_OBJECT
Q_PROPERTY(QQmlListProperty<QObject> myModel READ getList NOTIFY listChanged)
public:
explicit Message(QObject *parent = 0);
~Message();
void setListInstance(QList<QObject *> *dataList){ list = dataList; }
QQmlListProperty<QObject> myModel() const;
public slots:
void refreshLista();
QQmlListProperty<QObject> getList();
private:
QList<QObject *> *list;
signals:
void listChanged();
};
#endif // MESSAGE_H
消息.cpp
#include "message.h"
#include "dataobject.h"
#include <QDebug>
Message::Message(QObject *parent):QObject(parent){}
Message::~Message(){}
void Message::refreshLista(){
list->append(new DataObject("44444"));
emit listChanged();
qDebug() << " REFRESH LISTA ";
}
QQmlListProperty<QObject> Message::getList(){
return QQmlListProperty<QObject>(this, *list);
}
dataobject.h
#ifndef DATAOBJECT_H
#define DATAOBJECT_H
#include <QObject>
class DataObject : public QObject { Q_OBJECT
Q_PROPERTY( QString title READ title WRITE setTitle NOTIFY info)
public:
DataObject(QObject * parent = 0 );
DataObject(const QString &_title,QObject * parent=0 );
QString title() const;
void setTitle(const QString &);
signals:
void info();
private:
QString m_id;
QString m_title;
};
#endif // DATAOBJECT_H
数据对象.cpp
#include "dataobject.h"
DataObject::DataObject(QObject * parent): QObject(parent){}
DataObject::DataObject(const QString &_title,QObject * parent)
:QObject(parent),m_title(_title)
{}
QString DataObject::title() const { return m_title;}
void DataObject::setTitle(const QString &title) {
if ( title != m_title ) { m_title = title; emit info();}
}
要做的两件事:
Message
必须修改模型,因此Message
需要模型的实例- 当模型发生变化时,向QML发出信号,以便QML可以重新加载数据
假设您的Message
类负责该模型。首先,将模型传递给Message
。
class Message : public QObject {
//...
private:
QList<QObject *> *list;
public:
void setListInstance(QList<QObject *> *dataList){ list = dataList; }
}
//main.cpp
msg.setListInstance(&dataList);
您现在可以轻松更改模型的内容:
void Message::refreshLista(){ list->append(new DataObject("new")); /*whatever*/}
但是,QML不会重新加载模型,因为setContextProperty("myModel", QVariant::fromValue(dataList));
无法发出信号。从main.cpp中删除此行,并在Message
中创建一个新属性:
class Message : public QObject {
Q_OBJECT
Q_PROPERTY(QQmlListProperty<QObject> myModel READ getList NOTIFY listChanged)
public:
QQmlListProperty<QObject> getList();
signals:
void listChanged();
}
在实现中,创建一个QQmlListProperty,并在必要时发出属性更改信号。
void Message::refreshLista(){
list->append(new DataObject("new"));
emit listChanged();
}
QQmlListProperty<QObject> Message::getList(){
return QQmlListProperty<QObject>(this, *list);
}
最后,QML中的ListView应该绑定到message.myModel
,而不是myModel
:
ListView {
width: 400; height: 300;
model: message.myModel
//...
}
相关文章:
- 列表视图更改选择颜色
- Qt - QVector 和模型视图 - 从列表视图获取自定义类的最佳方法是什么?
- 列表视图项的替代颜色
- 如何将 QString 的 QList 分配给列表视图?
- 如何修复列表视图中的错误?,封装控件时无法选择任何项
- 如何在 c++ 中从列表视图中获取值
- Qml 列表视图在重置模型时保留视图
- 创建列表视图后无法更改列表视图中的列宽
- 如何在调整列表视图大小时将标题宽度设置为等于其列表视图控件宽度
- 如何更改列表视图的边框颜色
- Qt:将 c++ 中的列表连接到 QML 中的列表视图
- 如何绑定到列表视图的 OnItemSelectionChanged
- 列表视图最初不显示数据
- 从C++访问列表视图委托的文本
- LVM_GETNEXTITEM在列表视图中找不到项目
- UWP 列表视图占用的高度超过可用高度
- 如何在移动列表视图中添加滚动条?并且用户只能垂直滚动特定区域
- C++ win32 列表视图列
- QML 列表视图和密钥导航 - 处理单个密钥事件
- 循环访问列表视图项 win32 api