1

Qt 4.7.0 と windows7 を使用しています。カスタム ウィジェットに大きな問題があります。QLed カスタム ウィジェットを作成し、使用しています。

QLed ウィジェットにシグナル プロパティを追加し、QtDesigner を使用して設計時に、Ui ファイルに 100 個の QLed を追加し、各 QLed のシグナル プロパティを数値に設定したい (シグナル プロパティのタイプは重要ではない) ため、アクセスしたいこの 100 個の QLed は、私がコーディングして QList を作成し、それらにアクセスするときに

QFile file(":/test.ui");
QUiLoader loader;
file.open(QFile::ReadOnly);
QWidget *formWidget = loader.load(&file, this);
file.close();
formWidget->show();
QList<QLed *> allQLed = formWidget->findChildren<QLed *>();

QLeds にデータを送信したい

for(int i=0; i<allQLed.size(); i++){
    if(allQLed.at(i)->SIGNAL_PROPERTIES == 123)
        allQLed.at(i)->setValue(true);
    else    
        allQLed.at(i)->setValue(false);
}

これで、SIGNAL_PROPERTIES == 123 であるすべての QLed がオンになります。

私の理論と方法は間違っていますか、それとも他の方法でできますか? 問題を段階的に書き、dialog.cpp と cunstructor Dialog::Dialog(QWidget *parent) を変更し、*.pro ファイルをテスト プログラムに添付しました。ありがとう。

*.pro ファイル:

TARGET = example
TEMPLATE = app

QT += svg

CONFIG      += uitools

SOURCES += main.cpp\
        dialog.cpp\

HEADERS  += dialog.h\
            qled.h

FORMS    += dialog.ui

LIBS += libqledplugin

RESOURCES = test.qrc

** dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

#include <QFileDialog>
#include <QtUiTools>
#include "QLed.h"

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

//---------------------------------------------------------------
        QFile file(":/test.ui");
        QUiLoader loader;
        file.open(QFile::ReadOnly);
        QWidget *formWidget = loader.load(&file, this);
        file.close();
        formWidget->show();
        qDebug() << formWidget->children();

        QList<QCheckBox *> allCheckBox = formWidget->findChildren<QCheckBox *>();
        qDebug() << allCheckBox;
        qDebug() << allCheckBox.size();
        allCheckBox.at(2)->setChecked(true);
        QCheckBox *hello1 = formWidget->findChild<QCheckBox *>("checkBox_2");
        qDebug() << hello1;
        hello1->setChecked(true);

        QList<QLed *> allQLed = formWidget->findChildren<QLed *>();
        qDebug() << allQLed;
        qDebug() << allQLed.size();
        QLed *led = formWidget->findChild<QLed *>("qLed");
        qDebug() << led;
//---------------------------------------------------------------
}

qledplugin-build-desktop\release フォルダー ファイルをコピーしました。お、

に:

1) C:\QledTEST\my test program\mytest-build-desktop
2) C:\QledTEST\my test program\mytest-build-desktop\release
3) C:\QledTEST\my test program\mytest-build-desktop\debug
4) C:\Qt\2010.05\qt\include\QtGui
5) C:\Qt\2010.05\qt\include\Qt
6) C:\Qt\2010.05\qt\lib
7) C:\Qt\2010.05\qt\plugins\designer

、QtDesigner を使用して Ui ダイアログを設計し、QLed と QLabels を配置できます。すべてが適切であり、他のウィジェットも使用できます。その後、プログラムでこの Ui ファイルを表示したいので、UiLoader を使用しています。QWidget オブジェクトに Ui ファイルをロードします QWidget *formWidget = loader.load(&file, this); および .show() それ、コンパイル時に、1) [ビルドの問題] タブで次の警告が表示されます。

release/moc_qled.cpp:75: warning: 'QLed::staticMetaObject' redeclared without dllimport attribute after being referenced with dll linkage
release/moc_qled.cpp:84: warning: 'virtual const QMetaObject* QLed::metaObject() const' redeclared without dllimport attribute: previous dllimport ignored
release/moc_qled.cpp:89: warning: 'virtual void* QLed::qt_metacast(const char*)' redeclared without dllimport attribute: previous dllimport ignored
release/moc_qled.cpp:97: warning: 'virtual int QLed::qt_metacall(QMetaObject::Call, int, void**)' redeclared without dllimport attribute: previous dllimport ignored

しかし、私のプログラムの仕事。すべてが順調で、設計したファイルがロードされています。私はこのコマンドを使用します:

qDebug() << formWidget->children();

そして私は3)アプリケーション出力タブで見ることができます:

Starting C:\QledTEST\my test program\mytest-build-desktop\release\example.exe...
(QFormInternal::TranslationWatcher(0x14683d8) ,  QLed(0x1472320, name = "qLed_2") ,  QLed(0x14722a0, name = "qLed_3") ,  QLed(0x14723a0, name = "qLed_4") ,  QCheckBox(0x14810b8, name = "checkBox_2") ,  QCheckBox(0x14810d8, name = "checkBox_3") ,  QCheckBox(0x14810f8, name = "checkBox_4") ,  QLabel(0x1481118, name = "label") ,  QCheckBox(0x1481138, name = "checkBox") ,  QLed(0x14724e0, name = "qLed") )  
C:\QledTEST\my test program\mytest-build-desktop\release\example.exe exited with code 0

そのため、カスタム ウィジェットの QLed を含む formWidget の子が表示されます。

QLed と QCheckBox にデータを送信する必要があるので、QList を作成して試してみます。

QList<QCheckBox *> allCheckBox = formWidget->findChildren<QCheckBox *>();
qDebug() << allCheckBox;
qDebug() << allCheckBox.size();
allCheckBox.at(2)->setChecked(true); // i can send data
QCheckBox *hello1=formWidget->findChild<QCheckBox*>("checkBox_2");
qDebug() << hello1;
hello1->setChecked(true);// i can send data

私は得る:

(QCheckBox(0x14e10b8, name = "checkBox_2") ,  QCheckBox(0x14e10d8, name = "checkBox_3") ,  QCheckBox(0x14e10f8, name = "checkBox_4") ,  QCheckBox(0x14e1138, name = "checkBox") ) 
 4
QCheckBox(0x6f10b8, name = "checkBox_2")

それは4つのQCheckBoxを示しています

今私はカスタムウィジェットを試します:

QList<QLed *> allQLed = formWidget->findChildren<QLed *>();
    qDebug() << allQLed;
    qDebug() << allQLed.size();

私は得る:

() 
0 
QObject(0x0) //null pointer

私の大きな問題はここにあります: allQLed.size() はゼロで、null ポインターを取得します

コーディングで QLed ウィジェットを追加しても問題はありませんが、QtDesigner が必要です。

Q3Frame でこれをテストしました。これは QtDesigner のカスタム ウィジェット プラグインであり、同じ問題です。

なぜこれが起こったのか、どうすればよいですか?Qtはこれを行うことができませんか?

よろしくお願いします

4

0 に答える 0