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. Signals and slots in Mainwindow

Signals and slots in Mainwindow

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 6 Posters 5.4k 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.
  • B Offline
    B Offline
    Bharth
    wrote on last edited by
    #1

    Hi,
    how to pass signal to Mainwindow from other class

    MainWindow *mn=new MainWindow();
    connect(ui->pushButtonclick,SIGNAL(clicked()),mn,SLOT(slotButton3()));
    emit clicked();
    delete mn;
    its not going to slotButton3() method

    aha_1980A 1 Reply Last reply
    0
    • B Bharth

      Hi,
      how to pass signal to Mainwindow from other class

      MainWindow *mn=new MainWindow();
      connect(ui->pushButtonclick,SIGNAL(clicked()),mn,SLOT(slotButton3()));
      emit clicked();
      delete mn;
      its not going to slotButton3() method

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi @Bharth,

      MainWindow *mn=new MainWindow();
      connect(ui->pushButtonclick,SIGNAL(clicked()),mn,SLOT(slotButton3()));
      emit clicked();

      Shouldn't that be a signal from the button?

      delete mn;

      You are deleting the window here, therefore the slot is not called.

      You should try the connect code in a Qt Widgets Application (File > New File or Project > Applications > Qt Widgets Application) created by Qt Creator.

      Regards

      Qt has to stay free or it will die.

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

        Is this somewhere in MainWindow? To me this looks like it's from main.cpp

        connect can't know ui, so you need to do something like

        connect(mn->ui->pushButtonclick,SIGNAL(clicked()),mn,SLOT(slotButton3()));
        

        also, which clicked do you emit? I think it sould be

        emit mn->ui->pushbupushButtonclick->clicked();
        

        It's either that or you need to post some more code i think

        1 Reply Last reply
        1
        • T Offline
          T Offline
          TobbY
          wrote on last edited by
          #4

          @Bharth said in Signals and slots in Mainwindow:

          MainWindow *mn=new MainWindow();
          connect(ui->pushButtonclick,SIGNAL(clicked()),mn,SLOT(slotButton3()))

          simply replace emit clicked(); with ui->pushButtonclick->clicked();
          will make your example work // if you want to emit click signal from code only

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Bharth
            wrote on last edited by mrjj
            #5
            form.h
            ///////////////////////////////////////////////
            #include <QWidget>
            
            namespace Ui {
            class Form;
            }
            
            class Form : public QWidget
            {
                Q_OBJECT
            
            public:
                explicit Form(QWidget *parent = nullptr);
                ~Form();
                 static void doSomething();
            public slots:
                 void slotButton1();
                 void slotButton2();
            signals:
                  void clicked();
            
            private:
                Ui::Form *ui;
            };
            form.cpp
            /////////////////////////////////////////////////////
            #include "form.h"
            #include "ui_form.h"
            #include "form2.h"
            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            
            Form::Form(QWidget *parent) :
                QWidget(parent),
                ui(new Ui::Form)
            {
                ui->setupUi(this);
                connect(ui->pushButtonclick, SIGNAL(clicked()), this, SLOT(slotButton1()));
            }
            
            Form::~Form()
            {
                delete ui;
            }
            
            
            
            void Form::slotButton1()
            {
            
            MainWindow *mn=new MainWindow();
            
                connect(ui->pushButtonclick,SIGNAL(clicked()),mn,SLOT(slotButton3()));
                emit ui->pushButtonclick->clicked();
                //delete mn;
            }
            form2.h
            ////////////////////////////////////////////////////
            #ifndef FORM2_H
            #define FORM2_H
            
            #include <QWidget>
            
            namespace Ui {
            class Form2;
            }
            
            class Form2 : public QWidget
            {
                Q_OBJECT
            
            public:
                explicit Form2(QWidget *parent = nullptr);
                ~Form2();
            
            private:
                Ui::Form2 *ui;
            };
            
            
            form2.cpp
            /////////////////////////////////////////////
            #include "form2.h"
            #include "ui_form2.h"
            
            Form2::Form2(QWidget *parent) :
                QWidget(parent),
                ui(new Ui::Form2)
            {
                ui->setupUi(this);
            }
            
            Form2::~Form2()
            {
                delete ui;
            }
            mainwindow.h
            //////////////////////////////////
            #ifndef MAINWINDOW_H
            #define MAINWINDOW_H
            
            #include <QMainWindow>
            
            namespace Ui {
            class MainWindow;
            }
            
            class MainWindow : public QMainWindow
            {
                Q_OBJECT
            
            public:
                explicit MainWindow(QWidget *parent = nullptr);
                ~MainWindow();
            public slots:
                void slotButton();
                void slotButton3();
            public:
                Ui::MainWindow *ui;
            };
            
            #endif // MAINWINDOW_H
            mainwindow.cpp
            //////////////////////////////////
            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            #include "form.h"
            #include "form2.h"
            
            
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
            connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(slotButton()));
            
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            
            void MainWindow::slotButton()
            {
                Form *f1=new Form();
            
                ui->scrollArea->setWidget(f1);
            
            
            }
            
            void MainWindow::slotButton3()
            {
            
                 Form2 *f=new Form2();
                 ui->scrollArea_2->setStyleSheet("background-color:red;");
            
            }
            
            T 1 Reply Last reply
            0
            • T Offline
              T Offline
              TobbY
              wrote on last edited by
              #6

              Hi,
              Can you provide code tag from editor (Last in row). it's hard to read like that.

              mrjjM 1 Reply Last reply
              1
              • T TobbY

                Hi,
                Can you provide code tag from editor (Last in row). it's hard to read like that.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @TobbY
                added code tags.

                1 Reply Last reply
                0
                • B Bharth
                  form.h
                  ///////////////////////////////////////////////
                  #include <QWidget>
                  
                  namespace Ui {
                  class Form;
                  }
                  
                  class Form : public QWidget
                  {
                      Q_OBJECT
                  
                  public:
                      explicit Form(QWidget *parent = nullptr);
                      ~Form();
                       static void doSomething();
                  public slots:
                       void slotButton1();
                       void slotButton2();
                  signals:
                        void clicked();
                  
                  private:
                      Ui::Form *ui;
                  };
                  form.cpp
                  /////////////////////////////////////////////////////
                  #include "form.h"
                  #include "ui_form.h"
                  #include "form2.h"
                  #include "mainwindow.h"
                  #include "ui_mainwindow.h"
                  
                  Form::Form(QWidget *parent) :
                      QWidget(parent),
                      ui(new Ui::Form)
                  {
                      ui->setupUi(this);
                      connect(ui->pushButtonclick, SIGNAL(clicked()), this, SLOT(slotButton1()));
                  }
                  
                  Form::~Form()
                  {
                      delete ui;
                  }
                  
                  
                  
                  void Form::slotButton1()
                  {
                  
                  MainWindow *mn=new MainWindow();
                  
                      connect(ui->pushButtonclick,SIGNAL(clicked()),mn,SLOT(slotButton3()));
                      emit ui->pushButtonclick->clicked();
                      //delete mn;
                  }
                  form2.h
                  ////////////////////////////////////////////////////
                  #ifndef FORM2_H
                  #define FORM2_H
                  
                  #include <QWidget>
                  
                  namespace Ui {
                  class Form2;
                  }
                  
                  class Form2 : public QWidget
                  {
                      Q_OBJECT
                  
                  public:
                      explicit Form2(QWidget *parent = nullptr);
                      ~Form2();
                  
                  private:
                      Ui::Form2 *ui;
                  };
                  
                  
                  form2.cpp
                  /////////////////////////////////////////////
                  #include "form2.h"
                  #include "ui_form2.h"
                  
                  Form2::Form2(QWidget *parent) :
                      QWidget(parent),
                      ui(new Ui::Form2)
                  {
                      ui->setupUi(this);
                  }
                  
                  Form2::~Form2()
                  {
                      delete ui;
                  }
                  mainwindow.h
                  //////////////////////////////////
                  #ifndef MAINWINDOW_H
                  #define MAINWINDOW_H
                  
                  #include <QMainWindow>
                  
                  namespace Ui {
                  class MainWindow;
                  }
                  
                  class MainWindow : public QMainWindow
                  {
                      Q_OBJECT
                  
                  public:
                      explicit MainWindow(QWidget *parent = nullptr);
                      ~MainWindow();
                  public slots:
                      void slotButton();
                      void slotButton3();
                  public:
                      Ui::MainWindow *ui;
                  };
                  
                  #endif // MAINWINDOW_H
                  mainwindow.cpp
                  //////////////////////////////////
                  #include "mainwindow.h"
                  #include "ui_mainwindow.h"
                  #include "form.h"
                  #include "form2.h"
                  
                  
                  MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                  {
                      ui->setupUi(this);
                  connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(slotButton()));
                  
                  }
                  
                  MainWindow::~MainWindow()
                  {
                      delete ui;
                  }
                  
                  void MainWindow::slotButton()
                  {
                      Form *f1=new Form();
                  
                      ui->scrollArea->setWidget(f1);
                  
                  
                  }
                  
                  void MainWindow::slotButton3()
                  {
                  
                       Form2 *f=new Form2();
                       ui->scrollArea_2->setStyleSheet("background-color:red;");
                  
                  }
                  
                  T Offline
                  T Offline
                  TobbY
                  wrote on last edited by
                  #8

                  @Bharth

                  It won't go to slotButton3();

                  in your MainWindow make Form1 and Form2 class variable. instead of creating in function.

                  Class Form
                  declare signal clicked();
                  use:
                  connect(ui->pushButtonclick, SIGNAL(clicked()), this, SIGNAL(clicked()));
                  instead
                  connect(ui->pushButtonclick, SIGNAL(clicked()), this, SLOT(slotButton1()));

                  Class MainWindow:
                  MainWindow .h
                  Form *_form1;
                  Form2 *_form2;

                  MainWindow constructor
                  _form1 = new Form();
                  connect(form, SIGNAL(clicked()), this, SLOT(slotButton3()));

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    Bharth
                    wrote on last edited by
                    #9

                    it is going to slotButton3 in mainwindow i given qDebug<<" " its showing but their its not adding another widget class to mainwindow

                    jsulmJ 1 Reply Last reply
                    0
                    • B Bharth

                      it is going to slotButton3 in mainwindow i given qDebug<<" " its showing but their its not adding another widget class to mainwindow

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

                      @Bharth

                      void MainWindow::slotButton3()
                      {
                      
                           Form2 *f=new Form2();
                           ui->scrollArea_2->setStyleSheet("background-color:red;");
                      
                      }
                      
                      

                      You do not add the widget to any parent widget or layout and you do not call show() on it...

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

                      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