显示,更改数据并在qml中保留QQmlListProperty [英] Show, change data and keep QQmlListProperty in qml

查看:758
本文介绍了显示,更改数据并在qml中保留QQmlListProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去我解决了一个问题与QQmlListProperty暴露c ++ qlist到qml,问题解决了,现在我再次尝试使用他们的解决方案,但我不能。
第一次每次都暴露给qml的列表不同,这个时候没有,我在主类中有一个Qlist,她的许多数据都是从db加载的,而其他数据在运行时更改。

in the past i resolved a problem with QQmlListProperty for expose c++ qlist to qml, and the problem was resolved, now i try use their solution again, but i can't. the first time the list exposed to qml every time are different, this time no, i have a Qlist in a main class, and many data from her are loaded from db, and others changed in runtime.

当应用程序启动时,加载数据并暴露给Qml是确定,我需要更改页面(gui),并保持数据存储在程序中,可能使用其中的许多数据其他guis(作为文本),每次我改变页面(使用加载器元素)程序崩溃,我相信当qml中的指针的方向改变,当c ++类的数据复制到qi1中的gui。
我的代码来自QmlListproperty类和方法在qml中加载数据,并从主qml复制到页面。

When the application start, the load of data and exposed to Qml is ok, i need change of page(gui) and keep the data stored in program and probably used many of their data in other guis (as text), and every time i change of page (with loader element) the program crashes, i believe a directions of pointers are changed in qml when the data from c++ class is copied to gui in qml. My Code from QmlListproperty class and method of Load data in qml, and copy from the main qml to page.

类ListBombsUi.h的定义



