I have a QTreeWidget
with rows having some colors. When I try to select row (using mouse click) row's color changes to blue.
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.
(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
}