Qlist returns Size as -1



  • Hi,

    I am writing the list of portalsearchitems in an xml file. I can extract the info like Title,Owner but the Size returns -1. I need size for computing the space available.

    Code Snippet:
    main.qml

      for (var ii = 0; ii <= varidPortalSearchItemsCount -1 ;ii++)
                    {
                        vartextJson += '{"title":"'+ idPortalSearchItems.results[ii].title +'"
                                        ,"owner":"'+ idPortalSearchItems.results[ii].owner +'"
    
                                        ,"size":"'+ idPortalSearchItems.results[ii].size +'"  //Size is just shown as -1
    
    
                                        }' + ',';
                    }
    

    Thanks In advance.



  • @Mathan-M said in Qlist returns Size as -1:

      for (var ii = 0; ii <= varidPortalSearchItemsCount -1 ;ii++)
    

    This looks odd to me: why don't you use

    for ( var ii = 0; ii < varidPortalSearchItemsCount ; ++ii )
    


  • ok.

    I am accessing the value of Object.

    idPortalSearchItems.results[ii].size

    In the object, just it store only -1 value.


  • Qt Champions 2016

    Hi
    should it not be

    • idPortalSearchItems.results[ii].size() +

    as its a function call?

    Also, QList cannot return -1 as far as i know.
    it returns 0 for empty list.



  • Hi!
    I was curious if this might be some JavaScript side effect with "size" being some predefined property or something like that. So, I've build a minimal example and no, no problems, works as expected for me. Can you verify that?

    dataobject.h

    #ifndef DATAOBJECT_H
    #define DATAOBJECT_H
    
    #include <QObject>
    #include <QString>
    
    
    class DataObject : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
        Q_PROPERTY(int size READ size WRITE setSize NOTIFY sizeChanged)
    public:
        explicit DataObject(const QString &t = QString(), int s = 0, QObject *parent = 0);
        QString title() const { return m_title; }
        void setTitle(const QString &t) {
            if (t == m_title)
                return;
            m_title = t;
            emit titleChanged(t);
        }
        int size() const { return m_size; }
        void setSize(int s) {
            if (s == m_size)
                return;
            m_size = s;
            emit sizeChanged(s);
        }
    
    signals:
        void titleChanged(QString);
        void sizeChanged(int);
    private:
        QString m_title;
        int m_size;
    };
    
    #endif // DATAOBJECT_H
    

    dataobject.cpp

    #include "dataobject.h"
    
    DataObject::DataObject(const QString &t, int s, QObject *parent)
        : QObject(parent)
        , m_title(t)
        , m_size(s)
    {
    }
    

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QList>
    #include <QObject>
    #include <QQmlContext>
    
    #include "dataobject.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
    
        QList<QObject*> dataList;
        dataList << new DataObject("Cheyenne", 23);
        dataList << new DataObject("Greeneville", 42);
        dataList << new DataObject("Columbia", 666);
    
        QQmlApplicationEngine engine;
        engine.rootContext()->setContextProperty("dataModel", QVariant::fromValue(dataList));
        engine.load(QUrl(QLatin1String("qrc:/main.qml")));
    
        return app.exec();
    }
    

    main.qml

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.0
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Text {
            id: txt
            anchors.fill: parent
        }
        Button {
            id: btn
            text: "Click me"
            anchors.bottom: parent.bottom
            anchors.left: parent.left
            onClicked: {
                for (var i=0; i<dataModel.length; ++i)
                    txt.text += dataModel[i].title + " / " + dataModel[i].size + "\n";
            }
        }
    }
    

  • Moderators

    @Mathan-M You should also post other relevant information like what is results ? What does it return ?



  • Hi p3c0,

    The results will back the portalItems information. If the portal consits of document, It will brings the type,name,owner,size and other relevant information and same applies to map like map type,name,owner,size. The object returns from the portal is perfect except the field Size. It just returning -1. I assume, the problem is not with server.

    Thanks In advance.


  • Moderators

    @Mathan-M What is document ? What is its datatype ? What are type,name,owner,size in that document ? Is it a C++ or Javascript object ?



  • Is result of type QList<QObject*>?



  • yes, the return type will be Qlist



  • @Mathan-M Ok, then have a look at my code above; that works as expected, what's different to your code?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.