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. connect action to slot new syntax
Qt 6.11 is out! See what's new in the release blog

connect action to slot new syntax

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 1.3k Views 2 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.
  • H Offline
    H Offline
    hobbyProgrammer
    wrote on last edited by hobbyProgrammer
    #1

    Hi,

    I am trying to connect an action that's created in the designer to a slot.

    The action is actionLogout and can be called via ui->actionLog_out where ui is from MainWindow.
    The slot is logout().

    I already tried this:

        logoutAct = new QAction(tr("logout"));
        connect(this, logoutAct, this, &MainWindow::logout);
    

    it says: "no matching member function for call to connect"

    JonBJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      You seem slightly confused about the syntax
      It is
      Who has the signal, the signal , Who has the Slot, the Slot

      logoutAct = new QAction(tr("logout"));
      connect(logoutAct, &QAction::Triggered, this, &MainWindow::logout);

      so we have
      logoutAct (who) , &QAction::Triggered (the signal ) , this (who), &MainWindow::logout (slot)

      H 1 Reply Last reply
      6
      • H hobbyProgrammer

        Hi,

        I am trying to connect an action that's created in the designer to a slot.

        The action is actionLogout and can be called via ui->actionLog_out where ui is from MainWindow.
        The slot is logout().

        I already tried this:

            logoutAct = new QAction(tr("logout"));
            connect(this, logoutAct, this, &MainWindow::logout);
        

        it says: "no matching member function for call to connect"

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

        @hobbyProgrammer
        One thing to note: as per @mrjj's reply, you should (actually must) use either the old SIGNAL/SLOT() syntax (deprecated) or the new non-macro ones (recommended) like @mrjj's. Don't even try mixing them as you have! Stop using either SIGNAL() or SLOT() from now onward!

        1 Reply Last reply
        2
        • mrjjM mrjj

          Hi
          You seem slightly confused about the syntax
          It is
          Who has the signal, the signal , Who has the Slot, the Slot

          logoutAct = new QAction(tr("logout"));
          connect(logoutAct, &QAction::Triggered, this, &MainWindow::logout);

          so we have
          logoutAct (who) , &QAction::Triggered (the signal ) , this (who), &MainWindow::logout (slot)

          H Offline
          H Offline
          hobbyProgrammer
          wrote on last edited by
          #4

          @mrjj thank you, at least it doesn't come with errors now, but it doesn't respond to me clicking 'logout' in the menubar.

          I tried:

              connect(ui->actionLog_out, &QAction::triggered, this, &MainWindow::logout);
          

          and

              QAction *logoutAct = ui->actionLog_out;
              connect(logoutAct, &QAction::triggered, this, &MainWindow::logout);
          
          JonBJ 1 Reply Last reply
          0
          • H hobbyProgrammer

            @mrjj thank you, at least it doesn't come with errors now, but it doesn't respond to me clicking 'logout' in the menubar.

            I tried:

                connect(ui->actionLog_out, &QAction::triggered, this, &MainWindow::logout);
            

            and

                QAction *logoutAct = ui->actionLog_out;
                connect(logoutAct, &QAction::triggered, this, &MainWindow::logout);
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @hobbyProgrammer
            If you have just @mrjj's code it won't. That creates an "anonymous"/"standalone" QAction. You have to connect the QAction to the appropriate item on the QMenu, else they don't have anything to do with each other. I don't know if or how the Designer does that. And make sure you put a breakpoint or output something in whatever your MainWindow::logout is, so you are sure whether it is called or not.

            1 Reply Last reply
            1
            • J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              I updated my example..
              https://github.com/DeiVadder/LoginExample


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              JonBJ 1 Reply Last reply
              4
              • J.HilkJ J.Hilk

                I updated my example..
                https://github.com/DeiVadder/LoginExample

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

                @J-Hilk Looks like you've got a full-time job there :)

                J.HilkJ 1 Reply Last reply
                0
                • JonBJ JonB

                  @J-Hilk Looks like you've got a full-time job there :)

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @JonB If it's only taking a couple of seconds/minutes 🤷‍♂️
                  ;)


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  2

                  • Login

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