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. Second ui slots not working

Second ui slots not working

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 644 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.
  • U Offline
    U Offline
    Ucn_
    wrote on last edited by Ucn_
    #1

    Hello. I'm new to Qt, I have a second ui (second.cpp, second.h, second.ui + mainwindow.cpp, mainwindow.h, main.cpp, mainwindow.ui) which I call in the mainwindow on a buttonclicked, this works(the widget I invoke from second ui appears in a tab that is in the mainwindow), However, slots in the second ui don't work, even the automatic slots created in designer. But the ones in the mainwindow work. How can I make the slots in second ui work after a button in the same ui is clicked, considering that I inkove the second ui in the mainwindow? I tried to create slots in the designer, used qDebug to print something, and manually created slots "connect", it doesn't do anything not even give errors, but all this only work in the mainwindow. Thanks

    The structure of my code:
    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();
    
    private slots:
    
        void on_pushButton_clicked();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    secnui.h

    #ifndef SECNUI_H
    #define SECNUI_H
    
    #include "ui_secndui.h"
    #include <QObject>
    #include <QWidget>
    #include <QtWidgets>
    
    class secnUi : public QWidget
    {
        Q_OBJECT
    public:
        explicit secnUi(QWidget *parent = nullptr);
    
        QVBoxLayout *getTreeto(){
    
            return ui2.verticalLayout_6;
        }
    
    signals:
    
    private slots:
        void on_pushButton5_clicked();
    
    private:
        Ui::secnUi ui2;
    };
    
    #endif // SECNUI_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "secnui.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        secnUi captureWig;
        QVBoxLayout *sendWig = new QVBoxLayout;
        sendWig = captureWig.getTreeto();
    
        QWidget *w = new QWidget();
        w->setLayout(sendWig);
    
        ui->tabWidget->addTab(w, "Newtab");
    }
    
    

    secnui.cpp

    #include "secnui.h"
    #include "QDebug"
    
    secnUi::secnUi(QWidget *parent) : QWidget(parent)
    {
        ui2.setupUi(this);
    }
    
    void secnUi::on_pushButton5_clicked()
    {
        qDebug() << "test";
        ui2.pushButton_02->setText("test");
    }
    
    

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    
    

    mainwindow.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>657</width>
        <height>663</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralWidget">
       <layout class="QVBoxLayout" name="verticalLayout">
        <item>
         <widget class="QTabWidget" name="tabWidget"/>
        </item>
        <item>
         <widget class="QPushButton" name="pushButton">
          <property name="text">
           <string>add</string>
          </property>
         </widget>
        </item>
       </layout>
      </widget>
      <widget class="QMenuBar" name="menuBar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>657</width>
         <height>21</height>
        </rect>
       </property>
      </widget>
      <widget class="QToolBar" name="mainToolBar">
       <attribute name="toolBarArea">
        <enum>TopToolBarArea</enum>
       </attribute>
       <attribute name="toolBarBreak">
        <bool>false</bool>
       </attribute>
      </widget>
      <widget class="QStatusBar" name="statusBar"/>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources/>
     <connections/>
    </ui>
    
    

    secnui.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>secnUi</class>
     <widget class="QWidget" name="secnUi">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>426</width>
        <height>484</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>Form</string>
      </property>
      <widget class="QWidget" name="verticalLayoutWidget_3">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>421</width>
         <height>481</height>
        </rect>
       </property>
       <layout class="QVBoxLayout" name="verticalLayout_6">
        <item>
         <widget class="QGroupBox" name="groupBox_4">
          <property name="title">
           <string>GroupBox</string>
          </property>
          <layout class="QVBoxLayout" name="verticalLayout_7">
           <item>
            <widget class="QGroupBox" name="groupBox_5">
             <property name="title">
              <string>GroupBox</string>
             </property>
             <layout class="QHBoxLayout" name="horizontalLayout_2">
              <item>
               <widget class="QPushButton" name="pushButton5">
                <property name="text">
                 <string>PushButton</string>
                </property>
               </widget>
              </item>
              <item>
               <widget class="QPushButton" name="pushButton_02">
                <property name="text">
                 <string>PushButton</string>
                </property>
               </widget>
              </item>
              <item>
               <widget class="QPushButton" name="pushButton_3">
                <property name="text">
                 <string>PushButton</string>
                </property>
               </widget>
              </item>
             </layout>
            </widget>
           </item>
           <item>
            <widget class="QLabel" name="label">
             <property name="text">
              <string>Recent Added:</string>
             </property>
            </widget>
           </item>
           <item>
            <widget class="QTableWidget" name="tableWidget"/>
           </item>
           <item>
            <widget class="QDialogButtonBox" name="buttonBox_3">
             <property name="standardButtons">
              <set>QDialogButtonBox::Ok</set>
             </property>
            </widget>
           </item>
          </layout>
         </widget>
        </item>
       </layout>
      </widget>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    J.HilkJ 1 Reply Last reply
    0
    • U Ucn_

      @Pablo-J-Rogina I am not sure why the code is not showing up, but it's there. I am posting here again
      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();
      
      private slots:
      
          void on_pushButton_clicked();
      
      private:
          Ui::MainWindow *ui;
      };
      
      #endif // MAINWINDOW_H
      

      secnui.h

      #ifndef SECNUI_H
      #define SECNUI_H
      
      #include "ui_secndui.h"
      #include <QObject>
      #include <QWidget>
      #include <QtWidgets>
      
      class secnUi : public QWidget
      {
          Q_OBJECT
      public:
          explicit secnUi(QWidget *parent = nullptr);
      
          QVBoxLayout *getTreeto(){
      
              return ui2.verticalLayout_6;
          }
      
      signals:
      
      private slots:
          void on_pushButton5_clicked();
      
      private:
          Ui::secnUi ui2;
      };
      
      #endif // SECNUI_H
      

      mainwindow.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include "secnui.h"
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::on_pushButton_clicked()
      {
          secnUi captureWig;
          QVBoxLayout *sendWig = new QVBoxLayout;
          sendWig = captureWig.getTreeto();
      
          QWidget *w = new QWidget();
          w->setLayout(sendWig);
      
          ui->tabWidget->addTab(w, "Newtab");
      }
      

      secnui.cpp

      #include "secnui.h"
      #include "QDebug"
      
      secnUi::secnUi(QWidget *parent) : QWidget(parent)
      {
          ui2.setupUi(this);
      }
      
      void secnUi::on_pushButton5_clicked()
      {
          qDebug() << "test";
          ui2.pushButton_02->setText("test");
      }
      

      main.cpp

      #include "mainwindow.h"
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
      
          return a.exec();
      }
      

      mainwindow.ui

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>MainWindow</class>
       <widget class="QMainWindow" name="MainWindow">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>657</width>
          <height>663</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>MainWindow</string>
        </property>
        <widget class="QWidget" name="centralWidget">
         <layout class="QVBoxLayout" name="verticalLayout">
          <item>
           <widget class="QTabWidget" name="tabWidget"/>
          </item>
          <item>
           <widget class="QPushButton" name="pushButton">
            <property name="text">
             <string>add</string>
            </property>
           </widget>
          </item>
         </layout>
        </widget>
        <widget class="QMenuBar" name="menuBar">
         <property name="geometry">
          <rect>
           <x>0</x>
           <y>0</y>
           <width>657</width>
           <height>21</height>
          </rect>
         </property>
        </widget>
        <widget class="QToolBar" name="mainToolBar">
         <attribute name="toolBarArea">
          <enum>TopToolBarArea</enum>
         </attribute>
         <attribute name="toolBarBreak">
          <bool>false</bool>
         </attribute>
        </widget>
        <widget class="QStatusBar" name="statusBar"/>
       </widget>
       <layoutdefault spacing="6" margin="11"/>
       <resources/>
       <connections/>
      </ui>
      

      secndui.ui

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>secnUi</class>
       <widget class="QWidget" name="secnUi">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>426</width>
          <height>484</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>Form</string>
        </property>
        <widget class="QWidget" name="verticalLayoutWidget_3">
         <property name="geometry">
          <rect>
           <x>0</x>
           <y>0</y>
           <width>421</width>
           <height>481</height>
          </rect>
         </property>
         <layout class="QVBoxLayout" name="verticalLayout_6">
          <item>
           <widget class="QGroupBox" name="groupBox_4">
            <property name="title">
             <string>GroupBox</string>
            </property>
            <layout class="QVBoxLayout" name="verticalLayout_7">
             <item>
              <widget class="QGroupBox" name="groupBox_5">
               <property name="title">
                <string>GroupBox</string>
               </property>
               <layout class="QHBoxLayout" name="horizontalLayout_2">
                <item>
                 <widget class="QPushButton" name="pushButton5">
                  <property name="text">
                   <string>PushButton</string>
                  </property>
                 </widget>
                </item>
                <item>
                 <widget class="QPushButton" name="pushButton_02">
                  <property name="text">
                   <string>PushButton</string>
                  </property>
                 </widget>
                </item>
                <item>
                 <widget class="QPushButton" name="pushButton_3">
                  <property name="text">
                   <string>PushButton</string>
                  </property>
                 </widget>
                </item>
               </layout>
              </widget>
             </item>
             <item>
              <widget class="QLabel" name="label">
               <property name="text">
                <string>Recent Added:</string>
               </property>
              </widget>
             </item>
             <item>
              <widget class="QTableWidget" name="tableWidget"/>
             </item>
             <item>
              <widget class="QDialogButtonBox" name="buttonBox_3">
               <property name="standardButtons">
                <set>QDialogButtonBox::Ok</set>
               </property>
              </widget>
             </item>
            </layout>
           </widget>
          </item>
         </layout>
        </widget>
       </widget>
       <resources/>
       <connections/>
      </ui>
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #8

      @Ucn_ said in Second ui slots not working:

      secnUi captureWig;

      This is a local variable which is destroyed as soon as MainWindow::on_pushButton_clicked() finishes.
      Allocate captureWig on the heap.

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

      1 Reply Last reply
      4
      • U Ucn_

        Hello. I'm new to Qt, I have a second ui (second.cpp, second.h, second.ui + mainwindow.cpp, mainwindow.h, main.cpp, mainwindow.ui) which I call in the mainwindow on a buttonclicked, this works(the widget I invoke from second ui appears in a tab that is in the mainwindow), However, slots in the second ui don't work, even the automatic slots created in designer. But the ones in the mainwindow work. How can I make the slots in second ui work after a button in the same ui is clicked, considering that I inkove the second ui in the mainwindow? I tried to create slots in the designer, used qDebug to print something, and manually created slots "connect", it doesn't do anything not even give errors, but all this only work in the mainwindow. Thanks

        The structure of my code:
        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();
        
        private slots:
        
            void on_pushButton_clicked();
        
        private:
            Ui::MainWindow *ui;
        };
        
        #endif // MAINWINDOW_H
        

        secnui.h

        #ifndef SECNUI_H
        #define SECNUI_H
        
        #include "ui_secndui.h"
        #include <QObject>
        #include <QWidget>
        #include <QtWidgets>
        
        class secnUi : public QWidget
        {
            Q_OBJECT
        public:
            explicit secnUi(QWidget *parent = nullptr);
        
            QVBoxLayout *getTreeto(){
        
                return ui2.verticalLayout_6;
            }
        
        signals:
        
        private slots:
            void on_pushButton5_clicked();
        
        private:
            Ui::secnUi ui2;
        };
        
        #endif // SECNUI_H
        

        mainwindow.cpp

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include "secnui.h"
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        void MainWindow::on_pushButton_clicked()
        {
            secnUi captureWig;
            QVBoxLayout *sendWig = new QVBoxLayout;
            sendWig = captureWig.getTreeto();
        
            QWidget *w = new QWidget();
            w->setLayout(sendWig);
        
            ui->tabWidget->addTab(w, "Newtab");
        }
        
        

        secnui.cpp

        #include "secnui.h"
        #include "QDebug"
        
        secnUi::secnUi(QWidget *parent) : QWidget(parent)
        {
            ui2.setupUi(this);
        }
        
        void secnUi::on_pushButton5_clicked()
        {
            qDebug() << "test";
            ui2.pushButton_02->setText("test");
        }
        
        

        main.cpp

        #include "mainwindow.h"
        #include <QApplication>
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            MainWindow w;
            w.show();
        
            return a.exec();
        }
        
        

        mainwindow.ui

        <?xml version="1.0" encoding="UTF-8"?>
        <ui version="4.0">
         <class>MainWindow</class>
         <widget class="QMainWindow" name="MainWindow">
          <property name="geometry">
           <rect>
            <x>0</x>
            <y>0</y>
            <width>657</width>
            <height>663</height>
           </rect>
          </property>
          <property name="windowTitle">
           <string>MainWindow</string>
          </property>
          <widget class="QWidget" name="centralWidget">
           <layout class="QVBoxLayout" name="verticalLayout">
            <item>
             <widget class="QTabWidget" name="tabWidget"/>
            </item>
            <item>
             <widget class="QPushButton" name="pushButton">
              <property name="text">
               <string>add</string>
              </property>
             </widget>
            </item>
           </layout>
          </widget>
          <widget class="QMenuBar" name="menuBar">
           <property name="geometry">
            <rect>
             <x>0</x>
             <y>0</y>
             <width>657</width>
             <height>21</height>
            </rect>
           </property>
          </widget>
          <widget class="QToolBar" name="mainToolBar">
           <attribute name="toolBarArea">
            <enum>TopToolBarArea</enum>
           </attribute>
           <attribute name="toolBarBreak">
            <bool>false</bool>
           </attribute>
          </widget>
          <widget class="QStatusBar" name="statusBar"/>
         </widget>
         <layoutdefault spacing="6" margin="11"/>
         <resources/>
         <connections/>
        </ui>
        
        

        secnui.ui

        <?xml version="1.0" encoding="UTF-8"?>
        <ui version="4.0">
         <class>secnUi</class>
         <widget class="QWidget" name="secnUi">
          <property name="geometry">
           <rect>
            <x>0</x>
            <y>0</y>
            <width>426</width>
            <height>484</height>
           </rect>
          </property>
          <property name="windowTitle">
           <string>Form</string>
          </property>
          <widget class="QWidget" name="verticalLayoutWidget_3">
           <property name="geometry">
            <rect>
             <x>0</x>
             <y>0</y>
             <width>421</width>
             <height>481</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_6">
            <item>
             <widget class="QGroupBox" name="groupBox_4">
              <property name="title">
               <string>GroupBox</string>
              </property>
              <layout class="QVBoxLayout" name="verticalLayout_7">
               <item>
                <widget class="QGroupBox" name="groupBox_5">
                 <property name="title">
                  <string>GroupBox</string>
                 </property>
                 <layout class="QHBoxLayout" name="horizontalLayout_2">
                  <item>
                   <widget class="QPushButton" name="pushButton5">
                    <property name="text">
                     <string>PushButton</string>
                    </property>
                   </widget>
                  </item>
                  <item>
                   <widget class="QPushButton" name="pushButton_02">
                    <property name="text">
                     <string>PushButton</string>
                    </property>
                   </widget>
                  </item>
                  <item>
                   <widget class="QPushButton" name="pushButton_3">
                    <property name="text">
                     <string>PushButton</string>
                    </property>
                   </widget>
                  </item>
                 </layout>
                </widget>
               </item>
               <item>
                <widget class="QLabel" name="label">
                 <property name="text">
                  <string>Recent Added:</string>
                 </property>
                </widget>
               </item>
               <item>
                <widget class="QTableWidget" name="tableWidget"/>
               </item>
               <item>
                <widget class="QDialogButtonBox" name="buttonBox_3">
                 <property name="standardButtons">
                  <set>QDialogButtonBox::Ok</set>
                 </property>
                </widget>
               </item>
              </layout>
             </widget>
            </item>
           </layout>
          </widget>
         </widget>
         <resources/>
         <connections/>
        </ui>
        
        J.HilkJ Online
        J.HilkJ Online
        J.Hilk
        Moderators
        wrote on last edited by
        #2

        Hi @Ucn_

        I doubt anyone can help you if you do not post your code, here as well.

        Because from what you describe, it could be anything...


        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
        1
        • U Offline
          U Offline
          Ucn_
          wrote on last edited by
          #3

          @J-Hilk thanks. I have updated my code. I hope someone can help

          Pablo J. RoginaP 1 Reply Last reply
          0
          • U Ucn_

            @J-Hilk thanks. I have updated my code. I hope someone can help

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #4

            @Ucn_ said in Second ui slots not working:

            I have updated my code

            and where it is?

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            U 1 Reply Last reply
            0
            • Pablo J. RoginaP Pablo J. Rogina

              @Ucn_ said in Second ui slots not working:

              I have updated my code

              and where it is?

              U Offline
              U Offline
              Ucn_
              wrote on last edited by
              #5

              @Pablo-J-Rogina I posted the code, the private slot (void on_pushButton5_clicked()) does not work or even a manual one, the code I have posted is just structure of my code, I want to achieve the same results, it doesn't give errors, but the issue is the same

              Pablo J. RoginaP 1 Reply Last reply
              0
              • U Ucn_

                @Pablo-J-Rogina I posted the code, the private slot (void on_pushButton5_clicked()) does not work or even a manual one, the code I have posted is just structure of my code, I want to achieve the same results, it doesn't give errors, but the issue is the same

                Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on last edited by
                #6

                @Ucn_ said in Second ui slots not working:

                I posted the code

                Again, where?
                Are you able to see the code under this topic thread?
                I cannot.

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                1
                • U Offline
                  U Offline
                  Ucn_
                  wrote on last edited by
                  #7

                  @Pablo-J-Rogina I am not sure why the code is not showing up, but it's there. I am posting here again
                  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();
                  
                  private slots:
                  
                      void on_pushButton_clicked();
                  
                  private:
                      Ui::MainWindow *ui;
                  };
                  
                  #endif // MAINWINDOW_H
                  

                  secnui.h

                  #ifndef SECNUI_H
                  #define SECNUI_H
                  
                  #include "ui_secndui.h"
                  #include <QObject>
                  #include <QWidget>
                  #include <QtWidgets>
                  
                  class secnUi : public QWidget
                  {
                      Q_OBJECT
                  public:
                      explicit secnUi(QWidget *parent = nullptr);
                  
                      QVBoxLayout *getTreeto(){
                  
                          return ui2.verticalLayout_6;
                      }
                  
                  signals:
                  
                  private slots:
                      void on_pushButton5_clicked();
                  
                  private:
                      Ui::secnUi ui2;
                  };
                  
                  #endif // SECNUI_H
                  

                  mainwindow.cpp

                  #include "mainwindow.h"
                  #include "ui_mainwindow.h"
                  #include "secnui.h"
                  
                  MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                  {
                      ui->setupUi(this);
                  }
                  
                  MainWindow::~MainWindow()
                  {
                      delete ui;
                  }
                  
                  void MainWindow::on_pushButton_clicked()
                  {
                      secnUi captureWig;
                      QVBoxLayout *sendWig = new QVBoxLayout;
                      sendWig = captureWig.getTreeto();
                  
                      QWidget *w = new QWidget();
                      w->setLayout(sendWig);
                  
                      ui->tabWidget->addTab(w, "Newtab");
                  }
                  

                  secnui.cpp

                  #include "secnui.h"
                  #include "QDebug"
                  
                  secnUi::secnUi(QWidget *parent) : QWidget(parent)
                  {
                      ui2.setupUi(this);
                  }
                  
                  void secnUi::on_pushButton5_clicked()
                  {
                      qDebug() << "test";
                      ui2.pushButton_02->setText("test");
                  }
                  

                  main.cpp

                  #include "mainwindow.h"
                  #include <QApplication>
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                      MainWindow w;
                      w.show();
                  
                      return a.exec();
                  }
                  

                  mainwindow.ui

                  <?xml version="1.0" encoding="UTF-8"?>
                  <ui version="4.0">
                   <class>MainWindow</class>
                   <widget class="QMainWindow" name="MainWindow">
                    <property name="geometry">
                     <rect>
                      <x>0</x>
                      <y>0</y>
                      <width>657</width>
                      <height>663</height>
                     </rect>
                    </property>
                    <property name="windowTitle">
                     <string>MainWindow</string>
                    </property>
                    <widget class="QWidget" name="centralWidget">
                     <layout class="QVBoxLayout" name="verticalLayout">
                      <item>
                       <widget class="QTabWidget" name="tabWidget"/>
                      </item>
                      <item>
                       <widget class="QPushButton" name="pushButton">
                        <property name="text">
                         <string>add</string>
                        </property>
                       </widget>
                      </item>
                     </layout>
                    </widget>
                    <widget class="QMenuBar" name="menuBar">
                     <property name="geometry">
                      <rect>
                       <x>0</x>
                       <y>0</y>
                       <width>657</width>
                       <height>21</height>
                      </rect>
                     </property>
                    </widget>
                    <widget class="QToolBar" name="mainToolBar">
                     <attribute name="toolBarArea">
                      <enum>TopToolBarArea</enum>
                     </attribute>
                     <attribute name="toolBarBreak">
                      <bool>false</bool>
                     </attribute>
                    </widget>
                    <widget class="QStatusBar" name="statusBar"/>
                   </widget>
                   <layoutdefault spacing="6" margin="11"/>
                   <resources/>
                   <connections/>
                  </ui>
                  

                  secndui.ui

                  <?xml version="1.0" encoding="UTF-8"?>
                  <ui version="4.0">
                   <class>secnUi</class>
                   <widget class="QWidget" name="secnUi">
                    <property name="geometry">
                     <rect>
                      <x>0</x>
                      <y>0</y>
                      <width>426</width>
                      <height>484</height>
                     </rect>
                    </property>
                    <property name="windowTitle">
                     <string>Form</string>
                    </property>
                    <widget class="QWidget" name="verticalLayoutWidget_3">
                     <property name="geometry">
                      <rect>
                       <x>0</x>
                       <y>0</y>
                       <width>421</width>
                       <height>481</height>
                      </rect>
                     </property>
                     <layout class="QVBoxLayout" name="verticalLayout_6">
                      <item>
                       <widget class="QGroupBox" name="groupBox_4">
                        <property name="title">
                         <string>GroupBox</string>
                        </property>
                        <layout class="QVBoxLayout" name="verticalLayout_7">
                         <item>
                          <widget class="QGroupBox" name="groupBox_5">
                           <property name="title">
                            <string>GroupBox</string>
                           </property>
                           <layout class="QHBoxLayout" name="horizontalLayout_2">
                            <item>
                             <widget class="QPushButton" name="pushButton5">
                              <property name="text">
                               <string>PushButton</string>
                              </property>
                             </widget>
                            </item>
                            <item>
                             <widget class="QPushButton" name="pushButton_02">
                              <property name="text">
                               <string>PushButton</string>
                              </property>
                             </widget>
                            </item>
                            <item>
                             <widget class="QPushButton" name="pushButton_3">
                              <property name="text">
                               <string>PushButton</string>
                              </property>
                             </widget>
                            </item>
                           </layout>
                          </widget>
                         </item>
                         <item>
                          <widget class="QLabel" name="label">
                           <property name="text">
                            <string>Recent Added:</string>
                           </property>
                          </widget>
                         </item>
                         <item>
                          <widget class="QTableWidget" name="tableWidget"/>
                         </item>
                         <item>
                          <widget class="QDialogButtonBox" name="buttonBox_3">
                           <property name="standardButtons">
                            <set>QDialogButtonBox::Ok</set>
                           </property>
                          </widget>
                         </item>
                        </layout>
                       </widget>
                      </item>
                     </layout>
                    </widget>
                   </widget>
                   <resources/>
                   <connections/>
                  </ui>
                  
                  jsulmJ 1 Reply Last reply
                  0
                  • U Ucn_

                    @Pablo-J-Rogina I am not sure why the code is not showing up, but it's there. I am posting here again
                    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();
                    
                    private slots:
                    
                        void on_pushButton_clicked();
                    
                    private:
                        Ui::MainWindow *ui;
                    };
                    
                    #endif // MAINWINDOW_H
                    

                    secnui.h

                    #ifndef SECNUI_H
                    #define SECNUI_H
                    
                    #include "ui_secndui.h"
                    #include <QObject>
                    #include <QWidget>
                    #include <QtWidgets>
                    
                    class secnUi : public QWidget
                    {
                        Q_OBJECT
                    public:
                        explicit secnUi(QWidget *parent = nullptr);
                    
                        QVBoxLayout *getTreeto(){
                    
                            return ui2.verticalLayout_6;
                        }
                    
                    signals:
                    
                    private slots:
                        void on_pushButton5_clicked();
                    
                    private:
                        Ui::secnUi ui2;
                    };
                    
                    #endif // SECNUI_H
                    

                    mainwindow.cpp

                    #include "mainwindow.h"
                    #include "ui_mainwindow.h"
                    #include "secnui.h"
                    
                    MainWindow::MainWindow(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::MainWindow)
                    {
                        ui->setupUi(this);
                    }
                    
                    MainWindow::~MainWindow()
                    {
                        delete ui;
                    }
                    
                    void MainWindow::on_pushButton_clicked()
                    {
                        secnUi captureWig;
                        QVBoxLayout *sendWig = new QVBoxLayout;
                        sendWig = captureWig.getTreeto();
                    
                        QWidget *w = new QWidget();
                        w->setLayout(sendWig);
                    
                        ui->tabWidget->addTab(w, "Newtab");
                    }
                    

                    secnui.cpp

                    #include "secnui.h"
                    #include "QDebug"
                    
                    secnUi::secnUi(QWidget *parent) : QWidget(parent)
                    {
                        ui2.setupUi(this);
                    }
                    
                    void secnUi::on_pushButton5_clicked()
                    {
                        qDebug() << "test";
                        ui2.pushButton_02->setText("test");
                    }
                    

                    main.cpp

                    #include "mainwindow.h"
                    #include <QApplication>
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);
                        MainWindow w;
                        w.show();
                    
                        return a.exec();
                    }
                    

                    mainwindow.ui

                    <?xml version="1.0" encoding="UTF-8"?>
                    <ui version="4.0">
                     <class>MainWindow</class>
                     <widget class="QMainWindow" name="MainWindow">
                      <property name="geometry">
                       <rect>
                        <x>0</x>
                        <y>0</y>
                        <width>657</width>
                        <height>663</height>
                       </rect>
                      </property>
                      <property name="windowTitle">
                       <string>MainWindow</string>
                      </property>
                      <widget class="QWidget" name="centralWidget">
                       <layout class="QVBoxLayout" name="verticalLayout">
                        <item>
                         <widget class="QTabWidget" name="tabWidget"/>
                        </item>
                        <item>
                         <widget class="QPushButton" name="pushButton">
                          <property name="text">
                           <string>add</string>
                          </property>
                         </widget>
                        </item>
                       </layout>
                      </widget>
                      <widget class="QMenuBar" name="menuBar">
                       <property name="geometry">
                        <rect>
                         <x>0</x>
                         <y>0</y>
                         <width>657</width>
                         <height>21</height>
                        </rect>
                       </property>
                      </widget>
                      <widget class="QToolBar" name="mainToolBar">
                       <attribute name="toolBarArea">
                        <enum>TopToolBarArea</enum>
                       </attribute>
                       <attribute name="toolBarBreak">
                        <bool>false</bool>
                       </attribute>
                      </widget>
                      <widget class="QStatusBar" name="statusBar"/>
                     </widget>
                     <layoutdefault spacing="6" margin="11"/>
                     <resources/>
                     <connections/>
                    </ui>
                    

                    secndui.ui

                    <?xml version="1.0" encoding="UTF-8"?>
                    <ui version="4.0">
                     <class>secnUi</class>
                     <widget class="QWidget" name="secnUi">
                      <property name="geometry">
                       <rect>
                        <x>0</x>
                        <y>0</y>
                        <width>426</width>
                        <height>484</height>
                       </rect>
                      </property>
                      <property name="windowTitle">
                       <string>Form</string>
                      </property>
                      <widget class="QWidget" name="verticalLayoutWidget_3">
                       <property name="geometry">
                        <rect>
                         <x>0</x>
                         <y>0</y>
                         <width>421</width>
                         <height>481</height>
                        </rect>
                       </property>
                       <layout class="QVBoxLayout" name="verticalLayout_6">
                        <item>
                         <widget class="QGroupBox" name="groupBox_4">
                          <property name="title">
                           <string>GroupBox</string>
                          </property>
                          <layout class="QVBoxLayout" name="verticalLayout_7">
                           <item>
                            <widget class="QGroupBox" name="groupBox_5">
                             <property name="title">
                              <string>GroupBox</string>
                             </property>
                             <layout class="QHBoxLayout" name="horizontalLayout_2">
                              <item>
                               <widget class="QPushButton" name="pushButton5">
                                <property name="text">
                                 <string>PushButton</string>
                                </property>
                               </widget>
                              </item>
                              <item>
                               <widget class="QPushButton" name="pushButton_02">
                                <property name="text">
                                 <string>PushButton</string>
                                </property>
                               </widget>
                              </item>
                              <item>
                               <widget class="QPushButton" name="pushButton_3">
                                <property name="text">
                                 <string>PushButton</string>
                                </property>
                               </widget>
                              </item>
                             </layout>
                            </widget>
                           </item>
                           <item>
                            <widget class="QLabel" name="label">
                             <property name="text">
                              <string>Recent Added:</string>
                             </property>
                            </widget>
                           </item>
                           <item>
                            <widget class="QTableWidget" name="tableWidget"/>
                           </item>
                           <item>
                            <widget class="QDialogButtonBox" name="buttonBox_3">
                             <property name="standardButtons">
                              <set>QDialogButtonBox::Ok</set>
                             </property>
                            </widget>
                           </item>
                          </layout>
                         </widget>
                        </item>
                       </layout>
                      </widget>
                     </widget>
                     <resources/>
                     <connections/>
                    </ui>
                    
                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @Ucn_ said in Second ui slots not working:

                    secnUi captureWig;

                    This is a local variable which is destroyed as soon as MainWindow::on_pushButton_clicked() finishes.
                    Allocate captureWig on the heap.

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

                    1 Reply Last reply
                    4
                    • U Offline
                      U Offline
                      Ucn_
                      wrote on last edited by
                      #9

                      @jsulm thanks, secnUi *captureWig; captureWig = new secnUi(); fixed it. By the way do you know how can I connect a slot to this button that is in second ui, considering it is inside a loop and invoked in the mainwindow?

                      for (char letter = 'A'; letter <= 'M'; ++letter) {
                      
                              std::string s;
                              s += letter;
                              QString qstr = QString::fromStdString(s);
                              QPushButton *button_first = new QPushButton(qstr);
                              ui2.horizontalLayout->addWidget(button_first);
                          }
                      

                      a slot like this:

                      connect(button_first, &QPushButton::triggered, this, &secnUi::openInNewWindow);
                      
                      jsulmJ aha_1980A 2 Replies Last reply
                      0
                      • U Ucn_

                        @jsulm thanks, secnUi *captureWig; captureWig = new secnUi(); fixed it. By the way do you know how can I connect a slot to this button that is in second ui, considering it is inside a loop and invoked in the mainwindow?

                        for (char letter = 'A'; letter <= 'M'; ++letter) {
                        
                                std::string s;
                                s += letter;
                                QString qstr = QString::fromStdString(s);
                                QPushButton *button_first = new QPushButton(qstr);
                                ui2.horizontalLayout->addWidget(button_first);
                            }
                        

                        a slot like this:

                        connect(button_first, &QPushButton::triggered, this, &secnUi::openInNewWindow);
                        
                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by jsulm
                        #10

                        @Ucn_ said in Second ui slots not working:

                        connect(button_first, &QPushButton::triggered, this, &secnUi::openInNewWindow);

                        Sorry, you have to explain better: where is all this code located? Is "this" secnUi instance?
                        By the way this is not a slot but a call to connect().

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

                        U 1 Reply Last reply
                        1
                        • jsulmJ jsulm

                          @Ucn_ said in Second ui slots not working:

                          connect(button_first, &QPushButton::triggered, this, &secnUi::openInNewWindow);

                          Sorry, you have to explain better: where is all this code located? Is "this" secnUi instance?
                          By the way this is not a slot but a call to connect().

                          U Offline
                          U Offline
                          Ucn_
                          wrote on last edited by
                          #11

                          @jsulm Actually I figured out, I only needed your solution "Allocate captureWig on the heap". Once again thanks.

                          1 Reply Last reply
                          0
                          • U Ucn_

                            @jsulm thanks, secnUi *captureWig; captureWig = new secnUi(); fixed it. By the way do you know how can I connect a slot to this button that is in second ui, considering it is inside a loop and invoked in the mainwindow?

                            for (char letter = 'A'; letter <= 'M'; ++letter) {
                            
                                    std::string s;
                                    s += letter;
                                    QString qstr = QString::fromStdString(s);
                                    QPushButton *button_first = new QPushButton(qstr);
                                    ui2.horizontalLayout->addWidget(button_first);
                                }
                            

                            a slot like this:

                            connect(button_first, &QPushButton::triggered, this, &secnUi::openInNewWindow);
                            
                            aha_1980A Offline
                            aha_1980A Offline
                            aha_1980
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            Hi @Ucn_,

                            Just for your information:

                            std::string s;
                            s += letter;
                            QString qstr = QString::fromStdString(s);

                            can be done better with this constructor: QString qstr = QString(QChar(letter));

                            Regards

                            Qt has to stay free or it will die.

                            U 1 Reply Last reply
                            4
                            • aha_1980A aha_1980

                              Hi @Ucn_,

                              Just for your information:

                              std::string s;
                              s += letter;
                              QString qstr = QString::fromStdString(s);

                              can be done better with this constructor: QString qstr = QString(QChar(letter));

                              Regards

                              U Offline
                              U Offline
                              Ucn_
                              wrote on last edited by
                              #13

                              @aha_1980 thanks, I just didn't know how to convert that.

                              1 Reply Last reply
                              1

                              • Login

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