c++qt4contextmenuqtreeviewqtreewidgetitem

use QModelIndex to set background of a QTreeWidgetItem


I have MainWindow with a qTreeWidget. To add elements to this widget I've implemented this functions:

QTreeWidgetItem *MainWindow::prepareIt(QTreeWidgetItem *it, const QString &name, const QString &descr)
{
    it->setText(0, name);
    it->setText(1, descr);
    return it;
}

QTreeWidgetItem *MainWindow::addRoot(const QString &name, const QString &descr)
{
    QTreeWidgetItem *it = prepareIt(new QTreeWidgetItem(ui->treeWidget), name, descr);
    it->setBackground( 0, QColor{112, 77, 75} );
    it->setBackground( 1, QColor{78, 90, 110} );
    return it;
}

QTreeWidgetItem *MainWindow::addChild(QTreeWidgetItem *parent, const QString &name, const QString &descr)
{
    auto child = new QTreeWidgetItem(parent);

    child->setBackground( 0, QColor{102, 67, 65} );
    child->setBackground( 1, QColor{68, 80, 99} );

    parent->addChild(prepareIt(child, name, descr));
    return child;
}


...
addRoot(...);
addChild(parent,...);
...

It works as expected. Now I want to highlight some entrys in this qTreeWidget with a right mouse click. In the C-Tor of my MainWindow I implemented:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ...
    ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->treeWidget, SIGNAL (customContextMenuRequested(const QPoint &)), this, SLOT (rightClick(const QPoint &)));
    ...
}

and the slot is

void MainWindow::rightClick(const QPoint &pt)
{
    QModelIndex idx = ui->treeWidget->indexAt(pt);
    if (idx.isValid())
    {
        qDebug() << idx.data().toString();
        qDebug() << idx << "index.row()" << idx.row() << " index.column()" << idx.column();
    }
}

The slot is called as expected (the qDebug works), but how can I get from the QModelIndex idx to the corresponding QTreeWidgetItem for the highlighting? Or is there another way to make the highligt / change the color of the element?

Thanks in advance!!


Solution

  • Change the color of the cell:

    A possible solution is to use the setData() method:

    void MainWindow::rightClick(const QPoint &pt)
    {
        QModelIndex idx = ui->treeWidget->indexAt(pt);
        if (idx.isValid())
        {
            ui->treeWidget->model()->setData(idx, QColor("red"), Qt::BackgroundRole);
        }
    }
    

    But that will only change one cell, if you want to change the color then the entire row will have to be traversed:

    Change the row color:

    void MainWindow::rightClick(const QPoint &pt)
    {
        QModelIndex idx = ui->treeWidget->indexAt(pt);
        if (idx.isValid())
        {
            for(int c=0; c < ui->treeWidget->columnCount(); ++c){
                QModelIndex ix = idx.sibling(idx.row(), c);
                ui->treeWidget->model()->setData(ix, QColor("red"), Qt::BackgroundRole);
            }
        }
    }
    

    Or:

    void MainWindow::rightClick(const QPoint &pt)
    {
        QTreeWidgetItem *it = ui->treeWidget->itemAt(pt);
        if (it)
        {
            for(int c=0; c < ui->treeWidget->columnCount(); ++c){
                it->setBackground(c, QColor("red"));
            }
        }
    }