PDA

View Full Version : Working with QObject



been_1990
15th June 2009, 20:04
How do I work with QObject? I want a main Object as parent to several others that have several information in them. Ex:


QObject-mainParent{
QObject-child{
info1{};
info2{};
}
QObject-child{
info1{};
info2{};
}
QObject-child{
info1{};
info2{};
}
QObject-child{
info1{};
info2{};
}
}

wysota
15th June 2009, 21:09
What exactly is the problem? What did you already try?

been_1990
16th June 2009, 03:35
I guess I just need to know how to to that in a for() loop. And what exactly is QObject, is it a general-purpose object for OOP? Does addChild() create a new object and can I acess it and change-add its value? Can I give the child a name that can be acessed anywhere? Not much of a problem, just searching for guidance.
I actually want to make several boxes across the screen and give each one several values, such as heigth, width, name, image and be able to receive events from them, ex: click , doubleclick , hover, etc.

lni
16th June 2009, 03:54
I guess I just need to know how to to that in a for() loop. And what exactly is QObject, is it a general-purpose object for OOP? Does addChild() create a new object and can I acess it and change-add its value? Can I give the child a name that can be acessed anywhere? Not much of a problem, just searching for guidance.
I actually want to make several boxes across the screen and give each one several values, such as heigth, width, name, image and be able to receive events from them, ex: click , doubleclick , hover, etc.

Your questions beat our guru :)

aamer4yu
16th June 2009, 05:11
I actually want to make several boxes across the screen and give each one several values, such as heigth, width, name, image and be able to receive events from them, ex: click , doubleclick , hover, etc.
May be you can go for Graphics View framework ? your boxes will be graphics items..

or you can have several QWidgets on the screen, so u can get all hover, click etc events..
hope i understood ur prob right :cool:

nish
16th June 2009, 06:52
I guess I just need to know how to to that in a for() loop. And what exactly is QObject, is it a general-purpose object for OOP? Does addChild() create a new object and can I acess it and change-add its value? Can I give the child a name that can be acessed anywhere? Not much of a problem, just searching for guidance.
I actually want to make several boxes across the screen and give each one several values, such as heigth, width, name, image and be able to receive events from them, ex: click , doubleclick , hover, etc.

you seem to be reading an overdose of OOP concepts in theory. Assuming you know inheritance and pointers... start reading the "GUI programming with Qt4" book. At least start to take a look at the tutorials that are presented in QtAssistant. But for that u need to download the Qt. But first u need to figure out your OS. Depending upon the OS u need to select the correct qt sdk. and finally post your next query in NEWBIE forum.

ramsarvan
16th June 2009, 07:37
All Qt Widgets are derived from QObject.

Yes you can use QObject, if you just want a pure C++ object
(ie, if you dont need SIGNAL, SLOTS & PROPERTIES)
Also, the Q_OBJECT macro is mandatory for any object (including QObject)
that implements signals, slots or properties.

You can set a object name to your object as follows:
setObjectName("xxxx");


class MainWidget : public QWidget
{
Q_OBJECT
// follow Qt standards.
constructor
{
QStringList items, objects;
items<<"label1"<<"label2"<<label3";
objects<<"objl1"<<objl2"<<objl3";

QVBoxLayout* layout = new QVBoxLayout();
for (int i = 0; items.count(); i++)
{
QLabel* wid = new QLabel (items.at(i), this);
// Other way:
// QLabel* wid = new QLabel (items.at(i));
// wid.setParent (this);
setObjectName (objects.at(i)); /* Set the object name */
wid->setFixedSize(20, 20); /* Height, Width */
}

QList<QWidget *> widgets = this->findChildren<QWidget *>("objl1");
// returns you the list of widgets with the name "objl1"
if(widgets.count() > 0)
widgets[0]->setFixedSize(40,40); //changing the size

}
}

Now all label boxes you have created becomes children of your main
widget. And you can use findChildren and modify their properties.
Please modify the code with fixing syntax issues and Qt standards,
it should work fine.

Thanks,
Ram

nish
16th June 2009, 08:24
this guy... "ramsarvan", sits next to me, he thinks my post is BS.

wysota
16th June 2009, 08:36
I guess I just need to know how to to that in a for() loop.
Eem... what is the problem? You know how to use loops, right?


And what exactly is QObject, is it a general-purpose object for OOP?
No, there is no such thing in C++. QObject is a very specific class.


Does addChild() create a new object and can I acess it and change-add its value?
I don't see any "addChild()" method in QObject. But even if it was there, it would only establish a parent-child relationship between existing objects.


Can I give the child a name that can be acessed anywhere?
QObject::objectName()


I actually want to make several boxes across the screen and give each one several values, such as heigth, width, name, image and be able to receive events from them, ex: click , doubleclick , hover, etc.
You mean widgets? Then you need to read about layouts (QLayout and family) as well.

been_1990
16th June 2009, 17:26
May be you can go for Graphics View framework ? your boxes will be graphics items..

or you can have several QWidgets on the screen, so u can get all hover, click etc events..
hope i understood ur prob right :cool:

But thats exactly what I'll do. I'll read the number of .xml files in a directory. Then for each .xml file I'll load a QIcon data saved in a node, read other info from other nodes. Then put the QIcon pixmap in a QGraphicsView thats already set up. I need to somehow tie all the .xml nodes together so when I click on, let's say, QGraphicsView.firstChild() I can acess the other info that would be loaded into the same object as the graphic I just clicked. I hope I explained myself clearly. I've already done this using Actionscript 3.0 in Flash, but using QT beats me.

been_1990
19th June 2009, 03:57
I guess I started with my left foot in this thread... I started asking something that wont take me to where I want to go. This is what I want to know:

Whats the best way to load a pixmap from a data file? Using QGraphicsView? Or making a new widget for each pixmap? I want to be able to grab events that happen to the object holding the pixmap.

I'm really sorry for the wrong explanation of what I needed, one day I'll get it rigth from the beggining... :o

aamer4yu
19th June 2009, 05:31
Whats the best way to load a pixmap from a data file?
QPixmap::load or QPixmap::loadFromData


Or making a new widget for each pixmap? I want to be able to grab events that happen to the object holding the pixmap.
That depends on what kind of events you want to capture..and what you want to do on those events.
For basic mouse clicks and double clicks, use QLabel will be good enough. You can you QLabel in grid layout and show multiple pics..
If you want some anitmation, go for graphics view. You can refer Embedded dialog demo in Qt Demo (since 4.5) . Instead of dialog, you can have your widget or graphics item.

Can refer more if you tell what actions you want to perform on events on the pixmap.

been_1990
20th June 2009, 02:56
Functionality needed:

Click/Double-Click ;
Click&Drag;
On Hover animation;
Speed;
Left-Click menu;

wysota
20th June 2009, 09:22
Go for QGraphicsView and friends.

been_1990
20th June 2009, 17:09
Ok, I'll use QGraphicsView. Now back to my first question, how can I group some data? In a array? Or is there something more "QT"? Ex:


arrayMain{
obj1{"myname", 01001010-myData, myPlaceInTheWorld};
obj2{"myname", 01401010-myData, myPlaceInTheWorld};
obj3{"myname", 01021010-myData, myPlaceInTheWorld};
obj4{"myname", 01042210-myData, myPlaceInTheWorld};
}

wysota
20th June 2009, 21:12
Group it the way you want... I don't really understand the problem. Your data has some structure, so represent that structure using classes and instances of them.