access to another ui widget from mainwindow



  • 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 ?



  • 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.



  • 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.



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

    error: 'currentText' was not declared in this scope
    


  • 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();
    }
    


  • @Wieland

    Thanks, it works now.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.