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 的所有 QLeds 上

我的理论和方法不正确还是我可以用其他方式做?我一步一步地写了我的问题我改变了 dialog.cpp 和 cunstructor Dialog::Dialog(QWidget *parent) : 和 *.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

** 对话框.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 文件夹文件:qledplugin.dll、libqledplugin.a、moc_qled.cpp、moc_qledplugin.cpp、qrc_qled.cpp、moc_qled.o、moc_qledplugin.o、qled.o、qledplugin.o、qrc_qled。哦,

至:

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 对话框并放置 QLeds 和 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

所以我可以看到 formWidget 子项,其中包含我的自定义小部件的 QLeds。

现在我需要将数据发送到我的 QLeds 和 QCheckBoxs,我制作了一个 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() 为零,我得到空指针

如果我添加带有编码的 QLed 小部件,我没有问题,但我需要 QtDesigner。

我在 Q3Frame 上对此进行了测试,它是 QtDesigner 中的一个自定义小部件插件,并且存在同样的问题。

为什么会发生这种情况,我该怎么办?Qt不能这样做吗?

提前谢谢干杯

4

0 回答 0