QTreeView on_item_clicked and on_item_doubleclicked
-
Hello,
I have a QTreeView widget that is populated by a database. I have a couple of functions defined to handle the items single and double click. Only problem is, they are triggered by any button on the mouse. Right, Left, and Scroll wheel will fire the appropriate code. My question is, how can I determine within the on_xxx slot which button fired the event, Right, Left or Center. Is this possible without a massive amount of recoding?
void amsSupport::on_amsSupportTreeViewWidget_itemClicked(QTreeWidgetItem *item, int column) { Q_UNUSED(column) if(item->parent() != nullptr) { ui->amsSupportTreeViewWidget->setSelectionMode(QAbstractItemView::MultiSelection); ui->amsSupportReturnButton->setEnabled(true); ui->amsSupportEditButton->setEnabled(false); ui->amsSupportDeleteButton->setEnabled(false); ui->amsSupportPrintButton->setEnabled(false); ui->amsSupportEmailButton->setEnabled(false); } else { ui->amsSupportTreeViewWidget->setSelectionMode(QAbstractItemView::SingleSelection); ui->amsSupportTreeViewWidget->selectionModel()->clearSelection(); ui->amsSupportEditButton->setEnabled(true); ui->amsSupportDeleteButton->setEnabled(true); ui->amsSupportPrintButton->setEnabled(true); ui->amsSupportReturnButton->setEnabled(false); ui->amsSupportEmailButton->setEnabled(true); item->setSelected(true); } }
What I am trying to accomplish is only selecting the appropriate line if the Left mouse button was pressed.
Using Qt 6.4.3 on Windows 10 mingw compiler.
Thanks in advance.
-
Hello,
I have a QTreeView widget that is populated by a database. I have a couple of functions defined to handle the items single and double click. Only problem is, they are triggered by any button on the mouse. Right, Left, and Scroll wheel will fire the appropriate code. My question is, how can I determine within the on_xxx slot which button fired the event, Right, Left or Center. Is this possible without a massive amount of recoding?
void amsSupport::on_amsSupportTreeViewWidget_itemClicked(QTreeWidgetItem *item, int column) { Q_UNUSED(column) if(item->parent() != nullptr) { ui->amsSupportTreeViewWidget->setSelectionMode(QAbstractItemView::MultiSelection); ui->amsSupportReturnButton->setEnabled(true); ui->amsSupportEditButton->setEnabled(false); ui->amsSupportDeleteButton->setEnabled(false); ui->amsSupportPrintButton->setEnabled(false); ui->amsSupportEmailButton->setEnabled(false); } else { ui->amsSupportTreeViewWidget->setSelectionMode(QAbstractItemView::SingleSelection); ui->amsSupportTreeViewWidget->selectionModel()->clearSelection(); ui->amsSupportEditButton->setEnabled(true); ui->amsSupportDeleteButton->setEnabled(true); ui->amsSupportPrintButton->setEnabled(true); ui->amsSupportReturnButton->setEnabled(false); ui->amsSupportEmailButton->setEnabled(true); item->setSelected(true); } }
What I am trying to accomplish is only selecting the appropriate line if the Left mouse button was pressed.
Using Qt 6.4.3 on Windows 10 mingw compiler.
Thanks in advance.
@Chrisw01
The short answer is: No, the slot doesn't know which button has caused the signal to be fired.
But you could install an event filter and ignore the unwanted buttons, even depending on the mouse position. -
Hello,
I have a QTreeView widget that is populated by a database. I have a couple of functions defined to handle the items single and double click. Only problem is, they are triggered by any button on the mouse. Right, Left, and Scroll wheel will fire the appropriate code. My question is, how can I determine within the on_xxx slot which button fired the event, Right, Left or Center. Is this possible without a massive amount of recoding?
void amsSupport::on_amsSupportTreeViewWidget_itemClicked(QTreeWidgetItem *item, int column) { Q_UNUSED(column) if(item->parent() != nullptr) { ui->amsSupportTreeViewWidget->setSelectionMode(QAbstractItemView::MultiSelection); ui->amsSupportReturnButton->setEnabled(true); ui->amsSupportEditButton->setEnabled(false); ui->amsSupportDeleteButton->setEnabled(false); ui->amsSupportPrintButton->setEnabled(false); ui->amsSupportEmailButton->setEnabled(false); } else { ui->amsSupportTreeViewWidget->setSelectionMode(QAbstractItemView::SingleSelection); ui->amsSupportTreeViewWidget->selectionModel()->clearSelection(); ui->amsSupportEditButton->setEnabled(true); ui->amsSupportDeleteButton->setEnabled(true); ui->amsSupportPrintButton->setEnabled(true); ui->amsSupportReturnButton->setEnabled(false); ui->amsSupportEmailButton->setEnabled(true); item->setSelected(true); } }
What I am trying to accomplish is only selecting the appropriate line if the Left mouse button was pressed.
Using Qt 6.4.3 on Windows 10 mingw compiler.
Thanks in advance.
@Chrisw01
Additional to @Axel-Spoerl and alternative to an event filter, if you wanted to know what button was clicked I think you would have to make use ofQTreeView::mousePress
/ReleaseEvent()
to inspect theQMouseEvent
rather then just theclicked
signal. -
@Chrisw01
Additional to @Axel-Spoerl and alternative to an event filter, if you wanted to know what button was clicked I think you would have to make use ofQTreeView::mousePress
/ReleaseEvent()
to inspect theQMouseEvent
rather then just theclicked
signal.Hi and thanks for the replies.
I was afraid of that. I added some debug code to the function and its kind of weird. The on_clicked seams to be working fine. It will fire on the press of the left mouse button but not the center/roller or right button. I'd say that is normal behavior. But the on_doubleclicked function will fire on a double click of any of the mouse buttons. Not sure if that is right or not. What my issue is, my context menu code will not fire on a right click. I thought that perhaps the on_clicked/doubleclicked was intercepting the right click. Back to the books!
[EDIT]
One other thing I noticed just now is if you Left click an item followed by a Right click it will fire the on_doubleClicked code as well, I don't think that is proper either.Thanks again...
-
Hello,
I have a QTreeView widget that is populated by a database. I have a couple of functions defined to handle the items single and double click. Only problem is, they are triggered by any button on the mouse. Right, Left, and Scroll wheel will fire the appropriate code. My question is, how can I determine within the on_xxx slot which button fired the event, Right, Left or Center. Is this possible without a massive amount of recoding?
void amsSupport::on_amsSupportTreeViewWidget_itemClicked(QTreeWidgetItem *item, int column) { Q_UNUSED(column) if(item->parent() != nullptr) { ui->amsSupportTreeViewWidget->setSelectionMode(QAbstractItemView::MultiSelection); ui->amsSupportReturnButton->setEnabled(true); ui->amsSupportEditButton->setEnabled(false); ui->amsSupportDeleteButton->setEnabled(false); ui->amsSupportPrintButton->setEnabled(false); ui->amsSupportEmailButton->setEnabled(false); } else { ui->amsSupportTreeViewWidget->setSelectionMode(QAbstractItemView::SingleSelection); ui->amsSupportTreeViewWidget->selectionModel()->clearSelection(); ui->amsSupportEditButton->setEnabled(true); ui->amsSupportDeleteButton->setEnabled(true); ui->amsSupportPrintButton->setEnabled(true); ui->amsSupportReturnButton->setEnabled(false); ui->amsSupportEmailButton->setEnabled(true); item->setSelected(true); } }
What I am trying to accomplish is only selecting the appropriate line if the Left mouse button was pressed.
Using Qt 6.4.3 on Windows 10 mingw compiler.
Thanks in advance.
@Chrisw01 said in QTreeView on_item_clicked and on_item_doubleclicked:
My question is, how can I determine within the on_xxx slot which button fired the event, Right, Left or Center.
See QGuiApplication::mouseButtons() - it will work as long as you don't use a QueuedConnection.
-
@Chrisw01 said in QTreeView on_item_clicked and on_item_doubleclicked:
My question is, how can I determine within the on_xxx slot which button fired the event, Right, Left or Center.
See QGuiApplication::mouseButtons() - it will work as long as you don't use a QueuedConnection.
@Christian-Ehrlicher said in QTreeView on_item_clicked and on_item_doubleclicked:
See
QGuiApplication::mouseButtons()
Wow, never knew about that one!!
-
@Chrisw01 said in QTreeView on_item_clicked and on_item_doubleclicked:
My question is, how can I determine within the on_xxx slot which button fired the event, Right, Left or Center.
See QGuiApplication::mouseButtons() - it will work as long as you don't use a QueuedConnection.
Thanks, for that information, that (although I still think the on_doubleClick function should operate the same as the on_click function), provided me a way of making sure it was the left button firing the event.
Thanks everyone for your input..
-