Results 1 to 6 of 6

Thread: Another simple question...

  1. #1
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Another simple question...

    Hi everybody, I'm new on Qt and I want to know what's the common/elegant way to program this: I have a window and it manages a QTreeWidget and also another widgets. The tree widget is a tree of directories to know in wich directory have to work the other widgets . I want that when the user expand or collapse a directory, its item shows an opened or closed folder icon. I also want that, when the user expands a directory, create its subdirectories (if they aren't created yet). Well, as you can see this tree widget has a particular behaviour, but it doesn't need any private variable and also I only want an instance of this tree widget. I want to know if I should subclass QTreeWidget (and put in this new class all the SIGNALS and SLOTS to make the tree working as I want) or put all the code to manage this tree widget in the code of the window although it's specific for only the tree?

    Thanks.
    Last edited by Dark_Tower; 20th March 2006 at 12:11.

  2. #2
    Join Date
    Jan 2006
    Location
    Minsk, Brest, Belarus
    Posts
    54
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Another simple question...

    This is code from my small program:

    Qt Code:
    1. QStyle *style = tableView->style();
    2.  
    3. m_folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirClosedIcon),
    4. QIcon::Normal, QIcon::Off);
    5. m_folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirOpenIcon),
    6. QIcon::Normal, QIcon::On);
    7.  
    8. //here we get the folderIcon.
    9.  
    10. QTreeWidgetItem *newItem = new QTreeWidgetItem(tableView);
    11. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
    12. newItem->setIcon(0, folderIcon());
    13. newItem->setText(0, text);
    14.  
    15. //tags
    16. newItem->setData(0, Qt::UserRole, "folder");
    To copy to clipboard, switch view to plain text mode 

    And also make this item expandable!

    This is only part of code, use it as example and you'll understand.

  3. #3
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Another simple question...

    Thanks Xagen for the code, but I what want to know is if the common way to manage this tree widget is to subclass it (to relase code from the main window) or put the connections with the signals (item expanded/colapsed) and the code for its slots in the main window?
    Last edited by Dark_Tower; 20th March 2006 at 14:10.

  4. #4
    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: Another simple question...

    Quote Originally Posted by Dark_Tower
    Thanks Xagen for the code, but I what want to know is if the common way to manage this tree widget is to subclass it (to relase code from the main window) or put the connections with the signals (item expanded/colapsed) and the code for its slots in the main window?
    It's totally up to you. Mostly the only compulsion for subclassing is when you need to access some protected methods. Thanks to Qt's event filters, you can even catch events without subclassing a widget. But of course it depends on many factors. For example the amount of widget specific code you would need to write in another class. I would avoid writing exceeding amount of widget specific code in another class, just for the sake of clarity. And in an opposed situation it could be a bit vain to subclass just to add a simple feature/functionality when you can achive the same "outside" the class..

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

    Dark_Tower (20th March 2006)

  6. #5
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Another simple question... (related with Xagen code)

    Hi, sorry but I have to re-open this post. This time I need to do some questions about the code that Xagen kindly posted previously in this post:

    1) Can Xagen or someone else please explain me why an assignation of a pixmap in the same icon 2 times?

    Qt Code:
    1. m_folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirClosedIcon),
    2. QIcon::Normal, QIcon::Off);
    3. m_folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirOpenIcon),
    4. QIcon::Normal, QIcon::On);
    To copy to clipboard, switch view to plain text mode 

    2) The method "folderIcon" which of both pixmaps will contain the returned icon? And why doesn't access directly to the variable "m_folderIcon" ? as seems that's the purpose...
    Qt Code:
    1. newItem->setIcon(0, folderIcon());
    To copy to clipboard, switch view to plain text mode 

    Thanks.
    Last edited by Dark_Tower; 25th March 2006 at 11:14.

  7. #6
    Join Date
    Jan 2006
    Location
    Minsk, Brest, Belarus
    Posts
    54
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Cool Re: Another simple question...

    Answer is very simple:

    This icon represents two states:
    opened

    and

    closed

    ----
    Sorry for the late answer

Similar Threads

  1. Simple RegExp Question...
    By jared.carlson23 in forum Newbie
    Replies: 1
    Last Post: 4th July 2008, 14:10
  2. QTextEdit simple question
    By Marcopolo in forum Qt Tools
    Replies: 4
    Last Post: 11th October 2007, 00:01
  3. simple thread layout question
    By mhoover in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2006, 11:02
  4. simple question on Class-Members
    By mickey in forum General Programming
    Replies: 7
    Last Post: 4th February 2006, 22:37
  5. QTextEdit Qt4: simple question
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 18th January 2006, 12:03

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.