Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to send mouse clicks to a `QTreeWidgetItem` in my test?
QtWS25 Last Chance

How to send mouse clicks to a `QTreeWidgetItem` in my test?

Scheduled Pinned Locked Moved Solved General and Desktop
testingqtreewidgetitemmouse control
11 Posts 2 Posters 6.4k Views
  • 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.
  • J Offline
    J Offline
    Jakob
    wrote on 7 Dec 2015, 10:40 last edited by
    #1

    I wish to test the response of my application to mouse clicks on single QTreeWidgetItems which are configured to be user selectable.

    Objects that derive from QWidget have the click()-method, but the QTreeWidgetItem does not derive from QWidget hence doesn't have that method. Similarly, also the QTest::mouseClick() method can only act on a QWidget-derived class. So these two routes don't work.

    What route will work?

    J 1 Reply Last reply 7 Dec 2015, 14:32
    0
    • K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 7 Dec 2015, 12:52 last edited by kshegunov 12 Jul 2015, 12:52
      #2

      Hello,
      QTreeWidgetItem doesn't derive from QObject at all. If they did you probably could imagine the amount of overhead it would generate for a simple table/tree widget. I think for your case, you could use the QTreeWidget::itemClicked signal.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      J 1 Reply Last reply 7 Dec 2015, 13:14
      0
      • K kshegunov
        7 Dec 2015, 12:52

        Hello,
        QTreeWidgetItem doesn't derive from QObject at all. If they did you probably could imagine the amount of overhead it would generate for a simple table/tree widget. I think for your case, you could use the QTreeWidget::itemClicked signal.

        Kind regards.

        J Offline
        J Offline
        Jakob
        wrote on 7 Dec 2015, 13:14 last edited by
        #3

        @kshegunov Hej, that is a signal, so not useful for me - I want to simulate the click that eventually produces that signal.

        K 1 Reply Last reply 7 Dec 2015, 13:18
        0
        • J Jakob
          7 Dec 2015, 13:14

          @kshegunov Hej, that is a signal, so not useful for me - I want to simulate the click that eventually produces that signal.

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 7 Dec 2015, 13:18 last edited by
          #4

          @Jakob
          Oh, sorry, I'm still sleeping I guess. You could invoke the signal through QMetaObject::invokeMethod by passing your widget as sender and the needed arguments. Hope that helps.

          Kind regards.

          Read and abide by the Qt Code of Conduct

          J 1 Reply Last reply 7 Dec 2015, 13:38
          0
          • K kshegunov
            7 Dec 2015, 13:18

            @Jakob
            Oh, sorry, I'm still sleeping I guess. You could invoke the signal through QMetaObject::invokeMethod by passing your widget as sender and the needed arguments. Hope that helps.

            Kind regards.

            J Offline
            J Offline
            Jakob
            wrote on 7 Dec 2015, 13:38 last edited by
            #5

            @kshegunov Hm, that still doesn't do it - the main issue is that simply invoking the slot, will not change the checkState of the item, which is what I need

            K 1 Reply Last reply 7 Dec 2015, 13:53
            0
            • J Jakob
              7 Dec 2015, 13:38

              @kshegunov Hm, that still doesn't do it - the main issue is that simply invoking the slot, will not change the checkState of the item, which is what I need

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 7 Dec 2015, 13:53 last edited by
              #6

              @Jakob
              You can change the state externally. Something like this:

              void MyClass::clickItem(QTreeWidget * widget, QPoint clickPosition, int checkableColumn)
              {
                  QTreeWidgetItem * item = widget->itemAt(clickPosition);
                  item->setCheckState(checkableColumn, item->checkState() == Qt::Checked ? Qt::Unchecked : Qt::Checked);
                  QMetaObject::invokeMethod(widget, "itemClicked", Q_ARG(QTreeWidgetItem *, item), QARG(int, checkableColumn));
              }
              

              Read and abide by the Qt Code of Conduct

              J 1 Reply Last reply 9 Dec 2015, 15:26
              0
              • J Jakob
                7 Dec 2015, 10:40

                I wish to test the response of my application to mouse clicks on single QTreeWidgetItems which are configured to be user selectable.

                Objects that derive from QWidget have the click()-method, but the QTreeWidgetItem does not derive from QWidget hence doesn't have that method. Similarly, also the QTest::mouseClick() method can only act on a QWidget-derived class. So these two routes don't work.

                What route will work?

                J Offline
                J Offline
                Jakob
                wrote on 7 Dec 2015, 14:32 last edited by
                #7

                Hm, I just realized the click() method is only on QAbstractButton, not on a generic QWidget. Nevertheless, I really would want to use QTest::mouseClick() to toggle the state of the checkbox of a QTreeWidgetItem.

                1 Reply Last reply
                0
                • K kshegunov
                  7 Dec 2015, 13:53

                  @Jakob
                  You can change the state externally. Something like this:

                  void MyClass::clickItem(QTreeWidget * widget, QPoint clickPosition, int checkableColumn)
                  {
                      QTreeWidgetItem * item = widget->itemAt(clickPosition);
                      item->setCheckState(checkableColumn, item->checkState() == Qt::Checked ? Qt::Unchecked : Qt::Checked);
                      QMetaObject::invokeMethod(widget, "itemClicked", Q_ARG(QTreeWidgetItem *, item), QARG(int, checkableColumn));
                  }
                  
                  J Offline
                  J Offline
                  Jakob
                  wrote on 9 Dec 2015, 15:26 last edited by
                  #8

                  @kshegunov This is indeed what I turned out doing, except that I directly call the signal - apparently that is possible ...... Still not quite what I want but what I'll stick with for now (my main issue with this approach is that this way I don't have a regression for someone erroneously changing the connect()-call)

                  K 1 Reply Last reply 9 Dec 2015, 17:28
                  0
                  • J Jakob
                    9 Dec 2015, 15:26

                    @kshegunov This is indeed what I turned out doing, except that I directly call the signal - apparently that is possible ...... Still not quite what I want but what I'll stick with for now (my main issue with this approach is that this way I don't have a regression for someone erroneously changing the connect()-call)

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 9 Dec 2015, 17:28 last edited by
                    #9

                    @Jakob
                    Hmmm, I always thought that signals' access specifier is protected ... that is strange.

                    Read and abide by the Qt Code of Conduct

                    J 1 Reply Last reply 10 Dec 2015, 11:46
                    0
                    • K kshegunov
                      9 Dec 2015, 17:28

                      @Jakob
                      Hmmm, I always thought that signals' access specifier is protected ... that is strange.

                      J Offline
                      J Offline
                      Jakob
                      wrote on 10 Dec 2015, 11:46 last edited by
                      #10

                      @kshegunov As it turns out, line 72 in qobjectdefs.h (Qt 5.4.0) says:

                      # define Q_SIGNALS public
                      
                      K 1 Reply Last reply 10 Dec 2015, 12:01
                      0
                      • J Jakob
                        10 Dec 2015, 11:46

                        @kshegunov As it turns out, line 72 in qobjectdefs.h (Qt 5.4.0) says:

                        # define Q_SIGNALS public
                        
                        K Offline
                        K Offline
                        kshegunov
                        Moderators
                        wrote on 10 Dec 2015, 12:01 last edited by kshegunov 12 Oct 2015, 12:04
                        #11

                        @Jakob
                        Ah, yes. From Qt4 you get:
                        # define Q_SIGNALS protected
                        I firmly believe that this change was made to accommodate the new way of connecting signals. I don't find it to be a great improvement, but this is a personal opinion ... In the new framework it seems every object can easily rise another object's signal, which is a bit ... hm ... suspicious ... not that it was impossible before, but you had to go the extra mile, by using the QMetaObject class.

                        Read and abide by the Qt Code of Conduct

                        1 Reply Last reply
                        0

                        2/11

                        7 Dec 2015, 12:52

                        topic:navigator.unread, 9
                        • Login

                        • Login or register to search.
                        2 out of 11
                        • First post
                          2/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved