2

組み込みLinux用にQtで書かれたグラフィカルアプリケーションがあります。このアプリケーションの一部は、250ミリ秒ごとに表示画面を更新することです。ただし、約8〜10時間後、アプリケーションは「QList:メモリ不足」エラーでクラッシュします。関数とそれが発生する行を(ある意味で)分離しましたが、QListを使用していないため、なぜ発生するのかわかりません。この関数のアクティブなコード行は、この質問の最後にあります。

QListは、アイテムを保持するために使用するメモリを「縮小」しないことを認識していますが、コード内のどこにもQListを使用していません。uiウィジェット(ラベル、テキストフィールドなど)にさまざまなフォントとプロパティを設定するために「setStyleSheet」を呼び出すだけです。コードは他にもありますが、すべてコメント化されているため、setStyleSheetと関係があると思います。なぜこれが起こっているのか誰かが知っていますか?もしそうなら、あなたはこれを回避する方法を知っていますか?Qt 4.3 btwを使用しています(使用している組み込みシステムに特にロードされているため)。

お時間をいただき、誠にありがとうございます。

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ミリ秒ごとに実行されていることに注意してください。私はすでにその道を進み、行き止まりになっています。これはエラーコードです。

4

1 に答える 1

2

qstylesheetsを使用する方法は、静的プロパティ用です。プロパティをその場で変更する場合は、動的プロパティを使用したカスタマイズを検討します。

動的プロパティを使用したカスタマイズ

必須フィールドを持つフォームを提示する必要がある状況はたくさんあります。フィールドが必須であることをユーザーに示すための1つの効果的な(審美的に疑わしいとはいえ)解決策は、これらのフィールドの背景色として黄色を使用することです。これは、Qtスタイルシートを使用して実装するのが非常に簡単であることがわかりました。まず、次のアプリケーション全体のスタイルシートを使用します。

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

これは、mandatoryFieldQtプロパティがtrueに設定されているすべてのウィジェットの背景が黄色になることを意味します。

次に、必須フィールドウィジェットごとに、その場でmandativeFieldプロパティを作成し、それをtrueに設定します。例えば:

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

値を頻繁に交換する方が使いやすいようです。これはQt4.7でよく説明されていますが、Qt 4.3:スタイルシートの構文:セレクタータイプでも引き続き使用できるようです。

したがって、基本的に、アプリに設定されたqスタイルシートのリストに何度も追加するのではなく、一部のスタイルをプロパティに依存させ、そのプロパティを設定してから、スタイルシートの設定を解除してから再設定する必要があります。unpolishこれはとpolishコマンドで実行できると思います(スタイルとスタイル対応ウィジェットを参照)。

編集:以下は、この手法の使用例です。QPushButtonが設定されたmainwindow.uiがあります。

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

#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

#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++;
}
于 2012-11-26T16:15:42.847 に答える