首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >QList:内存不足

QList:内存不足
EN

Stack Overflow用户
提问于 2012-11-26 20:25:55
回答 1查看 1.4K关注 0票数 2

我有一个用Qt为嵌入式linux编写的图形应用程序。这个应用程序的一部分是每250ms更新一次显示屏。但是,在大约8-10小时后,应用程序崩溃,并出现"QList: Out of memory“错误。我已经隔离了函数和代码行(在某种意义上),但我不知道为什么会发生这种情况,因为我没有使用QList。此函数中唯一活动的代码行位于此问题的末尾。

我意识到QList不会“收缩”它用来保存项的内存,但我在代码中并没有使用QList。我只是调用' setStyleSheet‘来设置ui小部件(标签,文本字段等)的各种字体和属性。还有更多的代码,但它们都被注释掉了,所以我假设它与setStyleSheet有关。有人知道为什么会这样吗?如果是这样的话,你知道解决这个问题的方法吗?我用的是Q.t。4.3 btw (因为它是专门加载到我使用的嵌入式系统上的)。

非常感谢您的宝贵时间。

代码语言:javascript
运行
复制
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'}");
  }

编辑:

我应该提一下,这些行是根据来自另一个子系统的输入消息每隔250ms执行一次的。我已经走上了这条路,这是一条死胡同。这是错误代码。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-27 00:15:43

您使用qstylesheets的方式更多的是针对静态属性。如果您打算在运行中更改属性,我将研究一下Customizing Using Dynamic Properties

使用动态属性进行自定义

在很多情况下,我们需要提供一个包含必填字段的表单。要向用户表明该字段是必填字段,一种有效的解决方案(尽管在美学上有问题)是使用黄色作为这些字段的背景颜色。事实证明,使用Qt样式表很容易实现这一点。首先,我们将使用以下应用程序范围的样式表:

*mandatoryField="true“{背景色:黄色}

这意味着每个mandatoryField Qt属性设置为true的小部件都会有一个黄色背景。

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

QLineEdit *nameEdit =新建QLineEdit(this);nameEdit->setProperty("mandatoryField",true);QLineEdit *emailEdit =新建QLineEdit(this);emailEdit->setProperty("mandatoryField",true);QSpinBox *ageSpinBox = new QSpinBox(this);ageSpinBox->setProperty("mandatoryField",true);

对于频繁的值交换,它似乎更友好。这在Qt4.7中解释得很好,但在Qt4.3中似乎仍然可用:The Style Sheet Syntax: Selector Types

因此,基本上,您应该让一些样式依赖于某个属性,并设置该属性,然后取消设置样式表,然后重新设置它,而不是一次又一次地添加到应用程序的Q样式表列表中。我相信这可以通过unpolishpolish命令(参见Styles and Style Aware Widgets)来完成。

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

main.cpp

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
#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++;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13564823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档