PDA

View Full Version : Ui file loaded by UiLoader , but NULL pointer to custom widgets



serjik
16th May 2011, 15:49
Hello, i am using Qt 4.7.0 and windows7,
i have a Huge problem with custom widgets, i founded QLed custom widget,from:
http://qt-apps.org/content/show.php?content=72482
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 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 in ADVANCE.
//__________________________________________________ _________
//__________________________________________________ _________
//__________________________________________________ _________


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

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.

why this happend and what can i do?
Qt cant do this?

thanks in advance
cheers

high_flyer
16th May 2011, 16:19
Are you QLed objects direct children of the form, or do they have a parent which it self is a child of the form?

serjik
16th May 2011, 16:35
thanks high_flyer for reply, i think those are direct children,
and my problem is still my previous post here:

http://www.qtcentre.org/threads/41233-How-i-can-access-my-custom-plugin-when-i-loaded-a-ui-file-my-program-crashed

so please download "my test program.zip‎" and check it, thanks in advance.

high_flyer
16th May 2011, 16:57
i think those are direct children,
"think" is not good enough.
You have to be sure.
I am sorry, but I have no possibility (here) to download and check your code.

serjik
16th May 2011, 17:09
ok, in QtDesigner i add four QLed widgets to a form, so i think its direct child, i dont use a QLed in a QFrame.

high_flyer
16th May 2011, 17:37
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

It looks you have problem with your QLed library export/import, either the QLed class it self or the plugin.
Did you define the export macro when building the QLed plugin?

serjik
16th May 2011, 18:12
it is custom widget plugin for QtDesigner and contains Q_EXPORT_PLUGIN2(customwidgetplugin, QLedPlugin) .

high_flyer
17th May 2011, 09:50
you didn't answer my question, did you understand what I asked?

serjik
17th May 2011, 10:15
QLed is a custom widget in QLed.h file exist :
class QDESIGNER_WIDGET_EXPORT QLed : public QWidget
Is this your answer !!??

high_flyer
17th May 2011, 10:28
Partly.
This define either will expand to __declspec( dllimport ) or __declspec( dllexport ), depending if you define the define variable in your plugin project.
In your plugin you should have a header with something like:


#if defined(FOO_LIBRARY)
# define FOOSHARED_EXPORT Q_DECL_EXPORT
#else
# define FOOSHARED_EXPORT Q_DECL_IMPORT
#endif


and you have to define FOO_LIBRARY in your plugin project, otherwise the class doesn't get exported.

serjik
18th May 2011, 10:19
Hello high_flyer, thanks for replies,
Dear Sir, Qts manual says i can design a custom widget and make a dll also plugin export what it need, and use it in QtDesigner, i think you say i make new other dll for using in QtCreator, but then is it possible two dll and same QLed class??:confused:

i also tested this on Q3Frame it is a custom widget plugin in QtDesigner and same problem.

high_flyer
18th May 2011, 10:35
did you find in your project the code I posted in my last post?

serjik
18th May 2011, 10:44
in custom widgets have not any
//////////////////
__declspec( dllimport ) or __declspec( dllexport )

#if defined(FOO_LIBRARY)
# define FOOSHARED_EXPORT Q_DECL_EXPORT
#else
# define FOOSHARED_EXPORT Q_DECL_IMPORT
#endif
///////////////////////

my project and QLed also have not,
but my plugin has QDESIGNER_WIDGET_EXPORT "class QDESIGNER_WIDGET_EXPORT QLed : public QWidget"

high_flyer
18th May 2011, 11:27
and to what does QDESIGNER_WIDGET_EXPORT expand to?
In should expand to __declspec( dllimport ) under windows.

serjik
18th May 2011, 13:58
as QT manual and it's samples said custom widget plugin make by using QDESIGNER_WIDGET_EXPORT keyword.
i can not understand how i can use QDESIGNER_WIDGET_EXPORT keyword and you said __declspec( dllimport ) to have both of them.
Thanks in Advanced

high_flyer
18th May 2011, 14:16
try adding this to your QLed.h



#if defined(QLED_LIBRARY) //this should be defined in your preprocessor when you build the plugin
# define QDESIGNER_WIDGET_EXPORT Q_DECL_EXPORT
#else
# define QDESIGNER_WIDGET_EXPORT Q_DECL_IMPORT //this will be defined on the user application side. You don't need to do anything for that.
#endif


if it didn't help you will have to read about how to export\import classes over DLLs.
http://doc.qt.nokia.com/4.7-snapshot/sharedlibrary.html
It looks like you are not importing your QLed library.

serjik
23rd May 2011, 10:28
:)
Thanks high_flyer
I fix my problem by reading this link:
http://doc.qt.nokia.com/latest/designer-using-a-ui-file.html
But I didnt use UiLoader,Regards Lopez.

serjik
8th June 2011, 09:56
Hi, ok first i compile my plugin (here QLed you can get it http://qt-apps.org/content/show.php?content=72482), after compilation i add some QLed widgets to my FORM by QDesigner, then i copy QLed.h and QLed.cpp to my own project and in QLed.h file i delete QDESIGNER_WIDGET_EXPORT keyword, and now i can access my FORM children, BUT I DIDnt do with UiLoader, (if any body can do it please modify my code and upload, thanks in advance), now you can find my test program in attachment, Regards Serjik.