Question

I am currently trying to create a listview based on a C++ QStringList(QList<QString>). The problem here, is that the list does not show up, neither with a pre-filled list nor with with my updated list. The same append with a tableView

Here is some code :

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();
}

The button launching the listview

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();
    }
}

and finally the qml of the listview

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()
          }
      }
  }*/
}

Am I missing something ? A copy of an empty list ?

Note that console.log(handler.list);give me a good list, not an empty list or anything else.

Thanks in advance.

Was it helpful?

Solution

Apparently, QStringList != QList<QString>

I think that QStringList is specific for printing tasks (as display in a listview). Replacing all the old lists by this solved the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top