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. access to another ui widget from mainwindow

access to another ui widget from mainwindow

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 4.5k 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.
  • R Offline
    R Offline
    rapid84
    wrote on last edited by
    #1

    Hi,
    I have a ui project that has mainwindow and another ui file. The another ui file has a combobox and i want to access it from mainwindow and i want to use its selected item in mainwindow. How can i do ?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! The ui object should be private to its owning form. To make form data visible to other objects (e.g. your mainwindow) you can add...

      • ordinary getter and setter functions
      • Qt signals and slots
      • Qt properties

      ... to your second form.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        For example this could be your second form:

        In form.h:

        class Form : public QWidget
        {
            Q_OBJECT
        
        public:
            explicit Form(QWidget *parent = 0);
            ~Form();
        
            QString currentText() const;
        
        private:
            Ui::Form *ui;
        };
        

        In form.cpp:

        QString Form::currentText() const
        {
            return ui->comboBox->currentText();
        }
        

        You can now access QString Form::currentText() const from your main window to get the combobox data.

        R 1 Reply Last reply
        0
        • ? A Former User

          For example this could be your second form:

          In form.h:

          class Form : public QWidget
          {
              Q_OBJECT
          
          public:
              explicit Form(QWidget *parent = 0);
              ~Form();
          
              QString currentText() const;
          
          private:
              Ui::Form *ui;
          };
          

          In form.cpp:

          QString Form::currentText() const
          {
              return ui->comboBox->currentText();
          }
          

          You can now access QString Form::currentText() const from your main window to get the combobox data.

          R Offline
          R Offline
          rapid84
          wrote on last edited by
          #4

          @Wieland
          Thanks for reply but when i try to use it gives error:

          error: 'currentText' was not declared in this scope
          
          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by A Former User
            #5

            Here is a complete example for a mainwindow and a second form. The form has a combobox. The mainwindow has a pushbutton. Everytime the pushbutton is clicked the pushbutton's text is set to the current text of the combobox.

            mainwindow.h

            #ifndef MAINWINDOW_H
            #define MAINWINDOW_H
            
            #include <QMainWindow>
            
            class Form;
            
            namespace Ui {
            class MainWindow;
            }
            
            class MainWindow : public QMainWindow
            {
                Q_OBJECT
            
            public:
                explicit MainWindow(QWidget *parent = 0);
                ~MainWindow();
            
            private slots:
                void on_pushButton_clicked();
            
            private:
                Ui::MainWindow *ui;
                Form *m_form;
            };
            
            #endif // MAINWINDOW_H
            

            mainwindow.cpp

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            
            #include "form.h"
            
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
                m_form = new Form;
                m_form->show();
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            
            void MainWindow::on_pushButton_clicked()
            {
                ui->pushButton->setText( m_form->currentText() );
            }
            

            form.h

            #ifndef FORM_H
            #define FORM_H
            
            #include <QWidget>
            #include <QString>
            
            namespace Ui {
            class Form;
            }
            
            class Form : public QWidget
            {
                Q_OBJECT
            
            public:
                explicit Form(QWidget *parent = 0);
                ~Form();
            
                QString currentText() const;
            
            private:
                Ui::Form *ui;
            };
            
            #endif // FORM_H
            

            form.cpp

            #include "form.h"
            #include "ui_form.h"
            
            Form::Form(QWidget *parent) :
                QWidget(parent),
                ui(new Ui::Form)
            {
                ui->setupUi(this);
            }
            
            Form::~Form()
            {
                delete ui;
            }
            
            
            QString Form::currentText() const
            {
                return ui->comboBox->currentText();
            }
            
            R 1 Reply Last reply
            0
            • ? A Former User

              Here is a complete example for a mainwindow and a second form. The form has a combobox. The mainwindow has a pushbutton. Everytime the pushbutton is clicked the pushbutton's text is set to the current text of the combobox.

              mainwindow.h

              #ifndef MAINWINDOW_H
              #define MAINWINDOW_H
              
              #include <QMainWindow>
              
              class Form;
              
              namespace Ui {
              class MainWindow;
              }
              
              class MainWindow : public QMainWindow
              {
                  Q_OBJECT
              
              public:
                  explicit MainWindow(QWidget *parent = 0);
                  ~MainWindow();
              
              private slots:
                  void on_pushButton_clicked();
              
              private:
                  Ui::MainWindow *ui;
                  Form *m_form;
              };
              
              #endif // MAINWINDOW_H
              

              mainwindow.cpp

              #include "mainwindow.h"
              #include "ui_mainwindow.h"
              
              #include "form.h"
              
              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
                  m_form = new Form;
                  m_form->show();
              }
              
              MainWindow::~MainWindow()
              {
                  delete ui;
              }
              
              void MainWindow::on_pushButton_clicked()
              {
                  ui->pushButton->setText( m_form->currentText() );
              }
              

              form.h

              #ifndef FORM_H
              #define FORM_H
              
              #include <QWidget>
              #include <QString>
              
              namespace Ui {
              class Form;
              }
              
              class Form : public QWidget
              {
                  Q_OBJECT
              
              public:
                  explicit Form(QWidget *parent = 0);
                  ~Form();
              
                  QString currentText() const;
              
              private:
                  Ui::Form *ui;
              };
              
              #endif // FORM_H
              

              form.cpp

              #include "form.h"
              #include "ui_form.h"
              
              Form::Form(QWidget *parent) :
                  QWidget(parent),
                  ui(new Ui::Form)
              {
                  ui->setupUi(this);
              }
              
              Form::~Form()
              {
                  delete ui;
              }
              
              
              QString Form::currentText() const
              {
                  return ui->comboBox->currentText();
              }
              
              R Offline
              R Offline
              rapid84
              wrote on last edited by
              #6

              @Wieland

              Thanks, it works now.

              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