List-based class container for Gui?
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:
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
Re: List-based class container for Gui?
Quote:
Originally Posted by
naturalpsychic
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.
Quote:
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:
Code:
QList<QObject*> objectPointerList;
objectPointerList.append(new cObj());
Re: List-based class container for Gui?
Quote:
Originally Posted by
tbscope
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.
Quote:
Originally Posted by
tbscope
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.
Re: List-based class container for Gui?
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?
Re: List-based class container for Gui?
Quote:
Originally Posted by
tbscope
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:
Code:
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;
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:
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!
Re: List-based class container for Gui?
I have trouble following on what the problem is.
The original post says:
Quote:
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:
Code:
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?
Re: List-based class container for Gui?
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?
Quote:
Originally Posted by
naturalpsychic
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.
Quote:
i have got following code:
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:
Quote:
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.
Re: List-based class container for Gui?
Quote:
Originally Posted by
high_flyer
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:
Quote:
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 :)
Quote:
Originally Posted by
tbscope
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.
Re: List-based class container for Gui?
Quote:
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().
Quote:
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.
Re: List-based class container for Gui?
Re: List-based class container for Gui?
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.