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. Connection ui button to Slot in file.cpp
Forum Updated to NodeBB v4.3 + New Features

Connection ui button to Slot in file.cpp

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 2.2k 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.
  • N Offline
    N Offline
    Nafab213
    wrote on 18 Feb 2019, 22:48 last edited by
    #1

    I created two buttons in the central widget of my window from Qtdesigner. But I can not find the buttons to establish a connection to a slot in the file.cpp of my class. I have a slot "affichefenetrecene" and "affichefenetrecenep" that I want to connect to my buttons.
    That's myfile.cpp

    #include "fenprincipale.h"
    #include "ui_fenprincipale.h"
    #include "fencene.h"
    #include "fencenep.h"
    
    FenPrincipale::FenPrincipale(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::FenPrincipale)
    {
        ui->setupUi(this);
        fencen = new QWidget;
        setCentralWidget(fencen);
    
    
        QStackedWidget *centralfencen = new QStackedWidget;
        stackedWidget->addWidget(fencen);
        stackedWidget->addWidget(FenCene());
        stackedWidget->addWidget(FenCenep());
    
        connect(ui->bttoncene, SIGNAL(clicked()), this, SLOT(Affichefencene()));
        connect(ui->bttoncenep, SIGNAL(clicked()), this, SLOT(Affichefencenep()));
    }
    
    FenPrincipale::~FenPrincipale()
    {
        delete ui;
    }
    
    void FenPrincipale::Affichefencene()
    {
        centralfencen->setCentralWidget(FenCene());
    }
    
    void FenPrincipale::Affichefencenep()
    {
       centralfencen->setCentralWidget(FenCenep());
    }
    
    

    I will have to create them manually?
    How can I use the buttons in my file.cpp.

    J J 2 Replies Last reply 19 Feb 2019, 05:18
    0
    • K Offline
      K Offline
      Kent-Dorfman
      wrote on 19 Feb 2019, 02:17 last edited by
      #2

      Does the class header file include the Q_OBJECT macro? It is required so that the metadata for signals/slots are included. At least that used to be a requirement. I've been in pyqt land for too long...

      1 Reply Last reply
      3
      • D Offline
        D Offline
        dheerendra
        Qt Champions 2022
        wrote on 19 Feb 2019, 03:42 last edited by
        #3

        Did you declare them under the public slots ? Can you show your header files ? You cannot find means what ? Are you not able to compile the program ? Slot is not called when you click on the button ? Please explain.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        K 1 Reply Last reply 19 Feb 2019, 16:49
        4
        • N Nafab213
          18 Feb 2019, 22:48

          I created two buttons in the central widget of my window from Qtdesigner. But I can not find the buttons to establish a connection to a slot in the file.cpp of my class. I have a slot "affichefenetrecene" and "affichefenetrecenep" that I want to connect to my buttons.
          That's myfile.cpp

          #include "fenprincipale.h"
          #include "ui_fenprincipale.h"
          #include "fencene.h"
          #include "fencenep.h"
          
          FenPrincipale::FenPrincipale(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::FenPrincipale)
          {
              ui->setupUi(this);
              fencen = new QWidget;
              setCentralWidget(fencen);
          
          
              QStackedWidget *centralfencen = new QStackedWidget;
              stackedWidget->addWidget(fencen);
              stackedWidget->addWidget(FenCene());
              stackedWidget->addWidget(FenCenep());
          
              connect(ui->bttoncene, SIGNAL(clicked()), this, SLOT(Affichefencene()));
              connect(ui->bttoncenep, SIGNAL(clicked()), this, SLOT(Affichefencenep()));
          }
          
          FenPrincipale::~FenPrincipale()
          {
              delete ui;
          }
          
          void FenPrincipale::Affichefencene()
          {
              centralfencen->setCentralWidget(FenCene());
          }
          
          void FenPrincipale::Affichefencenep()
          {
             centralfencen->setCentralWidget(FenCenep());
          }
          
          

          I will have to create them manually?
          How can I use the buttons in my file.cpp.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 19 Feb 2019, 05:18 last edited by
          #4

          @Nafab213 said in Connection ui button to Slot in file.cpp:

          I will have to create them manually?

          You mean the slots? Yes, you have to implement them.
          What are their names actually? Because in the connect calls they have upper-case first later, but you say you have "affichefenetrecene" and "affichefenetrecenep". C++ is case sensitive.

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

          1 Reply Last reply
          3
          • N Nafab213
            18 Feb 2019, 22:48

            I created two buttons in the central widget of my window from Qtdesigner. But I can not find the buttons to establish a connection to a slot in the file.cpp of my class. I have a slot "affichefenetrecene" and "affichefenetrecenep" that I want to connect to my buttons.
            That's myfile.cpp

            #include "fenprincipale.h"
            #include "ui_fenprincipale.h"
            #include "fencene.h"
            #include "fencenep.h"
            
            FenPrincipale::FenPrincipale(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::FenPrincipale)
            {
                ui->setupUi(this);
                fencen = new QWidget;
                setCentralWidget(fencen);
            
            
                QStackedWidget *centralfencen = new QStackedWidget;
                stackedWidget->addWidget(fencen);
                stackedWidget->addWidget(FenCene());
                stackedWidget->addWidget(FenCenep());
            
                connect(ui->bttoncene, SIGNAL(clicked()), this, SLOT(Affichefencene()));
                connect(ui->bttoncenep, SIGNAL(clicked()), this, SLOT(Affichefencenep()));
            }
            
            FenPrincipale::~FenPrincipale()
            {
                delete ui;
            }
            
            void FenPrincipale::Affichefencene()
            {
                centralfencen->setCentralWidget(FenCene());
            }
            
            void FenPrincipale::Affichefencenep()
            {
               centralfencen->setCentralWidget(FenCenep());
            }
            
            

            I will have to create them manually?
            How can I use the buttons in my file.cpp.

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 19 Feb 2019, 05:46 last edited by
            #5

            @Nafab213
            how does your header file looks like, did you actually place Affichefencene and Affichefencenep under the slots macro ? You're using the old style qt4 connect so that is a requirement.

            I suggest the following, as it will either work or give you compile time errors:

            connect(ui->bttoncene, &QPushButton::clicked, this, &FenPrincipale::Affichefencene);
                connect(ui->bttoncenep,  &QPushButton::clicked, this, &FenPrincipale::Affichefencenep);
            

            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
            • D dheerendra
              19 Feb 2019, 03:42

              Did you declare them under the public slots ? Can you show your header files ? You cannot find means what ? Are you not able to compile the program ? Slot is not called when you click on the button ? Please explain.

              K Offline
              K Offline
              Kent-Dorfman
              wrote on 19 Feb 2019, 16:49 last edited by
              #6

              @dheerendra said in Connection ui button to Slot in file.cpp:

              Did you declare them under the public slots ? Can you show your header files ? You cannot find means what ? Are you not able to compile the program ? Slot is not called when you click on the button ? Please explain.

              is public slots: actually required in the declarations or is it just a nice visual queue? My foggy memory seems to indicate that it was not strictly required, as long as the methods were accessible where they are connected.

              J 1 Reply Last reply 19 Feb 2019, 17:13
              0
              • K Kent-Dorfman
                19 Feb 2019, 16:49

                @dheerendra said in Connection ui button to Slot in file.cpp:

                Did you declare them under the public slots ? Can you show your header files ? You cannot find means what ? Are you not able to compile the program ? Slot is not called when you click on the button ? Please explain.

                is public slots: actually required in the declarations or is it just a nice visual queue? My foggy memory seems to indicate that it was not strictly required, as long as the methods were accessible where they are connected.

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 19 Feb 2019, 17:13 last edited by
                #7

                @Kent-Dorfman
                requiered for Qt4 macros, visual queue for Qt5 syntax


                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
                3

                4/7

                19 Feb 2019, 05:18

                • Login

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