Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Handle Left-Right Double Click on QListWidget
Qt 6.11 is out! See what's new in the release blog

Handle Left-Right Double Click on QListWidget

Scheduled Pinned Locked Moved General and Desktop
13 Posts 3 Posters 8.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello,

    Qt doesn't see a difference between a Double Left Click and a Double Right Click, and I would like to do different things depending on the Double Click. So I had to code that on my own but that didn't work.

    My problem is that I have a QListWidget A, a QListWidget B and a QListWidget C. Whenever I Double Left Click an item in QListWidget A, I would like it to be added to QListWidget B. But if I Double Right Click it, I would like it to be added to QListWidget C instead.

    So this is what I did:

    @QListWidget searchList;
    searchList.setParent(&mainWidget);
    QObject::connect(&searchList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(MoveCard(QListWidgetItem*)));

    ClickEater eater;
    searchList.installEventFilter(&eater);@

    (moveItem is the slot to be used that I implemented myself for double clicks)

    ClickEater has the following definition:

    @#include <QMouseEvent>

    class ClickEater : public QObject
    {
    Q_OBJECT

    public:
     
    bool eventFilter(QObject*, QEvent*);
    bool Goto;
    

    };@

    and eventFilter has the following definition:

    @ bool ClickEater::eventFilter(QObject obj, QEvent event)
    {
    QMouseEvent
    truc = (QMouseEvent
    ) event;

    if (truc->type() == QEvent::MouseButtonPress) 
    {
        if (truc->button() == Qt::LeftButton)
            Goto = false;
        else if (truc->button() == Qt::RightButton)
            Goto = true;
    } 
     else
        return QObject::eventFilter(obj, event);
    
    return true;
    

    }@

    That way, whenever I (simple) click an item of QListWidget A, Goto is set to true or false. If I double click it, the last value of Goto will tell me what kind of double click it was. But that doesn't work. Even if I cast event into a QMouseEvent, I still never enter "if" nor "else". Help please!

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi, welcome to devnet.

      First of all this code is not ok:
      @
      QMouseEvent* truc = (QMouseEvent*) event;
      if (truc->type() == QEvent::MouseButtonPress)
      @
      You shouldn't cast first and then check for event type. The C type cast will always succeed, even if it's not a correct type (i.e. reinterpret_cast). From there it's undefined behavior. This should be
      @
      if (event->type() == QEvent::MouseButtonPress)
      {
      QMouseEvent* truc = static_cast<QMouseEvent*>(event);
      ...
      @

      But anyway there's no need for an event filter here. You can check which button was pressed a lot easier:
      @
      //assuming this is your slot
      void Whatever::moveCard(QListWidgetItem* item)
      {
      auto buttons = qApp->mouseButtons();
      if(buttons & Qt::LeftButton)
      //do something
      else if(buttons & Qt::RightButton)
      //do something else
      }
      @

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Hello,

        thank you for your fast reply. This was the code I tested:

        @void Interface::moveCard(QListWidgetItem* Item)
        {
        auto buttons = qApp->mouseButtons();
        std::cout << "??" << std::endl;

        if(buttons & Qt::LeftButton)
        std::cout << "ha" << std::endl;
        else if(buttons & Qt::RightButton)
        std::cout << "ho" << std::endl;
        }@

        But unfortunately, I never see "ha" nor "ho" :( Just:

        @??
        ??
        ??
        ??
        ??@

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          I still need help I'm afraid :(

          1 Reply Last reply
          0
          • Chris KawaC Online
            Chris KawaC Online
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            This method works and I checked it before posting so there must be something else in your app interfering with it. Can you post somewhere the complete project or, better yet, a small reproducible code that doesn't work with this method?

            Oh, I forgot to ask which OS are you on. On Windows the double-click signal is emitted before the final release so mouseButtons() has meaningful info. This might not be the case on other platforms.

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              I am using X-ubuntu (latest LTS).

              Well what is qApp exactly? I do declare in main

              @QApplication app(argc, argv);@

              but I can't rename app into qApp for some reason.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                qApp is a macro used to get the current instance of your QApplication. "Here":http://qt-project.org/doc/qt-5/qapplication.html#qApp for more informations

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  I see thanks.

                  So does this mean my problem has no solutions on Linux?

                  1 Reply Last reply
                  0
                  • Chris KawaC Online
                    Chris KawaC Online
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Maybe you can subclass the list widget, reimplement the mouse press/release event and emit a custom signal that would indicate which button was pressed.

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #10

                      Oh okay. I'm a newbie regarding Qt unfortunately, I would really need to see very similar examples so that I could get this done.

                      This is surprising that so simple a problem needs such complicated solutions, though.

                      1 Reply Last reply
                      0
                      • Chris KawaC Online
                        Chris KawaC Online
                        Chris Kawa
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Well it might be a simple case for you but right button double clicks are not something you see every day. I would even dare to say that's unintuitive.

                        For some info and examples on overloading event handlers you can go "here":http://qt-project.org/doc/qt-5/eventsandfilters.html#event-handlers
                        There's also a lot of that stuff on the net. Just look for "qt overriding mousepressevent" eg. "here":http://stackoverflow.com/questions/2733668/qwidget-keypressevent-override

                        1 Reply Last reply
                        0
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #12

                          Okay before trying the long way to do it, I replaced the itemDoubleClicked signal by simply itemClicked. I still print neither "haha" nor "hoho" after the second click (when I double click). Are you sure Ubuntu is the problem here?

                          buttons is always equal to 0 (when I print it)

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            If you really want to implement the non-standard double right click (you might even be going against the guidelines from Apple/Microsft/etc.), you should rather look at the mouse events functions.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt
                            • Unsolved