Results 1 to 7 of 7

Thread: QTableView column trouble

  1. #1
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default QTableView column trouble

    Im using a QAbstractTableModel with a QTableView and I would like to change a few things graphically.
    1. I know how to make one specific column editable. However, when I select a row, the entire row does not highlight, only the editable column on that row is highlighted. How do I go about getting the entire row highlighted, but leaving the one column eligible for editing.

    Here is my flags function:
    Qt Code:
    1. Qt::ItemFlags WTrackTableModel::flags(const QModelIndex &index) const
    2. {
    3. if (!index.isValid())
    4. return Qt::ItemIsEnabled;
    5. switch(index.column())
    6. {
    7. case 7: return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
    8. }
    9. return Qt::ItemIsEnabled;
    10. }
    To copy to clipboard, switch view to plain text mode 

    2. I set my row colors according to the color taken from an xml file like so if row colors are present:
    Qt Code:
    1. void WTrackTableView::setup(QDomNode node)
    2. {
    3. // Position
    4. if (!WWidget::selectNode(node, "Pos").isNull())
    5. {
    6. QString pos = WWidget::selectNodeQString(node, "Pos");
    7. int x = pos.left(pos.find(",")).toInt();
    8. int y = pos.mid(pos.find(",")+1).toInt();
    9. move(x,y);
    10. }
    11.  
    12. // Size
    13. if (!WWidget::selectNode(node, "Size").isNull())
    14. {
    15. QString size = WWidget::selectNodeQString(node, "Size");
    16. int x = size.left(size.find(",")).toInt();
    17. int y = size.mid(size.find(",")+1).toInt();
    18. setFixedSize(x,y);
    19. }
    20. // Foreground color
    21. QColor fgc(0,0,0);
    22. if (!WWidget::selectNode(node, "FgColor").isNull())
    23. {
    24. fgc.setNamedColor(WWidget::selectNodeQString(node, "FgColor"));
    25. }
    26. m_pTable->setForegroundColor(fgc);
    27.  
    28. // Row colors
    29. if (!WWidget::selectNode(node, "BgColorRowEven").isNull())
    30. {
    31. QColor r1;
    32. r1.setNamedColor(WWidget::selectNodeQString(node, "BgColorRowEven"));
    33. QColor r2;
    34. r2.setNamedColor(WWidget::selectNodeQString(node, "BgColorRowUneven"));
    35. setAlternatingRowColors ( true );
    36. m_pTable->setRowColor(r1, r2);
    37. QPalette Rowpalette = palette();
    38. Rowpalette.setColor(QPalette::Base, r1);
    39. Rowpalette.setColor(QPalette::AlternateBase, r2);
    40. setPalette(Rowpalette);
    41. }
    42.  
    43. /*
    44.   // BPM confidence colors
    45.   if (!WWidget::selectNode(node, "BgColorBpmNoConfirm").isNull())
    46.   {
    47.   QColor c1;
    48.   c1.setNamedColor(WWidget::selectNodeQString(node, "BgColorBpmNoConfirm"));
    49.   QColor c2;
    50.   c2.setNamedColor(WWidget::selectNodeQString(node, "BgColorBpmConfirm"));
    51.  
    52.   }
    53.   */
    54. }
    To copy to clipboard, switch view to plain text mode 
    in the QTableView on lines 28-41. Now what I would like to do is use the colors found in the BPM if statement on items in one single column. Say I have a function [bool bpmConfirm()] that returns true if the BPM is confirmed, I would then like to use r2, and r1 is the BPM is not confirmed. How would I go about doing this?

    just for reference, here is what my data function looks like in my model:
    Qt Code:
    1. QVariant WTrackTableModel :: data(const QModelIndex &index, int role) const
    2. {
    3. TrackInfoObject *m_pTrackInfo = m_pTrackPlaylist->getTrackAt(index.row());
    4.  
    5. if (!index.isValid())
    6. return QVariant();
    7.  
    8. if (index.row() >= m_pTrackPlaylist->getSongNum())
    9. return QVariant();
    10.  
    11. if (role == Qt::ForegroundRole )
    12. {
    13. return foregroundColor;
    14. }
    15. else if (role == Qt::DisplayRole )
    16. {
    17. switch(index.column())
    18. {
    19. case 0: return m_pTrackInfo->getScoreStr();
    20. case 1: return m_pTrackInfo->getTitle();
    21. case 2: return m_pTrackInfo->getArtist();
    22. case 3: return m_pTrackInfo->getType();
    23. case 4: return m_pTrackInfo->getDurationStr();
    24. case 5: return m_pTrackInfo->getBitrateStr();
    25. case 6: return m_pTrackInfo->getBpmStr();
    26. case 7: return m_pTrackInfo->getComment();
    27. }
    28. }
    29.  
    30. else
    31. return QVariant();
    32. }
    To copy to clipboard, switch view to plain text mode 
    Thanks!
    Nate

  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: QTableView column trouble

    Quote Originally Posted by nategoofs View Post
    1. I know how to make one specific column editable. However, when I select a row, the entire row does not highlight, only the editable column on that row is highlighted. How do I go about getting the entire row highlighted, but leaving the one column eligible for editing.
    Did you try changing the selectionBehavior of the view to "SelectRows"?

    Now what I would like to do is use the colors found in the BPM if statement on items in one single column. Say I have a function [bool bpmConfirm()] that returns true if the BPM is confirmed, I would then like to use r2, and r1 is the BPM is not confirmed. How would I go about doing this?
    Return those colors from the data() method of your model for the appropriate role (Foreground or Background) if you want that behaviour in the model or provide a custom delegate for the view and make the delegate "BPM aware" and simply paint using a different colour.

  3. #3
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTableView column trouble

    Did you try changing the selectionBehavior of the view to "SelectRows"?
    shouldn't setSelectionBehavior(QAbstractItemView::SelectRows ); work? I have that set up right now.

    Return those colors from the data() method of your model for the appropriate role (Foreground or Background) if you want that behaviour in the model or provide a custom delegate for the view and make the delegate "BPM aware" and simply paint using a different colour
    How would I go about making a custom delegate for this?

  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: QTableView column trouble

    Quote Originally Posted by nategoofs View Post
    shouldn't setSelectionBehavior(QAbstractItemView::SelectRows ); work? I have that set up right now.
    Yes, that's exactly what I meant.

    How would I go about making a custom delegate for this?
    You can start by reading QAbstractItemDelegate docs and then subclassing QItemDelegate and reimplementing drawDisplay() or paint().

  5. #5
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTableView column trouble

    Yes, that's exactly what I meant.
    But my problem is that I had setSelectionBehavior(QAbstractItemView::SelectRows ); set up already, and it's not working.

  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: QTableView column trouble

    Could you prepare a minimal compilable example reproducing the problem?

  7. #7
    Join Date
    Mar 2009
    Posts
    1
    Qt products
    Qt4

    Default Re: QTableView column trouble

    return return Qt::ItemIsEnabled|Qt::ItemIsSelectable; to have the other items selectable as well

Similar Threads

  1. QTableView without the first counter column?
    By pmaktieh.sirhc in forum Qt Programming
    Replies: 2
    Last Post: 4th January 2007, 22:03
  2. Replies: 9
    Last Post: 23rd November 2006, 11:39
  3. Replies: 0
    Last Post: 10th November 2006, 13:46
  4. Sorting QTableView
    By Jimmy2775 in forum Qt Programming
    Replies: 7
    Last Post: 9th February 2006, 16:47
  5. hidden QListView column suddenly visible
    By edb in forum Qt Programming
    Replies: 10
    Last Post: 27th January 2006, 08:00

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.