Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtableWidget VerticalHeader colour depending on row content
Forum Updated to NodeBB v4.3 + New Features

QtableWidget VerticalHeader colour depending on row content

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 259 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    f222
    wrote on last edited by
    #1

    Hello,

    The example shown are anonymized but the logic is the one I used.
    I currently have a QTableWidget with 2 columns (first one is the name of a book and the second one its price) and a header whose colour depends on the books price. It is populated as follows:

        int row {};
        for (Book const *book : qAsConst(booksList))
        {
            table->insertRow(row);
    
            table->setVerticalHeaderItem(
                row,
                getColorizedCell(row, book->price)
            );
    
            table->setItem(row, 0, getNewCell<QString>(book->name));
            table->setItem(row, 1, getNewCell<double>(book->price));
    
            row++;
        }
    
    • getColorizedCell creates a QTableWidgetItem containing first param as text and sets its textColor depending on the second param.
    • getNewCell creates a QTableWidgetItem and sets its DisplayRole Data to the param sent.

    This works well until I enabled sorting (setSortingEnabled(true)) on the table: when I sort my table the rows are sorted but the vertical header colour doesn't follow the sorting.

    Is there anyway to make this work natively with the QTableWidget or should I try to find a way to do this using a QTableView/Model ?

    1 Reply Last reply
    0
    • Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #2

      QTableWidget is a convenience class, providing an easy way to implement a table with basic features. The implementation above will apply the right colors once, when the QTableWidget is populated. It won't change if the data is shuffled around.

      For that purpose, I recommend to implement a QTableView with a custom model, inheriting from QAbstractTableModel. The model's data() override has to return a QVariant for different roles. The minimum requirement is DisplayRole, which has to contain the data to be displayed in the cell.
      The DecorationRole is what you're looking for to color a cell according to its content.

      Software Engineer
      The Qt Company, Oslo

      1 Reply Last reply
      1
      • F f222 has marked this topic as solved on

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved