Results 1 to 4 of 4

Thread: How to highlight certain items in QTeeView?

  1. #1
    Join Date
    Jun 2013
    Posts
    2

    Default How to highlight certain items in QTeeView?

    I have a QTreeView filled with some items and when a specific event occurs I want to highlight a few specific items in that view (by for example changing their color). How do I do that? And please, when you answer, tell the whole story, because I'm new to Qt and still many obvious things are not obvious to me.

    A couple of similar threads suggest overloading the model and specifically the "data()" method to return custom color (that's correct, right?) but this would require each item to know if it should be highlighted or not. Can I somehow store that information within each item?

  2. #2
    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 highlight certain items in QTeeView?

    It really depends what the semantics of your actions are. In general you can implement what you want either in the model, in the view or in the delegate so in any of the three components that make the ItemViews architecture. Implementing this behaviour in the model makes sense if assuming that you have more than one view on the same model, you want the same items highlighted in both views. Then you need to emit dataChanged in the model for the indexes that are to be updated and return the changed value from data(). Another solution is to keep a list of highlighted items in the view or delegate and reimplement proper methods in those components. Sorry for not "telling the whole story" but I'd have to quote half of the documentation so instead of that you can just read it yourself
    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.


  3. #3
    Join Date
    Jun 2013
    Posts
    2

    Default Re: How to highlight certain items in QTeeView?

    Ok, you really have to tell me more, because right now I can't move forward. Let's go the first thing you suggested - implement it in the model. I tried this:
    Qt Code:
    1. class HighlightModelItem : public QStandardItem
    2. {
    3. private:
    4. bool highlighted;
    5.  
    6. public:
    7. HighlightModelItem(void);
    8. explicit HighlightModelItem(const QString & text);
    9. HighlightModelItem(const QIcon & icon, const QString & text);
    10. explicit HighlightModelItem(int rows, int columns = 1);
    11. virtual ~HighlightModelItem(void);
    12. QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
    13. void setHighlighted(bool value);
    14. };
    To copy to clipboard, switch view to plain text mode 

    (BTW, I had no idea how subclassing should look like, I can't find EVEN ONE example on the internet... so I just reimplemented ctors/dtor from QStandardItem. :P)

    Qt Code:
    1. QVariant HighlightModelItem::data(const QModelIndex & index, int role) const
    2. {
    3. if (role == Qt::BackgroundColorRole && highlighted)
    4. {
    5. return QColor(255, 0, 0);
    6. }
    7. return data(index, role);
    8. }
    9.  
    10. void HighlightModelItem::setHighlighted(bool value)
    11. {
    12. highlighted = value;
    13. emitDataChanged();
    14. }
    To copy to clipboard, switch view to plain text mode 

    And I'm just using this instead of QStandardItem in QStandardItemModel object:
    Qt Code:
    1. QStandardItemModel * blocksModel = new QStandardItemModel();
    2. blocksModel->setHorizontalHeaderItem(0, new HighlightModelItem("Blocks"));
    3. inputCategory = new HighlightModelItem("Input blocks");
    4. processCategory = new HighlightModelItem("Processing blocks");
    5. outputCategory = new HighlightModelItem("Output blocks");
    6. blocksModel->setItem(0, inputCategory);
    7. blocksModel->setItem(1, processCategory);
    8. blocksModel->setItem(2, outputCategory);
    9. treeView->setModel(blocksModel);
    To copy to clipboard, switch view to plain text mode 

    But, as you can guess, this doesn't work. "HighlightModelItem::data()" never gets called (despite me calling HighlightModelItem::setHighlighted()" in my code), why is that?

  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 highlight certain items in QTeeView?

    I'm sorry, that's not even remotely what you need to do.

    http://qt-project.org/doc/qt-4.8/qab...itemmodel.html

    Read at least the "Subclassing" part and links leading from it.
    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. Replies: 2
    Last Post: 20th August 2010, 06:18
  2. highlight problem
    By supriyajn in forum Newbie
    Replies: 1
    Last Post: 20th May 2010, 20:00
  3. disabling highlight effect for qlistwidget items
    By sushmacr in forum Qt Programming
    Replies: 8
    Last Post: 5th April 2010, 17:53
  4. highlight in QTextEdit
    By ubuntudevelop78 in forum Qt Programming
    Replies: 1
    Last Post: 27th November 2009, 01:22
  5. Hover and Highlight QTable and QTree Items
    By VireX in forum Qt Programming
    Replies: 41
    Last Post: 18th May 2007, 22:55

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.