Results 1 to 2 of 2

Thread: Customizing QTableView grid style

  1. #1
    Join Date
    Jun 2019
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Customizing QTableView grid style

    Hello,

    I'm wondering several things. I have subclassed QTableView to make a custom table. I'd like to be able to do several things.

    First of all, I wanted the selected cells not to all have the "selected" color (blue by default) but instead have a frame around the selected cells (just like in Excel). To do this, I've used the following (in my custom QItemDelegate):

    Qt Code:
    1. void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. QModelIndex upIndex = index.sibling(index.row() - 1, index.column());
    4. QModelIndex downIndex = index.sibling(index.row() + 1, index.column());
    5. QModelIndex rightIndex = index.sibling(index.row(), index.column() + 1);
    6. QModelIndex leftIndex = index.sibling(index.row(), index.column() - 1);
    7.  
    8. auto newOption = option;
    9. if (option.state.testFlag(QStyle::State_Selected))
    10. {
    11. painter->save();
    12.  
    13. auto selIndexes = selM->selectedIndexes().toSet();
    14. painter->setPen(QPen(Qt::red, 5));
    15. if (!selIndexes.contains(rightIndex))
    16. painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
    17. if (!selIndexes.contains(upIndex))
    18. painter->drawLine(option.rect.topLeft(), option.rect.topRight());
    19. if (!selIndexes.contains(downIndex))
    20. painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
    21. if (!selIndexes.contains(leftIndex))
    22. painter->drawLine(option.rect.topLeft(), option.rect.bottomLeft());
    23.  
    24. painter->restore();
    25. // newOption.palette.setBrush(QPalette::Normal, QPalette::Highlight, index.data(Qt::BackgroundRole).value<QColor>());
    26. newOption.palette.setBrush(QPalette::Normal, QPalette::Highlight, Qt::gray);
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    This is probably not optimal, but I'd like to have something that works, before anything else.

    Now it seems to be working, but it unfortunately isn't automatically repainted. What happens when I select cells is the following:

    Current.png

    Which is not what I'm looking for at all. I guess it's not being repainted because (1) The points inside the zone are still red and (2) If I resize my window, I get the following:

    KindaCorrect.PNG

    Which is way closer to what I'm trying to achieve.

    I've already tried to do this in my QTableView:

    Qt Code:
    1. // selModel is my selection model
    2. connect(selModel, &QItemSelectionModel::selectionChanged, [this]() {
    3. for(const auto & selected : selModel->selectedIndexes())
    4. {
    5. update(visualRect(selected));
    6. repaint(visualRect(selected));
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 


    (Before, I actually used setDirtyRegion but it didn't work either, so I figured I'd do something more... brutal.)

    Please suggest if you have any ideas for any of the issues.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Customizing QTableView grid style

    Hmm, maybe you need to call update() on the viewport() widget.

    Cheers,
    _

Similar Threads

  1. grid color in QTableView
    By wagmare in forum Newbie
    Replies: 1
    Last Post: 27th February 2013, 15:54
  2. Replies: 2
    Last Post: 3rd April 2010, 16:18
  3. Replies: 1
    Last Post: 8th February 2010, 05:58
  4. Grid QTableView horizontal only
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 11th September 2009, 17:14
  5. Suggestions Anyone:QTableView Grid
    By locus in forum Qt Programming
    Replies: 3
    Last Post: 23rd January 2007, 09:01

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.