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. Accessing a class containing Mouse ClickEvents from another class
Forum Updated to NodeBB v4.3 + New Features

Accessing a class containing Mouse ClickEvents from another class

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 478 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.
  • L Offline
    L Offline
    leinad
    wrote on last edited by
    #1

    Hi,

    I have several classes that create and process QTableWidgets. They all work fine. I'm adding another capability to allow users to hightlight rows in the table and use CNTRL-C to copy those rows to the clipboard where I can paste to a text file, excel, etc.

    If I use the method below within the class where I create and manage the tableWidget, it works fine. However, since I have many classes that contain different tableWidgets, I would like to create a separate class that contains the code below so I can reference the that code without keeping multiple copies of the same code in different classes. This way if I make a change, it is in one place.

    Would someone know how to do this? When I create a class and use the logic below, typing CNTRL-C does not enter the keyPressEvent method as if I had it in the same class where the tableWidget is managed. The copyTextFromTableWidgetToClipBoard() method juts scans the table and see what is highlighted and copies to the clipboard. This works fine. I just can't figure out how to access the keyPressEvent method.

    keyPressEvent(QKeyEvent* event)
    {
    // If Ctrl-C typed
    // Or use event->matches(QKeySequence::Copy)
    if (event->key() == Qt::Key_C && (event->modifiers() & Qt::ControlModifier))
    {
    copyTextFromTableWidgetToClipBoard();
    }

    }

    Thanks!

    jsulmJ 1 Reply Last reply
    0
    • L Offline
      L Offline
      leinad
      wrote on last edited by
      #8

      Sure enough the event filter worked! Thanks.

      1 Reply Last reply
      1
      • L leinad

        Hi,

        I have several classes that create and process QTableWidgets. They all work fine. I'm adding another capability to allow users to hightlight rows in the table and use CNTRL-C to copy those rows to the clipboard where I can paste to a text file, excel, etc.

        If I use the method below within the class where I create and manage the tableWidget, it works fine. However, since I have many classes that contain different tableWidgets, I would like to create a separate class that contains the code below so I can reference the that code without keeping multiple copies of the same code in different classes. This way if I make a change, it is in one place.

        Would someone know how to do this? When I create a class and use the logic below, typing CNTRL-C does not enter the keyPressEvent method as if I had it in the same class where the tableWidget is managed. The copyTextFromTableWidgetToClipBoard() method juts scans the table and see what is highlighted and copies to the clipboard. This works fine. I just can't figure out how to access the keyPressEvent method.

        keyPressEvent(QKeyEvent* event)
        {
        // If Ctrl-C typed
        // Or use event->matches(QKeySequence::Copy)
        if (event->key() == Qt::Key_C && (event->modifiers() & Qt::ControlModifier))
        {
        copyTextFromTableWidgetToClipBoard();
        }

        }

        Thanks!

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @leinad said in Accessing a class containing Mouse ClickEvents from another class:

        copyTextFromTableWidgetToClipBoard

        You could move this method into a common parent class and subclass all your classes with QTableWidgets from that common parent class.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • L Offline
          L Offline
          leinad
          wrote on last edited by
          #3

          Thanks but the folks I'm working with would prefer that I don't subclass QTableWidget. Is there another way?

          jsulmJ 1 Reply Last reply
          0
          • L leinad

            Thanks but the folks I'm working with would prefer that I don't subclass QTableWidget. Is there another way?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @leinad Depends what exactly it is doing. Maybe it also could simply be a function with some parameters...

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • L Offline
              L Offline
              leinad
              wrote on last edited by
              #5

              I made it a simple class with the keyPressEvent. The problem is when I hit the keys, it is not recognized. Would an eventHandler work better?

              JonBJ 1 Reply Last reply
              0
              • L leinad

                I made it a simple class with the keyPressEvent. The problem is when I hit the keys, it is not recognized. Would an eventHandler work better?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #6

                @leinad said in Accessing a class containing Mouse ClickEvents from another class:

                I made it a simple class with the keyPressEvent. The problem is when I hit the keys, it is not recognized.

                Don't know what exactly you have in mind. Just having some class with a method named keyPressEvent won't hook up to QTableWidget::keyPressEvent() virtual method.

                So far as I know there will only be two possible/useful ways of "intercepting" a key press on a widget:

                • Subclass the widget and override virtual QTableWidget::keyPressEvent().
                • Install an event filter to deal with the key press, which avoids having to subclass.

                Actually the full, more complex details can be read in QCoreApplication::notify().

                1 Reply Last reply
                2
                • L Offline
                  L Offline
                  leinad
                  wrote on last edited by
                  #7

                  Thanks, I'm trying to use an event filter to see if I can get around the sub-classing.

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    leinad
                    wrote on last edited by
                    #8

                    Sure enough the event filter worked! Thanks.

                    1 Reply Last reply
                    1
                    • L leinad has marked this topic as solved on

                    • Login

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