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
QtWS25 Last Chance

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.
  • Nafab213N Offline
    Nafab213N Offline
    Nafab213
    wrote on 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.

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on 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
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on 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

        Kent-DorfmanK 1 Reply Last reply
        4
        • Nafab213N Nafab213

          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.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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
          • Nafab213N Nafab213

            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.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on 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
            • dheerendraD dheerendra

              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.

              Kent-DorfmanK Offline
              Kent-DorfmanK Offline
              Kent-Dorfman
              wrote on 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.HilkJ 1 Reply Last reply
              0
              • Kent-DorfmanK Kent-Dorfman

                @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.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on 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

                • Login

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