Ui file loaded by UiLoader , but NULL pointer to custom widgets
i am using Qt 4.7.0 and windows7, i have a Huge problem with custom widgets, i founded QLed custom widget, i use it.
i want to add a signal properties to QLed widget and in design time with QtDesigner i want to add 100 QLed's on my Ui file and set each QLed's signal properties to a number(type of signal properties is not important), so i want to access this 100 QLeds when i am coding and make a QList and access those
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 *>();
now i want to send data to 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);
}
now it will ON all QLeds that SIGNAL_PROPERTIES == 123
is my theory and metod incorect or i can do in other way? i wrote my problem step by step i changed dialog.cpp and cunstructor Dialog::Dialog(QWidget *parent) : and *.pro file i attached my test program. Thanks.
*.pro file:
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;
//---------------------------------------------------------------
}
i copied qledplugin-build-desktop\release folder files: 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.o,
to:
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
, i can design a Ui dialog with QtDesigner and placing QLeds and QLabels, every thing is good, and i also can use other widgets. after it, i want to show this Ui file in my program, i am using UiLoader, i load Ui file in a QWidget object QWidget *formWidget = loader.load(&file, this); and .show() it, in compile time i get this warnings in 1)Build Issues tab:
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
but my program work. every thing is good, and my designed file is loading. i use this command:
qDebug() << formWidget->children();
and i can see in 3) Application Output tab:
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
so i can see formWidget children, that containes QLeds my Custom Widgets.
now i need to send data to my QLeds and QCheckBoxs, i make a QList and try it:
QList<QCheckBox *> allCheckBox = formWidget->findChildren<QCheckBox *>();
qDebug() << allCheckBox;
qDebug() << allCheckBox.size();
allCheckBo开发者_StackOverflow中文版x.at(2)->setChecked(true); // i can send data
QCheckBox *hello1=formWidget->findChild<QCheckBox*>("checkBox_2");
qDebug() << hello1;
hello1->setChecked(true);// i can send data
i get:
(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")
it showes 4 QCheckBox
now i try Custom Widget:
QList<QLed *> allQLed = formWidget->findChildren<QLed *>();
qDebug() << allQLed;
qDebug() << allQLed.size();
i get:
()
0
QObject(0x0) //null pointer
my HUGE problem is here: allQLed.size() is zero and i get null pointers
if i add QLed widget with coding i have no problem BUT i need QtDesigner.
i tested this on Q3Frame it is a custom widget plugin in QtDesigner and same problem.
why this happend and what can i do? Qt cant do this?
thanks in advance cheers
精彩评论