definition of class ListBombsUi.h"

 #include "StructBombUi.h"

 class ListBombsUi: public QObject{
   Q_OBJECT
   Q_PROPERTY(QQmlListProperty<CStructBombUi>  bombsUi READ bombsUiList NOTIFY      bombsUiChanged);
   Q_CLASSINFO("DefaultProperty", "bombsUi");
  public:
   ListBombsUi(QObject *parent=0);
   QQmlListProperty<CStructBombUi> bombsUiList(); 

   static void appendBombUi(QQmlListProperty<CStructBombUi> *list, CStructBombUi *pdt);
   static void clear(QQmlListProperty<CStructBombUi> *property);
   static int listSize(QQmlListProperty<CStructBombUi> *property);
   static CStructBombUi *bombUiAt(QQmlListProperty<CStructBombUi> *property, int index);

   void addBomb(CStructBombUi *bombUi);
   Q_INVOKABLE int size();
   void Q_INVOKABLE getMemory();
   Q_INVOKABLE void clearList();
   CStructBombUi* getValue(int index) const;

   QList<CStructBombUi *> copyList();

  signals:
   void bombsUiChanged();

  public:
   int lastAdded;

  private:
   CStructBombUi *item;

   QList<CStructBombUi*> m_BombsUi;

我注册如何qmltype

i register how qmltype

qmlRegisterType<ListBombsUi>("BombsListUiModel", 1, 0, "ListBombsUi");

ListBombsUi的主要方法定义

definition of main methods of ListBombsUi

ListBombsUi::ListBombsUi(QObject *parent):QObject(parent)
{}

QQmlListProperty<CStructBombUi> ListBombsUi::bombsUiList() 
{
   return QQmlListProperty<CStructBombUi>(this, &m_BombsUi, &ListBombsUi::appendBombUi,
                 &ListBombsUi::listSize,
                 &ListBombsUi::bombUiAt,
                 &ListBombsUi::clear);

   emit bombsUiChanged();
}


 void ListBombsUi::appendBombUi(QQmlListProperty<CStructBombUi> *list, CStructBombUi *pdt)
 {
    ListBombsUi *bombsList= qobject_cast<ListBombsUi *>(list->object);

    if(bombsList)   {
      pdt->setParent(bombsList);
      bombsList->m_BombsUi.append(pdt);
      bombsList->bombsUiChanged();
    }
 }


  void ListBombsUi::clearList()
  {
    m_BombsUi.clear();
    emit bombsUiChanged();
  }

  void ListBombsUi::addBomb(CStructBombUi *bombUi)
  {
    m_BombsUi.append(bombUi);
    emit bombsUiChanged();
  }

在main类中定义两个数据,我使用第一个作为aux,可以直接使用第二个(他们是我的想法originly)

definition in main class for two data, i use the first as aux, but is possible use the second directly(their are my idea originallly)

QList<CStructBombUi * > listBombs;
ListBombsUi *listBufferBombs;  

我将一个listBufferBombs分配给qml中的元素

i assign a listBufferBombs to element in qml

listBufferBombs = mainObject->findChild<ListBombsUi*>("listGralBombs");

将数据公开给qml的方法

method for expose data to qml

 void EgasWindowWork::setDataOfBombsInModels()//, const QList <CEstrucBombUi> *const    dataArrayBombs)
 { 

   if( (mainObject) ) {

      CStructBombUi e,k,j;

      e.initializing(QStringList()<<"text1"<<"text2"<<"text3");
      e.update(QString("15"), QString("1"), QString("1"), QString("1"),QString("1"));

      k.initializing(QStringList()<<"text1"<<"text2");
      k.update(QString("2"), QString("2"), QString("2"), QString("2"),QString("2"));

      listBombs.append(&e);
      listBombs.append(&k);

      for(qint16 i=0; i<listBombs.size() ; i++)
         {  listBufferBombs->addBomb( listBombs.value(i) );   }

      QMetaObject::invokeMethod(mainObject, "setDataOfModelBombs"); //this method copy the list located in main qml for page
 }

metod main.qml文件

the metod in main.qml file

function setDataOfModelBombs()
{  
    if(itemOfPageLoaded)   {   
        itemOfPageLoaded.listBombs=listed
        itemOfPageLoaded.setDataOfBombs() //this function put every data inside the every field of qml gui element
    }
 }

在main.qml文件中声明loader元素

declaration of loader element in main.qml file

Loader{
    id: pageLoader
    focus: true
    //anchors.horizontalCenter: parent.horizontalCenter
    width: parent.width
    height: parent.height-50
    anchors.top: headerIcons.bottom
    objectName: "switchPages"
    z: 2
    source: "BombsUi.qml"
    property bool valid: item !== null

    Binding {
        target: pageLoader.item
        property: "numberBombs"
        value: numberOfBombs
        when: pageLoader.status == Loader.Ready
    }

}



method in qml file loaded as page

function setDataOfBombs()
{
    var size=newList.size()
    arrayBombs.model=size //i use a repater container, and model is the number of elements loaded

    if(size>0) {
        for(var i=0; i<size; i++) {
        //exactly in the next line the error happens
            arrayBombs.itemAt(i).priceText=newList.bombsUi[i].ultimateProductPrice
            arrayBombs.itemAt(i).volumeText = newList.bombsUi[i].ultimateProductVolume
            //exist mor date, but with this are sufficient
         }
     }
 }

这里是元素repeate-grid的声明

and here is declaration of element repeater-grid

   Item{
    width: parent.width; height: parent.height

    Item{
        id: bombsArea
        height: parent.height; width: parent.width*0.7
        anchors.leftMargin: 10
        y: 3
        x: 2
        Grid{
            id: gridOfBombsModel
            width: parent.width; height: parent.height
            spacing: 7
            rows: 6; columns: Math.round((parent.width/165))
            Repeater{
                id: arrayBombs

                BombModel{
                    numberBomb: (index + 1)
                    MouseArea{
                        anchors.fill: parent
                        onClicked:{
                            parent.borderProperties.color="red"
                            parent.borderProperties.width=1.5
                        }
                    }
                }
            }
        }
    }

程序加载页面1确定,但是我更改页面n并返回到页面1,崩溃。我接受替代方案,感谢您的帮助。

the program load the page 1 ok, but i change a page "n" and return to page 1, crashes. i accept alternatives to, Thanks for your help.

推荐答案

嗯,我发现两个东西,失败的构造函数CStructBombsUi和i descart使用辅助列表和使用CStructBombsUi *专用,并更改qml文件中的函数,我显示更改。

Well, i detected two things, fail in constructor of CStructBombsUi and i descart use of auxiliar list, and the use of CStructBombsUi* exclusively, and change the function in qml file, i show the changes.

main.qml中的更改

Changes in main.qml

function setDataOfModelBombs()
{
    if(itemOfPageLoaded) {
        itemOfPageLoaded.numberBombs=listBombs.size()
          itemOfPageLoaded.setDataOfBombs(listBombs.bombsUi)
    }
}

function setDataOfBombs(bombsUi)
{
   if(numberBombs>0) {
   for(var i=0; i<numberBombs; i++)
        {
            arrayBombs.itemAt(i).priceText=bombsUi[i].ultimateProductPrice
            arrayBombs.itemAt(i).volumeText = bombsUi[i].ultimateProductVolume
            arrayBombs.itemAt(i).amountText= bombsUi[i].getUltimateProductMount()
            arrayBombs.itemAt(i).fuelText= bombsUi[i].getUltimateProductName()

            if(bombsUi[i].getFuels())
                {arrayBombs.itemAt(i).setListOfFuelsInBomb( bombsUi[i].getFuels() ) }
         }
   }
}

下一行不存在更多

QList<CStructBombUi* > listBombs;

每一个都是一个指针

CStructBombUi *e=new CStructBombUi();

我不使用值列表,这样调整程序不会崩溃

i don't use a list of values, with this adjusts the program don't crashes

这篇关于显示,更改数据并在qml中保留QQmlListProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