PDA

View Full Version : List-based class container for Gui?



naturalpsychic
27th January 2011, 05:12
Hey folks,

I have made up a class that inherits QObject, now i want some container (for GUI) that can store my cObj class.... I have tried following:


QListWidgetItem *_cObj=new cObj()

to add item in list widget, but i think QT or precisely C++ doesn't support polymorphism like this, C# does...is there any way i can do this.. main problem is my cObj class is toooo big and i need to use functions and properties defined in it.

thanks in advance

tbscope
27th January 2011, 05:27
but i think QT or precisely C++ doesn't support polymorphism like this, C# does...
I don't think that any language can do this. I think you don't understand c++ that well. Maybe pick up a good book.


main problem is my cObj class is toooo big and i need to use functions and properties defined in it.
What do you mean with too big? Memory consumption? Too many functions? Why did you design it that way? Why don't you make use of Object Oriented techniques? ...

If you want a list of QObject pointers, you can do this:

QList<QObject*> objectPointerList;
objectPointerList.append(new cObj());

naturalpsychic
27th January 2011, 05:44
I don't think that any language can do this. I think you don't understand c++ that well. Maybe pick up a good book.

C# does it, i have been developing in C# for 4 years now. i am just trying to move to QT for sake of cross platform development.


What do you mean with too big?
yes has many functions and i have already used object oriented techs in it, it's not memory consuming, i have made it in a way it doesn't consume alot of memory (that was best part though ;) )

my problem was that it works fine in console, now im testing it in gui,

i have already used QVector<cObj*> in class but i need to show it to user. that was my question about.

tbscope
27th January 2011, 06:26
You can not turn an apple in an orange, even in C#. The best thing you can come up with is the least common denominator. You can not turn a simple QObject in a QListWidgetItem. They are not even based on the same base classes. Even C# will not let you do that, I'm very sure of that. And if it does, how does it do that?

You can turn one object of a certain type into another type in C++ by casting (see reinterpret_cast, static_cast, ...). But this is generally not a very good idea.
Make use of inheritance.

If I understand your second post correctly, I think you want to show some widgets in a listview?

naturalpsychic
27th January 2011, 17:17
You can not turn an apple in an orange, even in C#.

certainly no, but that wasn't even what i was saying, i was asking for turning fruit into either orange or apple... very common example i use when i explain polymorphism in C# is:



List<MyColors> colors = new List<MyColors>();
colors.Add(new MyRed());
colors.Add(new MyBlue());
colors.Add(new MyGreen());


and this would only be if all MyRed, MyBlue and MyGreen was derived from class MyColors.

anyway i really appreciate your reply, and i have already come over the problem.

what i did is i created two functions with following signatures;



QTreeWidgetItem* cObj::toQTreeWidgetItem(QTreeWidget *parent);

cObj* _cObj::fromQTreeWidgetItem(const QTreeWidgetItem* item);


and its working as expected..

still trying to find a way around exec() and whats that for (in QThread) and how to make loop work...

i have got following code:



for (int i=1 ; i< cObject->list.count() ; i++) //starting from 1 intentionally
{
cThread=&cObject->list[i][0]; //cObject is cObj (inherited from QThread) and list is vector i.e, vector<cObj*>
cThread->start(); //cThread is cObj

}


my problem is if i call "cThread->skip()" program ends straight away unexpectedly, but what i want is to process (so run()) for next iteration...

any expertises will be helpful!

appreciated!

high_flyer
27th January 2011, 17:34
I have trouble following on what the problem is.
The original post says:

now i want some container (for GUI) that can store my cObj class....
Now first, I am not sure what do you mean with "for gui".
Are the cObj visible widgtes, and you want a visible container, such a QListWidget, or are your object not visible, and you just need a container like QList?
The container classes usually are templates, so they take pretty much any type.
So for example, this will work:


QList<MyObjClass*> m_list;
MyObjCalss *pObj = new MyObjClass();
m_list.push_back(pObj);


Now the whole thing with inheritance, I don't see how it has to do with the original problem?

tbscope
27th January 2011, 17:40
Just like high-flyer, I'm having trouble following you and what you exactly want.

I guess you have some QObject based classes that you want to store in a list. You use a tree widget for this purpose so there will also be some text in the tree widget?
Then you want to run each of those objects in the list in a thread? Is that correct?


still trying to find a way around exec() and whats that for (in QThread) and how to make loop work...
exec() starts the event loop of the thread.


i have got following code:



for (int i=1 ; i< cObject->list.count() ; i++) //starting from 1 intentionally
{
cThread=&cObject->list[i][0]; //cObject is cObj (inherited from QThread) and list is vector i.e, vector<cObj*>
cThread->start(); //cThread is cObj

}


I hope you don't mind if I say that that is ugly code. And I'm sure you'll get problems along the way with the way you use pointers. Example:


my problem is if i call "cThread->skip()" program ends straight away unexpectedly, but what i want is to process (so run()) for next iteration...
I have no idea about the skip() function, is this something you created?. But most likely there's an access violation.

naturalpsychic
27th January 2011, 17:46
I have trouble following on what the problem is.

thanks for reply but i have solved my first problem now i have got problem mentioned in my last post (post#5)

thanks

Added after 4 minutes:


I hope you don't mind if I say that that is ugly code.

No i dont cuz i know it is ugly and thanks for bearing with me :)


I have no idea about the skip() function, is this something you created?.

yes skip() is function and is accessable, its suppose to skip current object in iteration and start() next object in iteration.

again i really appreciate you guys' patience.

high_flyer
27th January 2011, 17:47
still trying to find a way around exec() and whats that for (in QThread) and how to make loop work...

You don't need to "get around", just don't call it if you don't need to handle internal events, start() will just run your run().


my problem is if i call "cThread->skip()" program ends straight away unexpectedly, but what i want is to process (so run()) for next iteration...
Post you run().
Better yet, explain WHAT it is you are trying to achieve - not HOW you are trying to achieve it.
Its well possible threads are not needed.

naturalpsychic
27th January 2011, 18:04
done *** thanks!

wysota
29th January 2011, 13:59
Hi,

maybe you think you have solved your problem but after reading this thread I can tell you that you don't see your main problem. I don't know what you have been doing in C# for the last four years but based on this thread and a couple of others I can say you have serious issues either with communicating your thoughts or with your understanding of obect oriented programming (regardless of the language - be it C# or C++). At first you wanted to have a list of cObj objects and you wanted to create one by assigning instances of cObj to QTreeWidgetItem pointer. If cObj was derived from QTreeWidgetItem then this would work regardless of the language (as long as it supported OOP paradigm). If cObj didn't inherit QTreeWidgetItem, it wouldn't have worked in any OO language. Since we learned your cObj inherits QThread which has no common ancestors with QTreeWidgetItem this has no possible chance to work.

Then you try "working around" QThread::exec() by calling it. I think we have a different understanding on what "work around" means. For me it means you want to avoid something. Calling X is hardly avoiding X.

Furthermore you lack understanding of pointers which is not odd since you come from C# but you should really get the grips with the programmng language you want to use before doing anything complicated with it. Avoid using pointers if you don't know how to use them. For now it is like you wanted to learn how to run (Qt) without learning to walk (C++) first.

Oh, and one more thing. Reading the documentation for a class or method you are trying to use (and possibly looking at an example of use) really doesn't hurt.