qt - QList:内存不足

标签 qt out-of-memory qlist

我有一个用 Qt 编写的嵌入式 Linux 图形应用程序。该应用程序的一部分是每 250 毫秒更新一次显示屏幕。然而,大约 8-10 小时后,应用程序崩溃并出现“QList:内存不足”错误。我已经隔离了函数和它发生的行(在某种意义上),但我不知道为什么会发生它,因为我没有使用 QList。该函数中唯一有效的代码行位于该问题的末尾。

我意识到 QList 永远不会“收缩”它用来保存项目的内存,但我没有在代码中的任何地方使用 QList。我只是调用“setStyleSheet”来设置 ui 小部件(标签、文本字段等)上的各种字体和属性。还有更多代码,但所有代码都被注释掉了,所以我假设它与 setStyleSheet 有关。有谁知道为什么会发生这种情况?如果是这样,您知道解决这个问题的方法吗?我正在使用 Q.t。 4.3 顺便说一句(因为它是专门加载在我正在使用的嵌入式系统上的)。

非常感谢您的宝贵时间。

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 毫秒执行一次。我已经沿着那条路走下去了,那是一条死胡同。这是错误代码。

最佳答案

您使用 qstylesheets 的方式更多地用于静态属性。如果您打算即时更改属性,我会查看 Customizing Using Dynamic Properties .

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 }

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

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 中仍然可用:The Style Sheet Syntax: Selector Types

所以基本上,您应该使一些样式依赖于某个属性,并设置该属性,然后取消设置样式表,然后重新设置,而不是一遍又一遍地添加到为您的应用程序设置的 q 样式表列表中它。我相信这可以通过 unpolish 来完成和 polish命令(请参阅 Styles and Style Aware Widgets )。

编辑:下面是使用此技术的示例。有一个 mainwindow.ui,上面有一个 QPushButton。

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

主窗口.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

主窗口.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++;
}

关于qt - QList:内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13564823/

相关文章:

c++ - Ghostscript api 请求 "press <return> to continue"

c++ - Qt 必须在 QWidget 之前构造 QApplication

c++ - QtOpengl的演变

java - 如何修复 apache ignite 2.7.0 的 OOM 异常?

c++ - 将 QList<T> 保存到文件中?

c++ - QList 作为函数参数 - 链接错误 - LNK2019

c++ - 递归禁用子部件鼠标滚轮事件

java - 如何使用 Apache POI 加载大型 xlsx 文件?

android - 内存不足错误 : Using a image which is 1080*1920 do the splash page

c++ - 如何删除列出的 "QGraphicsPathItem"对象以控制进程内存使用?