Results 1 to 8 of 8

Thread: How to show two icon in QTreeView

  1. #1
    Join Date
    Oct 2012
    Location
    Gurgaon, Haryana, India
    Posts
    15
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Post How to show two icon in QTreeView

    Hello

    I am developing an application using Christophe Dumez's blog cdumez http://blogspot.in/2010/11/how-to-us...del-in-qml.htm
    Then i am showing my items using QTreeView.
    In tree model it take only one icon.

    Now i want to show two icons (One on left side of text & other on right side of text) in same row.
    so how can i do that.
    Can any one suggest me(any example) for that

  2. #2
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to show two icon in QTreeView

    Maybe could you provide a piece of your code?

  3. #3
    Join Date
    Oct 2012
    Location
    Gurgaon, Haryana, India
    Posts
    15
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: How to show two icon in QTreeView

    My model is custom, which is an abstract model. I have learned about Item delegate & try to find example code to display tow icons in a row but not successful. I found example about custom delegate e.g star delegate(http://qt-project.org/doc/qt-4.8/ite...rdelegate.html) but nothing for TreeView. I want to only display two icons in a row which display when my treeview display. But still i don't have any idea how to do that, so thats why i can't provide any code?

  4. #4
    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: How to show two icon in QTreeView

    QTreeView uses the same delegates as other view classes. You can use exactly the same approach. I have no idea what it has to do with the QML-related block post you linked to in your opening post.
    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.


  5. #5
    Join Date
    Oct 2012
    Location
    Gurgaon, Haryana, India
    Posts
    15
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: How to show two icon in QTreeView

    Hi,
    i am using QItemDelegates paint method for custom delegate. My code is as following
    .h file
    Qt Code:
    1. #include <QItemDelegate>
    2. #include <QModelIndex>
    3. #include <QObject>
    4. #include <QSize>
    5. #include <QSpinBox>
    6.  
    7. class TreeViewItemDelegate : public QItemDelegate
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. TreeViewItemDelegate(QObject *parent = 0);
    13.  
    14. void paint( QPainter * painter,
    15. const QStyleOptionViewItem & option,
    16. const QModelIndex & index ) const;
    17. };
    To copy to clipboard, switch view to plain text mode 
    .cpp file
    Qt Code:
    1. TreeViewItemDelegate::TreeViewItemDelegate(QObject *parent)
    2. : QItemDelegate(parent)
    3. {
    4. }
    5.  
    6. void TreeViewItemDelegate ::paint( QPainter * painter,
    7. const QStyleOptionViewItem & option,
    8. const QModelIndex & index ) const
    9. {
    10. QString contactURI = index.model()->data(index, Qt::DisplayRole).toString();
    11. QIcon icon = qvariant_cast<QIcon>(index.model()->data(index, Qt::DecorationRole));
    12. QIcon icon2 = qvariant_cast<QIcon>(index.model()->data(index, Qt::UserRole+5));
    13.  
    14. QPixmap iconPixmap = icon.pixmap(option.decorationSize);
    15. QPixmap iconPixmap2 = icon2.pixmap(option.decorationSize);
    16.  
    17. QStyleOptionViewItem myoption = option;
    18. QStyleOptionViewItem myoption2 = option;
    19. QStyleOptionViewItem myoption3 = option;
    20. myoption.displayAlignment = Qt::AlignCenter;
    21. myoption2.decorationAlignment = Qt::AlignLeft;
    22. myoption3.decorationAlignment = Qt::AlignRight;
    23.  
    24. drawBackground(painter, myoption, index);
    25. drawDecoration(painter, myoption, myoption2.rect, iconPixmap);
    26. drawDisplay(painter,myoption, myoption.rect, contactURI);
    27. drawDecoration(painter, myoption, myoption3.rect, iconPixmap2);
    28. drawFocus(painter, myoption,myoption.rect);
    29. }
    To copy to clipboard, switch view to plain text mode 
    There is on problem that only one icon is displaying. I know i am doing something wrong, so can anyone help me to correct this code to display both icon??
    Last edited by Pardeep; 11th February 2013 at 11:09. Reason: missing [code] tags

  6. #6
    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: How to show two icon in QTreeView

    Don't call drawDecoration() for the additional icon. Instead use QPainter::drawPixmap() directly.
    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.


  7. #7
    Join Date
    Oct 2012
    Location
    Gurgaon, Haryana, India
    Posts
    15
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to show two icon in QTreeView

    i added two more lines in my cpp file

    Qt Code:
    1. QRect *rect = new QRect(0,0,10,10);
    2. painter->drawPixmap(rect,iconPixmap);
    To copy to clipboard, switch view to plain text mode 
    but it is not working..

  8. #8
    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: How to show two icon in QTreeView

    It doesn't even compile. And if it did, the rect probably doesn't make sense anyway.
    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. Add icon to the right in a QTreeView
    By hubbobubbo in forum Qt Programming
    Replies: 3
    Last Post: 21st April 2010, 19:01
  2. QTreeView ICON
    By Raymond in forum Qt Programming
    Replies: 2
    Last Post: 28th October 2009, 02:43
  3. QPushButton - Only show the Icon
    By graciano in forum Newbie
    Replies: 9
    Last Post: 19th September 2009, 21:15
  4. QSystemTrayIcon doesn't show icon ?
    By probine in forum Qt Programming
    Replies: 3
    Last Post: 25th January 2007, 19:17
  5. WYSIWYG html, Window show png icon mac no!
    By patrik08 in forum Qt Programming
    Replies: 10
    Last Post: 25th May 2006, 12:01

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.