Results 1 to 7 of 7

Thread: Object Inherit from QTreeWidgetItem

  1. #1
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Object Inherit from QTreeWidgetItem

    Hi,

    i'm making a QTreeWidget that uses an object that inherits from QTreeWidgetItem.

    the code is:
    Object.h
    Qt Code:
    1. #pragma once
    2.  
    3. #include <QPixmap>
    4. #include <QDir>
    5. #include <QFile>
    6. #include <QString>
    7. #include <QTreeWidgetItem>
    8.  
    9. #include "Resources.h"
    10.  
    11. class Object:public QTreeWidgetItem{
    12. public:
    13. Object(QString file="",QString fileInfo="");
    14. void init();
    15.  
    16. bool isObject(){return (m_pixmap!=NULL);};
    17. void setName(QString newValue);
    18. void setPixmap(QString file=RES_FOLDER_22x22);
    19. private:
    20. QPixmap *m_pixmap;
    21. QString m_name;
    22. QString m_file;
    23. QString m_fileInfo;
    24. };
    To copy to clipboard, switch view to plain text mode 

    Object.cpp
    Qt Code:
    1. #include "Object.h"
    2.  
    3. Object::Object(QString file,QString fileInfo):m_file(file),m_fileInfo(fileInfo){
    4. m_pixmap=NULL;
    5. init();
    6. }
    7.  
    8. void Object::init(){
    9. if(QFileInfo(m_file).isDir()){
    10. QDir dir(m_file);
    11. setName(dir.dirName());
    12. setPixmap();
    13. foreach(QString entry, dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot)){
    14. Object *newObject;
    15. addChild(newObject);
    16. newObject=new Object(m_file+"/"+entry);
    17. }
    18. }
    19. else{
    20.  
    21. }
    22. }
    23.  
    24. void Object::setName(QString newValue){
    25. m_name=newValue;
    26. setText(0,newValue);
    27. }
    28.  
    29. void Object::setPixmap(QString file){
    30. if(QFile(file).exists()){
    31. if(m_pixmap) delete m_pixmap;
    32. m_pixmap=new QPixmap(file);
    33. setIcon(0,QIcon(file));
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 
    and the function that fills the QTreeWidget

    Qt Code:
    1. void Objects::refreshFiles(){
    2. QString objectsPath=OBJECTS_PATH;
    3. QDir dirInfo(objectsPath);
    4. if(dirInfo.exists()){
    5. foreach(QString entry, dirInfo.entryList(QDir::Dirs|QDir::NoDotAndDotDot)){
    6. Object *item=new Object(dirInfo.absolutePath()+"/"+entry);
    7. treeObjects->addTopLevelItem(item);
    8. }
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    for the TopLevelItems it works fine, but in subitems it uses the first top level item again.

    Any ideas. Thanks

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Object Inherit from QTreeWidgetItem

    Quote Originally Posted by xgoan
    for the TopLevelItems it works fine, but in subitems it uses the first top level item again.
    As far as I remember, there's some restriction in QTreeWidget code that one can not construct a tree of items first and then add the top level item. One might need to add the top level item first and then continue with adding child items.. I didn't read through your code carefully though so I'm not sure if that's the case.

    You might consider using QDirModel+QTreeView too, it might simplify things quite a lot.. take a look at this post.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    xgoan (18th August 2006)

  4. #3
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Object Inherit from QTreeWidgetItem

    Ouch...

    So I will do the recursivity in the refreshFile() function.

    Thanks for the help.

  5. #4
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Object Inherit from QTreeWidgetItem

    Another question, can the QTreeWidget items be resized?

  6. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Object Inherit from QTreeWidgetItem

    J-P Nurmi

  7. #6
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Object Inherit from QTreeWidgetItem

    Could be, but the icon dont resize properly because the item has the same space between QIcon and Text

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Object Inherit from QTreeWidgetItem

    J-P Nurmi

Similar Threads

  1. From extends QTreeWidgetItem emit signal?
    By patrik08 in forum Qt Programming
    Replies: 4
    Last Post: 19th May 2006, 14:54
  2. Passing Object to dll
    By ankurjain in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 09:50
  3. enabling a QTreeWidgetItem as false
    By Kapil in forum Newbie
    Replies: 2
    Last Post: 3rd March 2006, 07:32
  4. Signal-Slot, object instances
    By ct in forum Qt Programming
    Replies: 3
    Last Post: 16th February 2006, 19:08
  5. Using QSA: A very basic question
    By yogeshm02 in forum Newbie
    Replies: 3
    Last Post: 26th January 2006, 07:34

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.