Results 1 to 3 of 3

Thread: QTreeWidget - no selection with mouse right click

  1. #1

    Default QTreeWidget - no selection with mouse right click

    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:


    Qt Code:
    1. myTreeWidget->installEventFilter(this);
    2.  
    3. bool MyClassViewsWidget::eventFilter(QObject *obj, QEvent *event)
    4. {
    5.  
    6. if (event->type() == QEvent::ContextMenu)
    7. {
    8. QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
    9.  
    10. if (mouseEvent)
    11. {
    12.  
    13. return true;
    14. }
    15.  
    16. }
    17. return false;
    18.  
    19. }
    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:

    Qt Code:
    1. bool rightButtonPressed = false;
    2.  
    3. bool MyClassViewsWidget::eventFilter(QObject *obj, QEvent *event)
    4. {
    5.  
    6. if (event->type() == QEvent::ContextMenu)
    7. {
    8. QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
    9.  
    10. if (mouseEvent)
    11. {
    12.  
    13. rightButtonPressed = true;
    14. return true;
    15. }
    16.  
    17. }
    18. return false;
    19.  
    20. }
    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:

    Qt Code:
    1. connect(myTreeWidget, SIGNAL(itemPressed(QTreeWidgetItem*,int)), SLOT(onItemPressed()));
    2.  
    3. void MyClassViewsWidget::onItemPressed()
    4. {
    5. if (!rightButtonPressed )
    6. {
    7. applyChanges();
    8. }
    9. else
    10. {
    11. rightButtonPressed = false;
    12. }
    13.  
    14. }
    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 :

    Qt Code:
    1. connect(myTreeWidget, SIGNAL(customContextMenuRequested(const QPoint &)),SLOT(onCustomContextMenu(const QPoint &)));
    2.  
    3.  
    4. void DtObserverViewsWidget::onCustomContextMenu(const QPoint& point)
    5. {
    6. std::cout << "onCustomContextMenu(const QPoint& point)" << std::endl; // this was only a test
    7. }
    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeWidget - no selection with mouse right click

    Try installing the event filter on the tree widget's viewport().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3

    Default Re: QTreeWidget - no selection with mouse right click

    Hi, At the end I just extended from QTreeWidget and re implement function mousePressEvent



    Qt Code:
    1. class MyQtreeView : public QTreeWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. MyQtreeView (QWidget *parent = 0);
    6. protected:
    7. virtual void mousePressEvent(QMouseEvent *event);
    8. };
    To copy to clipboard, switch view to plain text mode 

    and in the cpp file:


    Qt Code:
    1. void MyQtreeView ::mousePressEvent(QMouseEvent *event)
    2. {
    3. if (event->button() != Qt::RightButton)
    4. {
    5. QTreeWidget::mousePressEvent(event);
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 5
    Last Post: 15th February 2016, 20:30
  2. Replies: 4
    Last Post: 19th August 2013, 09:53
  3. Replies: 2
    Last Post: 16th July 2012, 12:40
  4. QTreeWidget selection
    By adhit in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2008, 09:08
  5. QTreeWidget selection
    By Arthur in forum Qt Programming
    Replies: 3
    Last Post: 21st March 2007, 05:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.