Results 1 to 5 of 5

Thread: tablemodel row, set / reset background color

  1. #1
    Join Date
    Dec 2013
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default tablemodel row, set / reset background color

    Hi everybody,
    I'm struggling around with a propably simple problem. I use a QSqlQueryModel to display some data. When a cell is double-clicked, the whole row should be marked by changing the background color. This is done by reimplementing the model and overload the data function.
    But: when another row is double-clicked the previous coloured row stays marked (but should not). When a button is clicked, or the window is resized, the marking vanishes.
    Can I emit some signal to redraw the table view? What happens, when a button is clicked - can I do the same?
    I also tried to emit dataChanged and layoutChanged signals from within the data function, but because it's const this seems not to work (compiler complains: passing 'const MyModel' as 'this' argument of 'void QAbstractItemModel::layoutChanged()' discards qualifiers [-fpermissive])

    Also I tried repaint(), it had no effect.

    Qt Code:
    1. QVariant MyModel::data(const QModelIndex &item, int role) const
    2. {
    3. QVariant result = QSqlQueryModel::data(item, role);
    4.  
    5. int row = item.row();
    6. int col = item.column();
    7.  
    8. // generate a log message when this method gets called
    9. qDebug() << QString("row %1, col%2, role %3").arg(row).arg(col).arg(role);
    10.  
    11. switch(role){
    12. case Qt::BackgroundRole:
    13. if (row == active_row) // member active_row is set by a funciton call
    14. {
    15. QBrush redBackground(Qt::red);
    16. return redBackground;
    17. }
    18. break;
    19. }
    20.  
    21. return result;
    22. }
    To copy to clipboard, switch view to plain text mode 

  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: tablemodel row, set / reset background color

    If you set active_role in a function call, shouldn't that function emit the signals?
    After all that function changes what data() will return afterwards.

    Cheers,
    _

  3. #3
    Join Date
    Dec 2013
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: tablemodel row, set / reset background color

    I wasn't clear enough in my description. The QSqlQueryModel is derived to MyModel:

    Qt Code:
    1. MyModel::MyModel(QObject *parent)
    2. :QSqlQueryModel(parent)
    3. {
    4. active_row = -1; // no row will be colored
    5. }
    To copy to clipboard, switch view to plain text mode 

    So, I have nothing to do with setting the color in the application code. All that must be done is to set the active row.

    Qt Code:
    1. void MyModel::setActiveRow(int row) // called from slot doubleClick
    2. {
    3. active_row = row;
    4. }
    To copy to clipboard, switch view to plain text mode 

    The workaround I found meanwhile, is to implement a function, that emits the protected signal "layoutChanged":
    Qt Code:
    1. void MyModel::__layoutChanged()
    2. {
    3. emit layoutChanged();
    4. }
    To copy to clipboard, switch view to plain text mode 

    I have to call it after calling setActiveRow. That's no clean interface, further on there must be a reason, why the signal is protected.
    It works so far, but is there any better solution?

  4. #4
    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: tablemodel row, set / reset background color

    Quote Originally Posted by linoprit View Post
    So, I have nothing to do with setting the color in the application code. All that must be done is to set the active row.

    Qt Code:
    1. void MyModel::setActiveRow(int row) // called from slot doubleClick
    2. {
    3. active_row = row;
    4. }
    To copy to clipboard, switch view to plain text mode 
    This method changes the model's data, i.e. MyModel::data() returns different values after that call changed active_row.
    So naturally it is the place to emit change signals.

    Qt Code:
    1. void MyModel::setActiveRow(int row)
    2. {
    3. if (active_row == row)
    4. return; // no change
    5.  
    6. int oldRow = active_row;
    7. active_row = row;
    8.  
    9. // de-highlight previously active row
    10. if (oldRow > -1) {
    11. QModelIndex rowBegin = index(oldRow, 0);
    12. QModelIndex rowEnd = index(oldRow, columnCount() - 1);
    13. emit dataChanged(rowBegin, rowEnd);
    14. }
    15.  
    16. // highlight newly active row
    17. if (row > -1) {
    18. QModelIndex rowBegin = index(row, 0);
    19. QModelIndex rowEnd = index(row, columnCount() - 1);
    20. emit dataChanged(rowBegin, rowEnd);
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  5. #5
    Join Date
    Dec 2013
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: tablemodel row, set / reset background color

    Thank you very much! That was what I didn't understand.
    The set-function has to control which parts need to be redrawn, not the data-function.

    Code is working. Great!

    Cheers,
    Harald

Similar Threads

  1. Background color
    By FelixB in forum Qwt
    Replies: 4
    Last Post: 24th June 2013, 07:03
  2. Setting Background Color
    By SixDegrees in forum Qt Programming
    Replies: 5
    Last Post: 16th February 2011, 18:33
  3. Replies: 1
    Last Post: 9th February 2010, 12:11
  4. background color of not selected tab
    By iridium in forum Qt Programming
    Replies: 0
    Last Post: 13th August 2009, 15:57
  5. QTreeWidgetItem: reset background color
    By supergillis in forum Qt Programming
    Replies: 4
    Last Post: 3rd August 2008, 13:12

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.