Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved access to another ui widget from mainwindow

    General and Desktop
    2
    6
    3054
    Loading More Posts
    • 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
      rapid84 last edited by

      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 Reply Quote 0
      • ?
        A Former User last edited by

        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 Reply Quote 0
        • ?
          A Former User last edited by

          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 Reply Quote 0
          • R
            rapid84 @Guest last edited by

            @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 Reply Quote 0
            • ?
              A Former User last edited by 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 1 Reply Last reply Reply Quote 0
              • R
                rapid84 @Guest last edited by

                @Wieland

                Thanks, it works now.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post