我目前正在尝试基于C ++ QStringList
(QList<QString>
)创建一个列表视图。
这里的问题是列表没有出现,既没有预先填写的列表也没有我的更新列表。同样附加tableView
以下是一些代码:
NetworkHander.h
class NetworkHandler : public INetworkHandler
{
Q_OBJECT
Q_PROPERTY(QList<QString> list
READ listApplications
WRITE setListApplications
NOTIFY listApplicationsChanged)
public:
NetworkHandler();
~NetworkHandler();
Q_INVOKABLE QList<QString> listApplications() const;
void setListApplications(const QList<QString> &list);
signals:
void listApplicationsChanged();
public slots:
void replyFinished(QNetworkReply* reply);
void refresh();
void open(const QString &file);
Q_INVOKABLE void getApplications();
private:
QList<QString> m_listApplications;
};
NetworkHandler.cpp
#include <QJsonArray>
#include <QJsonDocument>
#include <QListIterator>
#include "NetworkHandler.h"
#include "NetworkCommunication.h"
NetworkHandler::NetworkHandler() {
m_listApplications<<"Bla";
m_listApplications<<"Bla2";
m_listApplications<<"Bla3";
m_listApplications<<"Bla4";
}
NetworkHandler::~NetworkHandler() {
}
void NetworkHandler::replyFinished(QNetworkReply* reply)
{
reply->moveToThread(this->thread());
QString string = reply->readAll();
QByteArray b = string.toLocal8Bit();
QJsonDocument doc = QJsonDocument::fromJson(b);
QJsonArray array = doc.array();
QList<QString> stringList;
QList<QVariant> variantList = array.toVariantList();
QList<QVariant>::iterator i;
for(i = variantList.begin(); i < variantList.end(); ++i) {
stringList << (*i).toString();
}
setListApplications(stringList);
reply->disconnect();
reply->deleteLater();
}
void NetworkHandler::refresh()
{
NetworkCommunication::getInstance()->getService("http://10.208.162.106:8080/WCFServices/DataService/getvariables");
}
void NetworkHandler::open(const QString &file)
{
NetworkCommunication::getInstance()->sendDataToService("http://10.208.162.106:8080/WCFServices/IOService/open/" + file, "", 1);
}
void NetworkHandler::getApplications()
{
NetworkCommunication::getInstance()->getService("http://10.208.162.106:8080/WCFServices/IOService/getstoredapplications");
}
QList<QString> NetworkHandler::listApplications() const
{
return m_listApplications;
}
void NetworkHandler::setListApplications(const QList<QString> &list)
{
m_listApplications = QList<QString>(list);
emit listApplicationsChanged();
}
的main.cpp
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QScreen *screen = QGuiApplication::screens()[0];
QtQuick2ApplicationViewer viewer;
QRect screenSize = screen->availableGeometry();
QQmlContext *ctxt = viewer.rootContext();
ctxt->setContextProperty("screen", screenSize);
viewer.setMainQmlFile(QStringLiteral("qml/testPortabilite/main.qml"));
viewer.showFullScreen();
NetworkHandler* handler = new NetworkHandler();
ctxt->setContextProperty("handler", handler);
NetworkCommunication::getInstance()->setHandler(handler);
return app.exec();
}
启动列表视图的按钮
ToolButton {
id: open
objectName: "open"
height: menuBar.height
width: height
anchors.right: refresh.left
Image {
height: parent.height
width: height
source: "qrc:/ic_open"
smooth: true
}
onClicked: {
Qt.createComponent("dialog.qml").createObject(root, {});
handler.getApplications();
}
}
最后是listview的qml
import QtQuick 2.0
import QtQuick.Controls 1.1
Item {
id: dialogComponent
anchors.fill: parent
width: screen.width
height: screen.height
// Add a simple animation to fade in the popup
// let the opacity go from 0 to 1 in 400ms
PropertyAnimation {
target: dialogComponent;
property: "opacity";
duration: 400;
from: 0; to: 1;
easing.type: Easing.InOutQuad ;
running: true
}
// This rectange is the a overlay to partially show the parent through it
// and clicking outside of the 'dialog' popup will do 'nothing'
Rectangle {
anchors.fill: parent
id: overlay
color: "#000000"
opacity: 0.6
// add a mouse area so that clicks outside
// the dialog window will not do anything
MouseArea {
anchors.fill: parent
}
}
ListView {
id: listview
width: dialogComponent.width*0.8
height: dialogComponent.height*0.8
anchors.centerIn: parent
Component {
id: rowDelegate
Rectangle {
id: wrapper
width: listview.width
height: 100
Text {
anchors.left: parent.left
anchors.leftMargin: 20
anchors.verticalCenter: parent.verticalCenter
font.pixelSize: 40
// deligate can direclty use ListElement role name
text: modelData
}
}
}
model: handler.list
delegate: rowDelegate
MouseArea {
anchors.fill: parent
onClicked: {
console.log(handler.list);
dialogComponent.destroy()
}
}
}
/*TableView {
id: listView
width: dialogComponent.width*0.8
height: dialogComponent.height*0.8
model: handler.list
alternatingRowColors: true
anchors.centerIn: parent
TableViewColumn {
role: "dataModel"
title: "Element"
}
MouseArea {
anchors.fill: parent
onClicked: {
console.log(handler.list);
dialogComponent.destroy()
}
}
}*/
}
我错过了什么吗?空列表的副本?
请注意console.log(handler.list);
给我一个好的列表,而不是空列表或其他任何内容。
提前致谢。
答案 0 :(得分:0)
显然,QStringList
!= QList<QString>
我认为QStringList特定于打印任务(在listview中显示)。用这个替换所有旧列表解决了这个问题。