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. Connecting Buttons with custom signals and slots
Forum Updated to NodeBB v4.3 + New Features

Connecting Buttons with custom signals and slots

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 1.1k Views 1 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.
  • Thank YouT Offline
    Thank YouT Offline
    Thank You
    wrote on last edited by
    #1

    Re:
    https://forum.qt.io/topic/120733/connecting-buttons-of-one-page-to-label-of-another-page/13

    
    
    #ifndef MAINWIN_H
    #define MAINWIN_H
    
    #include <QMainWindow>
    #include<QMessageBox>
    #include<QPushButton>
    #include<navigation.h>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class mainwin; }
    QT_END_NAMESPACE
    
    class mainwin : public QMainWindow
    {
    Q_OBJECT
    
    public:
    mainwin(QWidget *parent = nullptr);
    ~mainwin();
    navigation *nav_bar;
    private slots:
    void homeButtonClickedSlot();
    
    private slots:
    void on_home_button_clicked();
    void on_about_button_clicked();
    void on_contact_button_clicked();
    void on_settings_button_clicked();
    void on_charts_button_clicked();
    private:
    Ui::mainwin *ui;
    };
    #endif // MAINWIN_H
    
    

    This is mainwin.h

    
    
    #include "mainwin.h"
    #include "ui_mainwin.h"
    
    mainwin::mainwin(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::mainwin)
    {
    ui->setupUi(this);
    
    //connection to buttons
    nav_bar = new navigation;
    connect(nav_bar,SIGNAL(homeButtonClickedSignal()),this,SLOT(homeButtonClickedSlot()));
    
    }
    
    mainwin::~mainwin()
    {
    delete ui;
    }
    
    void mainwin::homeButtonClickedSlot()
    {
    on_home_button_clicked();
    QMessageBox::information(this, "Title", "Hello");
    }
    
    void mainwin::on_home_button_clicked()
    {
    QMessageBox::information(this, "Title" , "Button is clicked");
    QStackedWidget *stackWidget = ui->mainApp;
    stackWidget->setCurrentIndex(0);
    }
    
    

    mainwin.cpp

    
    
    #include "navigation.h"
    #include "ui_navigation.h"
    
    navigation::navigation(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::navigation)
    {
    ui->setupUi(this);
    }
    
    navigation::~navigation()
    {
    delete ui;
    }
    
    void navigation::on_home_clicked()
    {
    emit homeButtonClickedSignal();
    QMessageBox::information(this,"title","clicked" );
    
    }
    
    

    navigation.cpp

    
    
    #ifndef NAVIGATION_H
    #define NAVIGATION_H
    
    #include <QWidget>
    #include<QPushButton>
    #include<QMessageBox>
    
    namespace Ui {
    class navigation;
    }
    
    class navigation : public QWidget
    {
    Q_OBJECT
    
    public:
    explicit navigation(QWidget *parent = nullptr);
    ~navigation();
    
    signals:
    void homeButtonClickedSignal();
    
    private slots:
    void on_home_clicked();
    
    private:
    Ui::navigation *ui;
    };
    
    #endif // NAVIGATION_H
    
    

    navigation.h

    alt text

    Here the navigation pannel is navigation.ui and whole thing is mainwin.ui

    Can anyone say why this is not functioning

    Let's make QT free or It will go forever

    TRUE AND FALSE <3

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • Thank YouT Thank You

      ![alt text](bd134254-5af4-462d-8659-c67e5b131686-image.png image url)

      Yes its working but i dont need this I just want the one in right side not separately

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

      @Thank-You Then don't do

      nav_bar = new navigation;
      

      Use the one from your ui->

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

      Thank YouT 1 Reply Last reply
      3
      • Thank YouT Thank You

        Re:
        https://forum.qt.io/topic/120733/connecting-buttons-of-one-page-to-label-of-another-page/13

        
        
        #ifndef MAINWIN_H
        #define MAINWIN_H
        
        #include <QMainWindow>
        #include<QMessageBox>
        #include<QPushButton>
        #include<navigation.h>
        
        QT_BEGIN_NAMESPACE
        namespace Ui { class mainwin; }
        QT_END_NAMESPACE
        
        class mainwin : public QMainWindow
        {
        Q_OBJECT
        
        public:
        mainwin(QWidget *parent = nullptr);
        ~mainwin();
        navigation *nav_bar;
        private slots:
        void homeButtonClickedSlot();
        
        private slots:
        void on_home_button_clicked();
        void on_about_button_clicked();
        void on_contact_button_clicked();
        void on_settings_button_clicked();
        void on_charts_button_clicked();
        private:
        Ui::mainwin *ui;
        };
        #endif // MAINWIN_H
        
        

        This is mainwin.h

        
        
        #include "mainwin.h"
        #include "ui_mainwin.h"
        
        mainwin::mainwin(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::mainwin)
        {
        ui->setupUi(this);
        
        //connection to buttons
        nav_bar = new navigation;
        connect(nav_bar,SIGNAL(homeButtonClickedSignal()),this,SLOT(homeButtonClickedSlot()));
        
        }
        
        mainwin::~mainwin()
        {
        delete ui;
        }
        
        void mainwin::homeButtonClickedSlot()
        {
        on_home_button_clicked();
        QMessageBox::information(this, "Title", "Hello");
        }
        
        void mainwin::on_home_button_clicked()
        {
        QMessageBox::information(this, "Title" , "Button is clicked");
        QStackedWidget *stackWidget = ui->mainApp;
        stackWidget->setCurrentIndex(0);
        }
        
        

        mainwin.cpp

        
        
        #include "navigation.h"
        #include "ui_navigation.h"
        
        navigation::navigation(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::navigation)
        {
        ui->setupUi(this);
        }
        
        navigation::~navigation()
        {
        delete ui;
        }
        
        void navigation::on_home_clicked()
        {
        emit homeButtonClickedSignal();
        QMessageBox::information(this,"title","clicked" );
        
        }
        
        

        navigation.cpp

        
        
        #ifndef NAVIGATION_H
        #define NAVIGATION_H
        
        #include <QWidget>
        #include<QPushButton>
        #include<QMessageBox>
        
        namespace Ui {
        class navigation;
        }
        
        class navigation : public QWidget
        {
        Q_OBJECT
        
        public:
        explicit navigation(QWidget *parent = nullptr);
        ~navigation();
        
        signals:
        void homeButtonClickedSignal();
        
        private slots:
        void on_home_clicked();
        
        private:
        Ui::navigation *ui;
        };
        
        #endif // NAVIGATION_H
        
        

        navigation.h

        alt text

        Here the navigation pannel is navigation.ui and whole thing is mainwin.ui

        Can anyone say why this is not functioning

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

        @Thank-You said in Connecting Buttons with custom signals and slots:

        why this is not functioning

        What is not functioning?

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

        Thank YouT 1 Reply Last reply
        0
        • Thank YouT Thank You

          Re:
          https://forum.qt.io/topic/120733/connecting-buttons-of-one-page-to-label-of-another-page/13

          
          
          #ifndef MAINWIN_H
          #define MAINWIN_H
          
          #include <QMainWindow>
          #include<QMessageBox>
          #include<QPushButton>
          #include<navigation.h>
          
          QT_BEGIN_NAMESPACE
          namespace Ui { class mainwin; }
          QT_END_NAMESPACE
          
          class mainwin : public QMainWindow
          {
          Q_OBJECT
          
          public:
          mainwin(QWidget *parent = nullptr);
          ~mainwin();
          navigation *nav_bar;
          private slots:
          void homeButtonClickedSlot();
          
          private slots:
          void on_home_button_clicked();
          void on_about_button_clicked();
          void on_contact_button_clicked();
          void on_settings_button_clicked();
          void on_charts_button_clicked();
          private:
          Ui::mainwin *ui;
          };
          #endif // MAINWIN_H
          
          

          This is mainwin.h

          
          
          #include "mainwin.h"
          #include "ui_mainwin.h"
          
          mainwin::mainwin(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::mainwin)
          {
          ui->setupUi(this);
          
          //connection to buttons
          nav_bar = new navigation;
          connect(nav_bar,SIGNAL(homeButtonClickedSignal()),this,SLOT(homeButtonClickedSlot()));
          
          }
          
          mainwin::~mainwin()
          {
          delete ui;
          }
          
          void mainwin::homeButtonClickedSlot()
          {
          on_home_button_clicked();
          QMessageBox::information(this, "Title", "Hello");
          }
          
          void mainwin::on_home_button_clicked()
          {
          QMessageBox::information(this, "Title" , "Button is clicked");
          QStackedWidget *stackWidget = ui->mainApp;
          stackWidget->setCurrentIndex(0);
          }
          
          

          mainwin.cpp

          
          
          #include "navigation.h"
          #include "ui_navigation.h"
          
          navigation::navigation(QWidget *parent) :
          QWidget(parent),
          ui(new Ui::navigation)
          {
          ui->setupUi(this);
          }
          
          navigation::~navigation()
          {
          delete ui;
          }
          
          void navigation::on_home_clicked()
          {
          emit homeButtonClickedSignal();
          QMessageBox::information(this,"title","clicked" );
          
          }
          
          

          navigation.cpp

          
          
          #ifndef NAVIGATION_H
          #define NAVIGATION_H
          
          #include <QWidget>
          #include<QPushButton>
          #include<QMessageBox>
          
          namespace Ui {
          class navigation;
          }
          
          class navigation : public QWidget
          {
          Q_OBJECT
          
          public:
          explicit navigation(QWidget *parent = nullptr);
          ~navigation();
          
          signals:
          void homeButtonClickedSignal();
          
          private slots:
          void on_home_clicked();
          
          private:
          Ui::navigation *ui;
          };
          
          #endif // NAVIGATION_H
          
          

          navigation.h

          alt text

          Here the navigation pannel is navigation.ui and whole thing is mainwin.ui

          Can anyone say why this is not functioning

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

          @Thank-You I'm almost 100% sure, this "nav_bar" is not the one you see on your screen:

          {
          ui->setupUi(this);
          
          //connection to buttons
          nav_bar = new navigation;
          connect(nav_bar,SIGNAL(homeButtonClickedSignal()),this,SLOT(homeButtonClickedSlot()));
          
          }
          

          change it to the following and see if you suddenly have 2 Navbars

          {
          ui->setupUi(this);
          
          //connection to buttons
          nav_bar = new navigation;
          nav_bar->resize(100,500);
          nav_bar->show();
          connect(nav_bar,SIGNAL(homeButtonClickedSignal()),this,SLOT(homeButtonClickedSlot()));
          
          }
          

          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.

          Thank YouT 1 Reply Last reply
          1
          • jsulmJ jsulm

            @Thank-You said in Connecting Buttons with custom signals and slots:

            why this is not functioning

            What is not functioning?

            Thank YouT Offline
            Thank YouT Offline
            Thank You
            wrote on last edited by
            #4

            @jsulm The buttons are not functioning

            Let's make QT free or It will go forever

            TRUE AND FALSE <3

            1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @Thank-You I'm almost 100% sure, this "nav_bar" is not the one you see on your screen:

              {
              ui->setupUi(this);
              
              //connection to buttons
              nav_bar = new navigation;
              connect(nav_bar,SIGNAL(homeButtonClickedSignal()),this,SLOT(homeButtonClickedSlot()));
              
              }
              

              change it to the following and see if you suddenly have 2 Navbars

              {
              ui->setupUi(this);
              
              //connection to buttons
              nav_bar = new navigation;
              nav_bar->resize(100,500);
              nav_bar->show();
              connect(nav_bar,SIGNAL(homeButtonClickedSignal()),this,SLOT(homeButtonClickedSlot()));
              
              }
              
              Thank YouT Offline
              Thank YouT Offline
              Thank You
              wrote on last edited by
              #5

              @J-Hilk You can exactly see my code
              https://filebin.net/3o8y2q5zwxb0l0sa
              Yes it is the same

              Let's make QT free or It will go forever

              TRUE AND FALSE <3

              J.HilkJ 1 Reply Last reply
              0
              • Thank YouT Thank You

                @J-Hilk You can exactly see my code
                https://filebin.net/3o8y2q5zwxb0l0sa
                Yes it is the same

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

                @Thank-You

                I just checked and its not!!!!!

                inside mainwin ui is a navigation item, thats the one you see on screen, the one you create inside the constructor and connect your signals to is a 2nd one and not show!!

                You should really test what I wrote and you will see it for yourself instead of simply refusing it.


                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
                • Thank YouT Offline
                  Thank YouT Offline
                  Thank You
                  wrote on last edited by
                  #7

                  ![alt text](bd134254-5af4-462d-8659-c67e5b131686-image.png image url)

                  Yes its working but i dont need this I just want the one in right side not separately

                  Let's make QT free or It will go forever

                  TRUE AND FALSE <3

                  J.HilkJ jsulmJ 2 Replies Last reply
                  0
                  • Thank YouT Thank You

                    ![alt text](bd134254-5af4-462d-8659-c67e5b131686-image.png image url)

                    Yes its working but i dont need this I just want the one in right side not separately

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

                    @Thank-You
                    from looking at sour source code,

                    this should work:

                    {
                    ui->setupUi(this);
                    
                    //connection to buttons
                    connect(ui->navigation_menu,SIGNAL(homeButtonClickedSignal()),this,SLOT(homeButtonClickedSlot()));
                    
                    }
                    

                    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
                    • Thank YouT Thank You

                      ![alt text](bd134254-5af4-462d-8659-c67e5b131686-image.png image url)

                      Yes its working but i dont need this I just want the one in right side not separately

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

                      @Thank-You Then don't do

                      nav_bar = new navigation;
                      

                      Use the one from your ui->

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

                      Thank YouT 1 Reply Last reply
                      3
                      • jsulmJ jsulm

                        @Thank-You Then don't do

                        nav_bar = new navigation;
                        

                        Use the one from your ui->

                        Thank YouT Offline
                        Thank YouT Offline
                        Thank You
                        wrote on last edited by Thank You
                        #10

                        @jsulm Yes it did work.
                        Silly me just using new instead of assigning it to ui->navigation_menu(it is the object name in ui).

                        Thank You

                        Let's make QT free or It will go forever

                        TRUE AND FALSE <3

                        jsulmJ 1 Reply Last reply
                        0
                        • Thank YouT Thank You

                          @jsulm Yes it did work.
                          Silly me just using new instead of assigning it to ui->navigation_menu(it is the object name in ui).

                          Thank You

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

                          @Thank-You said in Connecting Buttons with custom signals and slots:

                          assigning it to ui->navigation

                          Not sure what you mean. You should not assign anything to ui->navigation - it is set up when you call

                          ui->setupUi(this);
                          

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

                          Thank YouT 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @Thank-You said in Connecting Buttons with custom signals and slots:

                            assigning it to ui->navigation

                            Not sure what you mean. You should not assign anything to ui->navigation - it is set up when you call

                            ui->setupUi(this);
                            
                            Thank YouT Offline
                            Thank YouT Offline
                            Thank You
                            wrote on last edited by
                            #12

                            @jsulm Sorry It was mistake.
                            I have corrected it.

                            Let's make QT free or It will go forever

                            TRUE AND FALSE <3

                            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 Extensions
                            • Unsolved