Hi,
I'm using a QTreeWidget to display a list of items. I want to avoid selecting items when the mouse right button is pressed on the widget. I tried several approaches:
1. using eventFitler:
myTreeWidget->installEventFilter(this);
bool MyClassViewsWidget
::eventFilter(QObject *obj,
QEvent *event
) {
if (event
->type
() == QEvent::ContextMenu) {
QMouseEvent *mouseEvent
= static_cast<QMouseEvent
*>
(event
);
if (mouseEvent)
{
return true;
}
}
return false;
}
myTreeWidget->installEventFilter(this);
bool MyClassViewsWidget::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::ContextMenu)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
if (mouseEvent)
{
return true;
}
}
return false;
}
To copy to clipboard, switch view to plain text mode
but still the items got selected. Then I used a global variable that would help me keep track if the right button was pressed:
bool rightButtonPressed = false;
bool MyClassViewsWidget
::eventFilter(QObject *obj,
QEvent *event
) {
if (event
->type
() == QEvent::ContextMenu) {
QMouseEvent *mouseEvent
= static_cast<QMouseEvent
*>
(event
);
if (mouseEvent)
{
rightButtonPressed = true;
return true;
}
}
return false;
}
bool rightButtonPressed = false;
bool MyClassViewsWidget::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::ContextMenu)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
if (mouseEvent)
{
rightButtonPressed = true;
return true;
}
}
return false;
}
To copy to clipboard, switch view to plain text mode
and then ask in the itemPressed signal if this variable is true, if it is then don't apply changes:
connect(myTreeWidget,
SIGNAL(itemPressed
(QTreeWidgetItem*,
int)),
SLOT(onItemPressed
()));
void MyClassViewsWidget::onItemPressed()
{
if (!rightButtonPressed )
{
applyChanges();
}
else
{
rightButtonPressed = false;
}
}
connect(myTreeWidget, SIGNAL(itemPressed(QTreeWidgetItem*,int)), SLOT(onItemPressed()));
void MyClassViewsWidget::onItemPressed()
{
if (!rightButtonPressed )
{
applyChanges();
}
else
{
rightButtonPressed = false;
}
}
To copy to clipboard, switch view to plain text mode
then I realized that the itemPressed signal is being called first than eventFilter, which it won't allow me to change the state of the global variable.
2. Then I used :
connect(myTreeWidget,
SIGNAL(customContextMenuRequested
(const QPoint &)),
SLOT(onCustomContextMenu
(const QPoint &)));
void DtObserverViewsWidget::onCustomContextMenu(const QPoint& point)
{
std::cout << "onCustomContextMenu(const QPoint& point)" << std::endl; // this was only a test
}
connect(myTreeWidget, SIGNAL(customContextMenuRequested(const QPoint &)),SLOT(onCustomContextMenu(const QPoint &)));
void DtObserverViewsWidget::onCustomContextMenu(const QPoint& point)
{
std::cout << "onCustomContextMenu(const QPoint& point)" << std::endl; // this was only a test
}
To copy to clipboard, switch view to plain text mode
but again, the itemPressed signal gets called first than customContextMenuRequested.
Is there any way to control the mouse right button event to not select any item in the QTreeView?
What other approach can I try?
Bookmarks