找回密码
 立即注册
收起左侧

用setcontextproperty重置gridview的model,运行多次后程序崩溃

0
回复
7883
查看
[复制链接]
累计签到:1 天
连续签到:1 天
来源: 2017-12-18 22:34:17 显示全部楼层 |阅读模式
3Qter豆
本帖最后由 inanity 于 2017-12-18 22:34 编辑

主要想实现的功能是:界面中用gridview画视图,gridview的model数据是由QQmlContext::setcontextproperty加载的QList<QOBJECT*>。但修改QList<QOBJECT*>中元素后,用setcontextproperty重置模型时,程序会崩溃。
有几个问题:
1.程序是用setcontextproperty重置模型后几次才崩溃的,也就是能运行N次setcontextproperty,界面也能显示出QList中的数据。
2.每次setcontextproperty后,程序内存会涨2-3M左右,而且内存值不会往下掉(也就是说setcontextproperty会申请一些空间,还不会释放)。
3.qt creator的debug模式是不是有内存限制,内存涨到500M左右有些要用内存操作就会保存,比如加载图片用的QImage。4.是否用继承QAbstractListModel比使用QList来加载Model数据好些。

以下是简化过的部分代码:
main.qml
  1. GridView{
  2.             id:gridview
  3.             ...
  4.             model: templist
  5.             delegate: gridview_component
  6.         }
  7.     }
  8.     Component{
  9.         id:gridview_component
  10.         Item{
  11.             ...
  12.             Rectangle{
  13.                 ...
  14.                 Text {
  15.                     id: text11
  16.                     ...
  17.                     text: text1
  18.                 }
  19.                 Text {
  20.                     id: text22
  21.                     ...
  22.                     text: text2
  23.                 }
  24.                Text {
  25.                     id: text33
  26.                     ...
  27.                     text: text3
  28.                 }
  29.       }
  30. }
复制代码
main.cpp:
temp是QList<QObect*>
  1. int main(int argc, char *argv[])
  2. {
  3.     QGuiApplication app(argc, argv);
  4.     QQmlApplicationEngine engine;
  5.     QQmlContext* context=engine.rootContext();
  6.     Midware *midware= new Midware();
  7.     midware->setContext(context);
  8.     context->setContextProperty("templist",QVariant::fromValue(midware->temp));
  9.     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
  10.     return app.exec();
  11. }
复制代码

Midware类包含一个QTcpsocket,其中
  1.     connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(readMsg()));
复制代码
readMsg中...是省略的接收消息的代码,消息不完整不会进入下面代码,会直接return 出去。
temp是QList<QObect*>
  1. void Midware::readMsg()
  2. {
  3.     while(true)
  4.     {
  5.         ...
  6.         qDeleteAll(temp);
  7.         temp.clear();
  8.         qsrand(time(0));
  9.         int max=qrand()%16+1;
  10.         for(int i=0;i<max;i++)
  11.         {
  12.             Data* dataTemp=new Data();
  13.             temp.append(dataTemp);
  14.         }
  15.         context->setContextProperty("templist"    ,nullptr);
  16.         context->setContextProperty("templist"    , QVariant::fromValue(temp));
  17.     }
  18. }
复制代码
加这句有时能跑的时间长点,不知道是不是那几次内存没涨到限制值。
  1. context->setContextProperty("templist"    ,nullptr);
复制代码

Data类:
  1. class Data :public QObject
  2. {
  3.     Q_OBJECT
  4.     Q_PROPERTY(QString text1 READ read_text1 CONSTANT)
  5.     Q_PROPERTY(QString text2 READ read_text2 CONSTANT)
  6.     Q_PROPERTY(QString text3 READ read_text3 CONSTANT)
  7. private:
  8.     QString text1;
  9.     QString text2;
  10.     QString text3;
  11. public:
  12.     Data(){ text1="text1"; text2="text2"; text3="text3";}
  13.     ~Data();
  14.     QString read_text1() const {   return this->text1;    }
  15.     QString read_text2() const {   return this->text2;    }
  16.     QString read_text3() const {   return this->text3;    }
  17. };
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了