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. connect pointer of member function as QShortCuts slot
Qt 6.12 LTS is out! See what's new in the release blog

connect pointer of member function as QShortCuts slot

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 1.7k 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
    LeaA
    wrote on last edited by
    #1

    hi,
    I'm trying to write a function that gets a vector of QKeySequence and a pointer to slot function.
    i't compiles but when I press the button the slot doesn't activated.

    here is my code:
    class MyClass:public QWidget
    {
    Q_OBJECT
    public:
    void connectVecToSlot();
    void func(void (MyClass::*slot_func)(),vector<QKeySequence> vec);
    public slots:
    void do_something();
    };

    the implementation of func:
    void MyClass:: func(void (MyClass::*slot_func)(),vector<QKeySequence> vec){
    vector<QKeySequence>::iterator it = vec.begin();
    for ( ; it != vec.end(); ++it){
    new QShortcut(*it,this,SLOT((slot_func)()));
    }
    }

    when i call to func:
    void MyClass:: connectVecToSlot(){
    vector<QKeySequence> vect = getMyVector();//a function that returns a vector of QKeySequence
    func(&MyClass::do_something,vect);
    }

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

      OK,
      I found here the solution:
      http://www.qtcentre.org/threads/8392-How-to-declare-SLOT-as-a-parameter-to-member-function

      1 Reply Last reply
      0
      • L LeaA

        hi,
        I'm trying to write a function that gets a vector of QKeySequence and a pointer to slot function.
        i't compiles but when I press the button the slot doesn't activated.

        here is my code:
        class MyClass:public QWidget
        {
        Q_OBJECT
        public:
        void connectVecToSlot();
        void func(void (MyClass::*slot_func)(),vector<QKeySequence> vec);
        public slots:
        void do_something();
        };

        the implementation of func:
        void MyClass:: func(void (MyClass::*slot_func)(),vector<QKeySequence> vec){
        vector<QKeySequence>::iterator it = vec.begin();
        for ( ; it != vec.end(); ++it){
        new QShortcut(*it,this,SLOT((slot_func)()));
        }
        }

        when i call to func:
        void MyClass:: connectVecToSlot(){
        vector<QKeySequence> vect = getMyVector();//a function that returns a vector of QKeySequence
        func(&MyClass::do_something,vect);
        }

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

        @LeaA
        Dunno, just a guess, but I don't see any connect()? https://stackoverflow.com/a/45387865/489865

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

          @JonB
          I'm using Qt 4.7 and the format you show me is in Qt 5.9

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @LeaA said in connect pointer of member function as QShortCuts slot:

            SLOT((slot_func)())

            where is your slot called slot_func() defined? And it should be SLOT(slotFunc()).
            connect() will give you a return value which will be false in your case and you will get a runtime warning that such a slot does not exist.
            Qt4 doesn't support PMF connects.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

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

              it's defined in MyClass.
              I'm getting a warning that slot_func unused parameter in func.. maybe because of that the SLOT doesn't activated?

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                I don't see any function 'slot_func()' in your class... and the signature for SLOT() is also wrong as I mentioned before. You can't use a pmf in SLOT() signature!

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

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

                  @Christian-Ehrlicher
                  maybe you don't understand my question..
                  I have many SLOTs in me class and I want connect them to a short cut by using the func function.
                  So,every SLOT I want connect I want send it to func and in this function to connect it only for not write the same code in many places..

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

                    OK,
                    I found here the solution:
                    http://www.qtcentre.org/threads/8392-How-to-declare-SLOT-as-a-parameter-to-member-function

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      There is no pmf in this thread either. And just for saving three params to a connect slot... don't know if it's worth ...

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      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