Results 1 to 12 of 12

Thread: Trouble coloring table view rows

  1. #1
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Trouble coloring table view rows

    I have a QTableView which is created on the heap in the main window.
    Need to selectively color rows, so a db query gets a list of rows to be colored.
    From that, I get an index list of the rows. Checked the index list and the indexes are all valid. Created a delegate to use painter( just a stub yet).
    The problem comes in trying to assign the delegate to the selected rows.
    Here is the offending line of code:
    Qt Code:
    1. view-->QAbstractItemView::setItemDelegateForRow(rowList[i], new SentDelegate);
    To copy to clipboard, switch view to plain text mode 
    Gives compile error complaining it can't call the function without an object.
    Qt Code:
    1. #ifndef SENTDELEGATE_H
    2. #define SENTDELEGATE_H
    3.  
    4. #include <QObject>
    5. #include <QItemDelegate>
    6. #include <QModelIndex>
    7.  
    8. class SentDelegate : public QItemDelegate
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit SentDelegate(QObject *parent = 0);
    13.  
    14. signals:
    15.  
    16. public slots:
    17.  
    18. };
    19.  
    20. #endif // SENTDELEGATE_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "sentdelegate.h"
    2. #include <QtGui>
    3.  
    4. SentDelegate::SentDelegate(QObject *parent) :
    5. QItemDelegate(parent)
    6. {
    7. }
    To copy to clipboard, switch view to plain text mode 
    And in mainwindow.h
    Qt Code:
    1. #include "sentdelegate.h"
    To copy to clipboard, switch view to plain text mode 
    I used the same approach with a different form and delegate and it works fine.
    Where am I going wrong here?

  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: Trouble coloring table view rows

    The correct approach would be different. Create a custom delegate and assign it for the whole table. Make the delegate paint the cell based on a piece of data from the model (using a custom role). To colour some item simply set this piece of data on specific indexes. If you don't want to place such data in your model because of any reason (i.e. when having more than one table assigned to the model), place a proxy model between the view and the base model.
    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. The following user says thank you to wysota for this useful post:

    waynew (29th August 2010)

  4. #3
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Trouble coloring table view rows

    Ok, that makes sense. I changed the design, but still getting the same compile error.
    I subclassed QItemDelegate as QslDelegate to use the paint function.
    Here is the relevant code:
    mainwindow.h
    Qt Code:
    1. #include "qsldelegate.h"
    To copy to clipboard, switch view to plain text mode 
    And part of MainWindow
    Qt Code:
    1. // before the ctor
    2. QslDelegate* delegate;
    3. //in the function that creates the view
    4. view-->QAbstractItemView::setItemDelegate(delegate);
    To copy to clipboard, switch view to plain text mode 
    Compile error is trying to call the function without an object.
    Must be something but I can't find anything wrong with the code.

  5. #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: Trouble coloring table view rows

    Should be:
    Qt Code:
    1. view->setItemDelegate(delegate);
    To copy to clipboard, switch view to plain text mode 
    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.


  6. #5
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Trouble coloring table view rows

    Now the error is "setItemDelegate was not declared in this scope"
    I have the set delegate line in the code right after where the view was created.

  7. #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: Trouble coloring table view rows

    Please show us a complete piece of code generating the error.
    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.


  8. #7
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Trouble coloring table view rows

    Thanks Wysota - I included QAbstractItemView in the mainwindow.h and now it compiles ok.
    Now to work on the paint part....

  9. #8
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Trouble coloring table view rows

    I got it working, mostly. After some experimenting, I went back to my original method of using view->setItemDelegateForRow(rowList[i], delegate); for the index list that needs coloring. It works ok and colors the correct rows, but when the user changes the sort order, the wrong rows are colored, so I call the function again via a signal/slot on the header when the user changes the sort order.

    The only problem is, I need to reset the row coloring to the default before re-coloring to the correct rows and I don't see how to do this.

  10. #9
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Trouble coloring table view rows

    Is your query criteria simple enough that you can perform a test in the delegate painter? If it is you could use the QModelIndex::sibling() method we recently learned about.
    Qt Code:
    1. void Delegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    2. {
    3. if(index.sibling(index.row(), columnToTest).data() == "your criteria"){
    4. painter->fillRect(option.rect,QBrush(someColor));
    5. }
    6. painter->drawText(option.rect,Qt::AlignCenter |Qt::AlignCenter,index.data().toString());
    7. }
    To copy to clipboard, switch view to plain text mode 
    And then, as Wysota says above, set the delegate on the entire table.

  11. #10
    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: Trouble coloring table view rows

    Quote Originally Posted by waynew View Post
    I got it working, mostly. After some experimenting, I went back to my original method of using view->setItemDelegateForRow(rowList[i], delegate); for the index list that needs coloring. It works ok and colors the correct rows, but when the user changes the sort order, the wrong rows are colored, so I call the function again via a signal/slot on the header when the user changes the sort order.

    The only problem is, I need to reset the row coloring to the default before re-coloring to the correct rows and I don't see how to do this.
    If you want to have more work then that's your problem My approach needs approximately 10 lines of code in total
    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.


  12. #11
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Trouble coloring table view rows

    Then I have a question Wysota - using your method, which is definitely more elegant, how would one uncolor the row when the data in that row changes such that one no longer wants it colored?

  13. #12
    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: Trouble coloring table view rows

    Clear the flag you previously set for the row to be coloured in the first place.
    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: 5
    Last Post: 3rd April 2010, 04:07
  2. Position of rows in table
    By Slewman in forum Qt Programming
    Replies: 10
    Last Post: 18th February 2010, 15:13
  3. Replies: 4
    Last Post: 26th October 2009, 22:25
  4. Refreshing one or more rows in a table view
    By William Wilson in forum Qt Programming
    Replies: 4
    Last Post: 25th May 2009, 00:10
  5. How to set two rows in a table view Horizontal header?
    By sivollu in forum Qt Programming
    Replies: 11
    Last Post: 29th April 2009, 04:57

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.