QList:内存不足 [英] QList: Out of memory

查看:196
本文介绍了QList:内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Qt编写的用于嵌入式Linux的图形应用程序.此应用程序的一部分是每250毫秒更新一次显示屏幕.但是,大约8到10个小时后,应用程序崩溃,并显示"QList:内存不足"错误.我已经隔离了函数和行(在某种意义上)发生的地方,但是我不知道为什么会发生,因为我没有使用QList.该函数中仅有的活动代码行在此问题的结尾.

I have a graphical application written in Qt for embedded linux. Part of this application is to update a display screen every 250 ms. However, after about 8-10 hours the application crashes with a "QList: Out of memory" error. I've isolated the function and the lines (in a sense) where it happens, but I have no idea why it happens since I'm not using QList. The only active lines of code in this function are at the end of this question.

我意识到QList从未缩小"用于保存项目的内存,但是我没有在代码中的任何地方使用QList.我只调用'setStyleSheet'来设置ui小部件(标签,文本字段等)上的各种字体和属性.还有更多代码,但全部注释掉了,因此我假设它与setStyleSheet有关.有人知道为什么会这样吗?如果是这样,您知道解决此问题的方法吗?我正在使用Q.t. 4.3 btw(由于它是专门加载到我正在使用的嵌入式系统上的).

I realize that QList doesn't ever 'shrink' the memory it uses to hold items, but I'm not using QList anywhere in my code. I'm only calling 'setStyleSheet' to set various fonts and properties on ui widgets (labels, text fields, etc) There's more code, but its all commented out so I'm assuming it has something to do with setStyleSheet. Does anyone know why this is happening? If so, do you know of a way around this? I'm using Q.t. 4.3 btw (due to it being specifically loaded on the embedded system I'm using).

非常感谢您的时间.

if(twc_rx){
        ui->label_Rx->setStyleSheet("QLabel { background-color: lime; font: bold 16px 'Arial' }");
  }else if(!twc_rx){
    ui->label_Rx->setStyleSheet("QLabel { background-color: grey; font: bold 16px 'Arial' }");

  }//line 561 to 684
  if(twc_tx){
   ui->label_Tx->setStyleSheet("QLabel { background-color: lime; font: bold 16px 'Arial' }");
  }else{
   ui->label_Tx->setStyleSheet("QLabel { background-color: grey; font: bold 16px 'Arial' }");
  }if(ats_stat){
       ui->label_ATS->setStyleSheet("QLabel { background-color: lime; border-radius: 10; font: bold 16px 'Arial'}");
  }else{
       ui->label_ATS->setStyleSheet("QLabel { background-color: red; border-radius: 10; font: bold 16px 'Arial'}");
  }
  if(atp_stat){
       ui->label_atp2->setStyleSheet("QLabel { background-color: lime; border-radius: 10; font: bold 16px 'Arial'}");
  }else{
       ui->label_atp2->setStyleSheet("QLabel { background-color: red; border-radius: 10; font: bold 16px 'Arial'}");
  }
  if(ato_stat){
       ui->label_ATO->setStyleSheet("QLabel { background-color: lime; border-radius: 10; font: bold 16px 'Arial'}");

  }else{
       ui->label_ATO->setStyleSheet("QLabel { background-color: red; border-radius: 10; font: bold 16px 'Arial'}");
  }

我应该提到这些行是基于另一个子系统的输入消息每250毫秒执行一次.我已经走了那条路,走到了尽头.这是错误代码.

I should mention that these lines are being executed every 250 ms based on an input message from another subsystem. I've already went down that road and its a dead end. This is the error code.

推荐答案

使用qstylesheets的方式更多地用于静态属性.如果您打算随时更改属性,请查看

The way you are using qstylesheets is more for static properties. If you intend to change properties on the fly, I would look into Customizing Using Dynamic Properties.

使用动态属性自定义

在很多情况下,我们需要提供具有以下内容的表单 必须填写.为了向用户指示该字段是必填字段, 一种有效的(尽管从美学上来说是可疑的)解决方案是使用黄色 作为这些字段的背景色.原来这是非常 易于使用Qt样式表实现.首先,我们将使用 以下应用程序范围的样式表:

Customizing Using Dynamic Properties

There are many situations where we need to present a form that has mandatory fields. To indicate to the user that the field is mandatory, one effective (albeit esthetically dubious) solution is to use yellow as the background color for those fields. It turns out this is very easy to implement using Qt Style Sheets. First, we would use the following application-wide style sheet:

 *[mandatoryField="true"] { background-color: yellow }

这意味着设置了强制字段Qt属性的每个小部件 设为true将会有一个黄色背景.

This means that every widget whose mandatoryField Qt property is set to true would have a yellow background.

然后,对于每个必填字段小部件,我们只需创建一个 必选属性,并将其设置为true.例如:

Then, for each mandatory field widget, we would simply create a mandatoryField property on the fly and set it to true. For example:

 QLineEdit *nameEdit = new QLineEdit(this);
 nameEdit->setProperty("mandatoryField", true);

 QLineEdit *emailEdit = new QLineEdit(this);
 emailEdit->setProperty("mandatoryField", true);

 QSpinBox *ageSpinBox = new QSpinBox(this);
 ageSpinBox->setProperty("mandatoryField", true);

对于频繁交换值似乎更友好. Qt 4.7对此进行了很好的解释,但在Qt 4.3中似乎仍然可用:样式表语法:选择器类型

It appears to be more friendly for swapping values frequently. This is explained well for Qt 4.7, but it appears to still be available in Qt 4.3: The Style Sheet Syntax: Selector Types

因此,基本上,不要一遍又一遍地添加到为您的应用设置的q个样式表的列表中,您应该使某些样式依赖于一个属性,并设置该属性,然后取消设置样式表,然后重新设置它.我相信可以使用 unpolish polish 命令(请参见

So basically, instead of adding to the list of q stylesheets set for your app over and over again, you should make some of the styles dependent on a property, and set that property, then unset the stylesheet and then re-set it. I believe this can be done with the unpolish and polish commands (see Styles and Style Aware Widgets).

以下是使用此技术的示例.有一个带有QPushButton的mainwindow.ui.

Below is an example of using this technique. There is a mainwindow.ui with a QPushButton on it.

main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.h

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimerEvent>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
public slots:
    void timerEvent(QTimerEvent *);

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QString stylesheet =
            "*[mandatoryField=\"true\"] { background-color: yellow }"
            "*[mandatoryField=\"false\"] { background-color: gray }"
            "QPushButton[otherField=\"true\"] { border: none; }"
            "QPushButton[otherField=\"false\"] { border-color: navy; }";
    ui->pushButton->setProperty("mandatoryField", true);
    ui->pushButton->setProperty("otherField", true);
    ui->pushButton->setStyleSheet(stylesheet);

    this->startTimer(1000);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::timerEvent(QTimerEvent *)
{
    static int count = 0;

    if(count % 2)
    {
        bool previousMandatoryFieldValue = ui->pushButton->property("mandatoryField").toBool();
        ui->pushButton->setProperty("mandatoryField", !previousMandatoryFieldValue);
        qDebug() << "mf" << previousMandatoryFieldValue;
    }
    else
    {
        bool previousOtherFieldValue = ui->pushButton->property("otherField").toBool();
        ui->pushButton->setProperty("otherField", !previousOtherFieldValue);
        qDebug() << "of" << previousOtherFieldValue;
    }
    ui->pushButton->style()->unpolish(ui->pushButton);
    ui->pushButton->style()->polish(ui->pushButton);

    this->update();

    count++;
}

这篇关于QList:内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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