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. How to change background color of a cell in a treeview at runtime please?
QtWS25 Last Chance

How to change background color of a cell in a treeview at runtime please?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 8.5k Views
  • 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.
  • H Offline
    H Offline
    hpgh
    wrote on last edited by
    #1

    What I use is QSqlQueryModel + QTreeView.
    I am planning to change background color of a cell in a treeview by double clicking the cell, the following are the codes, but it won't work.
    Please tell me what is wrong with them . Thanks a lot.

    @
    void MainWindow::on_tableViewMain_doubleClicked(QModelIndex index)
    {
    processTableViewMainDoubleClicked(index);
    ui->tableViewMain->model->setData(index,QVariant(QColor(Qt::red)),Qt::BackgroundRole);
    ui->tableViewMain->update(index);
    }
    @

    EDIT: inserted @-tags for code, Gerolf. Please use that in future for code formatting

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      I don't know whether a QSqlQueryModel allows storing of such data.
      You could achieve it by using a proxy model.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • H Offline
        H Offline
        hpgh
        wrote on last edited by
        #3

        Problem solved by Implementing a Custom Delegate Inherits QStyledItemDelegate.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          stukdev
          wrote on last edited by
          #4

          Can you show the solution?

          [quote author="hpgh" date="1296664055"]Problem solved by Implementing a Custom Delegate Inherits QStyledItemDelegate.
          [/quote]

          1 Reply Last reply
          0
          • H Offline
            H Offline
            hpgh
            wrote on last edited by
            #5

            What I want to do is that the whole row will change the background color when I double click a cell in the row.
            So I wrote my own paint method in MyItemDelegate class inherits QStyledItemDelegate:

            @
            void MyItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
            {
            int row = index.row();
            if(row == myrow)
            {
            painter->fillRect(option.rect, mycolor);
            QStyledItemDelegate::paint(painter,option,index);
            }
            else
            {
            QStyledItemDelegate::paint(painter,option,index);
            }
            }

            and the following is response function of double-click event.

            void MainWindow::processTableViewMainDoubleClicked(QModelIndex &index)
            {
            ...
            ...
            // Mark the line when double clicked.
            ui->tableViewMain->setItemDelegate(new MyItemDelegate(row, QColor(Qt::yellow)));
            ...
            }

            @

            1 Reply Last reply
            0
            • ? This user is from outside of this forum
              ? This user is from outside of this forum
              Guest
              wrote on last edited by
              #6

              hi hpgh, can you use the @ tag to make the code more readable. thanks

              1 Reply Last reply
              0
              • H Offline
                H Offline
                hpgh
                wrote on last edited by
                #7

                [quote author="chetankjain" date="1297153689"]hi hpgh, can you use the @ tag to make the code more readable. thanks[/quote]

                I will pay attention next time.

                1 Reply Last reply
                0

                • Login

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