0

I have a QTreeWidget with rows having some colors. When I try to select row (using mouse click) row's color changes to blue.

enter image description here

I have clicked on middle row
To avoid change in color, I used following property of QTreeWidget

ui->treeWidget->setSelectionMode(QAbstractItemView::NoSelection);

After adding this, color changing issue was solved.

enter image description here

(I have clicked on middle row )

But because of this, another issue came up. By adding

ui->treeWidget->setSelectionMode(QAbstractItemView::NoSelection);

which means, there will not be any selection. And due to this, on Right mouse click, I have some options ( copy text , copy hierarichal path etc ) are not working. (Because row is not getting selected )

How to solve above problem ?

Here is my code :

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    CreateActions();
    CreateRightClickMenu();
    InitializeTree();
    displayTree();
}        

void MainWindow::displayTree()
{

    ui->treeWidget->setColumnCount(4);
    QStringList labels;
    labels << "Instance" << "Domains" << "Model" <<"Model Type";
    ui->treeWidget->setHeaderLabels(labels);
    ui->treeWidget->setSelectionMode(QAbstractItemView::NoSelection);
    QTreeWidgetItem* root = new QTreeWidgetItem(ui->treeWidget);

    root->setText(0,"top");
    root->setText(2,"top");
    root->setText(3,"Module");

    ui->treeWidget->addTopLevelItem(root);
    QTreeWidgetItem* child1 = new QTreeWidgetItem();
    child1->setText(0,"U1");
    child1->setText(1,"PD1");
    child1->setText(2,"lvds_system");
    child1->setText(3,"Module");

    root->addChild(child1);

    // adding more data in tree same as above
}

 void MainWindow:: CreateAction()
    {
        _copy= new QAction("Copy", this);
        // some other options       
        connect(_copy, SIGNAL(triggered()), this, SLOT(CopyName()));      
               
       QList<QAction *> actions;
       actions.append(_copy);
       //some other actions is added         
           
       addActions(actions); 
    }
               
    void MainWindow:: CreateRightClickMenu()
    {
          _rightClickMenu = new QMenu(this);
          _rightClickMenu->addAction(_copy);
    }             
      
      
void MainWindow::InitializTree()
{
     ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);       
     // some layout code 
} 

    

MainWindow.h

   QMenu* _rightClickMenu;
   QAction* _copy;            
  
CopyName()
{
    const QList<QTreeWidgetItem*>& select = tree->selectedItems();
    if (select.size() > 0) {
        QTreeWidgetItem* item = select.at(0);
        // operation on item
}
tushar
  • 313
  • 4
  • 10
  • @Nicolas Holthaus : I saw your solution from How to remove default blue selection from QTreeView. https://stackoverflow.com/questions/28366805/remove-blue-selection-from-qtreeview-in-fusion-style I applied what you said in style sheet. But it did not work. My row is fully white and there is no text. I want to keep row color as it is. Can you help me in this ? – tushar Oct 11 '22 at 07:57
  • @Matt Guerrette : I saw your solution from How to remove default blue selection from QTreeView. https://stackoverflow.com/questions/28366805/remove-blue-selection-from-qtreeview-in-fusion-style I did what you said in answer. But it did not work out. I want to remove default blue selection and want to keep my current row color intact. Can you help me ? – tushar Oct 11 '22 at 08:02
  • Quoting: "And due to this, on Right mouse click, I have some options ( copy text , copy hierarichal path etc ) are not working. (Because row is not getting selected )" ... well, I do not see this functionality in the code snippet you showed. If you add it there, maybe we can find some workaround. – HiFile.app - best file manager Oct 11 '22 at 09:33
  • @HiFile.app-bestfilemanager Issue is not Right mouse options are not working. I know, why they are not working. Because I have set `setSelectionMode(QAbstractItemView::NoSelection);` and here NoSelection means, row is not going to select. And if row is not going to select then your Right mouse option is not going to work. – tushar Oct 11 '22 at 10:05
  • @HiFile.app-bestfilemanager And if we remove (root cause) `setSelectionMode(QAbstractItemView::NoSelection);` then , default blue selection of `QTreeWidget` appears. So main issue is How to remove default blue selection of QTreeWidget ? – tushar Oct 11 '22 at 10:07
  • Even if there is no selection, the tree widget still has the current item `QTreeWidget::currentItem()`. In other words, current item and selection are two different things. And you can invoke some actions for this current item as a reaction to right click. But because I do not see how you are handling the reaction to the right mouse button, I cannot tell you how to change it to work with the current item instead of the selection. – HiFile.app - best file manager Oct 11 '22 at 10:19
  • The idea that you somehow just make the blue background color transparent so that you do not see it, is not a good idea. Then the user will not see which row is selected, right? But the selection will still be there... The right way is that if you do not want the selections, then just switch them off with `setSelectionMode(QAbstractItemView::NoSelection)`. – HiFile.app - best file manager Oct 11 '22 at 10:22
  • @HiFile.app-bestfilemanager I have added Right mouse option code – tushar Oct 11 '22 at 10:48
  • @HiFile.app-bestfilemanager I tried to hide default blue color selection through style sheet using `background-color : transparent` but because of this, my full row became white. I tried various style sheet to over come blue color selection and retain my original row color but did not succeed. Then I found `NoSelection` option. But now Right mouse options are not working. I just want to retain my `Original color of rows` and when user hover / click on rows, user should get feel of selection. – tushar Oct 11 '22 at 10:53
  • If you want to alter the appearance of one or more cells in a view you should probably look at using a custom [item delegate](https://doc.qt.io/qt-6/qabstractitemdelegate.html). – G.M. Oct 11 '22 at 11:01
  • @tushar@tushar Yes, I can see the action and the menu... but how are you invoking it? Where is the code which leads from right-click event (or context menu event) to showing the menu? ... I think there is some misunderstanding. If you want to see some menu, you really do not need any selection, you can just display the menu for the current item. (Note: this does not apply ONLY if you need multiselections. In that case of course you cannot switch off the selections. So - do you need multiselections?) – HiFile.app - best file manager Oct 11 '22 at 11:08
  • @tushar And if you want multiselections and still need to keep the original colour of the rows, then as G.M. suggests you need to write your own custom item delegate. – HiFile.app - best file manager Oct 11 '22 at 11:10
  • @HiFile.app-bestfilemanager I have updated the code. Sorry for mis understanding but I can see all the Right mouse options but none is working. I do not want multi selection. On click, single row should get selected ( or should get selection feel ) and my row color should get preserved. – tushar Oct 11 '22 at 11:46
  • @HiFile.app-bestfilemanager Now I understood what you were trying to say. I have updated my `CopyName()` slot. When from Right mouse option I click on `copy`, `CopyName()` runs. And I found that, `select.size == 0`. And that's why, I dont get output. How to select row now ? – tushar Oct 12 '22 at 05:51

0 Answers0