Results 1 to 11 of 11

Thread: List-based class container for Gui?

  1. #1
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default 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:

    Qt Code:
    1. QListWidgetItem *_cObj=new cObj()
    To copy to clipboard, switch view to plain text mode 

    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
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: List-based class container for Gui?

    Quote Originally Posted by naturalpsychic View Post
    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:
    Qt Code:
    1. QList<QObject*> objectPointerList;
    2. objectPointerList.append(new cObj());
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: List-based class container for Gui?

    Quote Originally Posted by tbscope View Post
    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 View Post
    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.
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default 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?
    Last edited by tbscope; 27th January 2011 at 06:36.

  5. #5
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: List-based class container for Gui?

    Quote Originally Posted by tbscope View Post
    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:

    Qt Code:
    1. List<MyColors> colors = new List<MyColors>();
    2. colors.Add(new MyRed());
    3. colors.Add(new MyBlue());
    4. colors.Add(new MyGreen());
    To copy to clipboard, switch view to plain text mode 

    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;

    Qt Code:
    1. QTreeWidgetItem* cObj::toQTreeWidgetItem(QTreeWidget *parent);
    2.  
    3. cObj* _cObj::fromQTreeWidgetItem(const QTreeWidgetItem* item);
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. for (int i=1 ; i< cObject->list.count() ; i++) //starting from 1 intentionally
    2. {
    3. cThread=&cObject->list[i][0]; //cObject is cObj (inherited from QThread) and list is vector i.e, vector<cObj*>
    4. cThread->start(); //cThread is cObj
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 

    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!
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: List-based class container for Gui?

    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:
    Qt Code:
    1. QList<MyObjClass*> m_list;
    2. MyObjCalss *pObj = new MyObjClass();
    3. m_list.push_back(pObj);
    To copy to clipboard, switch view to plain text mode 

    Now the whole thing with inheritance, I don't see how it has to do with the original problem?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default 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 View Post
    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:

    Qt Code:
    1. for (int i=1 ; i< cObject->list.count() ; i++) //starting from 1 intentionally
    2. {
    3. cThread=&cObject->list[i][0]; //cObject is cObj (inherited from QThread) and list is vector i.e, vector<cObj*>
    4. cThread->start(); //cThread is cObj
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 
    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.

  8. #8
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: List-based class container for Gui?

    Quote Originally Posted by high_flyer View Post
    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

    Quote Originally Posted by tbscope View Post
    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.
    Last edited by naturalpsychic; 27th January 2011 at 17:46.
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: List-based class container for Gui?

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. #10
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: List-based class container for Gui?

    done *** thanks!
    Last edited by naturalpsychic; 27th January 2011 at 18:56.
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. class list
    By okanakyuz in forum Qt Tools
    Replies: 1
    Last Post: 17th December 2009, 09:51
  2. Replies: 3
    Last Post: 7th August 2009, 11:21
  3. error when adding signal slot to a QGLWidget based class
    By john_god in forum Qt Programming
    Replies: 2
    Last Post: 12th April 2009, 00:31
  4. Quick ? about qSort(Container & container)
    By JimDaniel in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2007, 12:20
  5. Saving properties of a QObject based class
    By ghorwin in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2007, 02:26

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.