How to access QT element from different file



  • Hello everybody, im pretty new to -QT- Qt (installed it only today), and i have this very simple application:

    i have created -QT- Qt widget project, and i have these files
    main.cpp
    mainwindow.cpp
    mainwindow.h
    ui_mainwindow.h
    mainwindow.ui

    so, the main window is created and i have added a new element via designer in mainwindow.ui - a simple text area named textEdit

    so my question is, how i can write something into this textEdit from main.cpp

    when i try

    @textEdit->setText("test");@

    it says that textEdit is undeclared, and i know that it is declared in ui_mainwindow.h i just dont know how to tell it to the main.cpp file
    ive been sitting on this for few hours, and could not figure anything, i found out, that ui_mainwindow.h is using namespace
    @namespace Ui {
    class MainWindow: public Ui_MainWindow {};
    } // namespace Ui@

    so i tried something like
    @Ui::MainWindow->textEdit->setText("test");@
    but that does not work either...

    //i figured out that i can access textEdit withing mainwindow.cpp using ui->textEdit->setText(); and i would like to do the same, but from the main.cpp

    So could someone help me, how to make this simple thing works ? :)

    Best Regards,
    andyc



  • You normally create a new class that is a subclass of QMainWindow and your Ui created class like so in your mainwindow.cpp.
    @class MainWindow : public QMainWindow, public Ui::MainWindow@

    Then you could use textEdit.setText("test") in the constructor of your MainWindow.

    You could also create a pointer to the Ui::MainWindow in the private section of your MainWindow implementation like so.
    @private:
    Ui::MainWindow ui;@

    Then in your MainWindow constructor your would have to do ui.setupUi(this); instead of just setupUi(this).
    Then change text edit like this. ui.textEdit->setText("test");

    Hope this helps.



  • But i have everything like you said , i am using Qt Creator, and it generated code for me - i did not edit these files
    and it looks like this:

    mainwindow.cpp and it looks like
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    @

    and 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 = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H@

    and ui_mainwindow.h
    @
    QT_BEGIN_NAMESPACE

    class Ui_MainWindow
    {
    public:
    QWidget *centralwidget;
    QTextEdit *textEdit;
    QMenuBar *menubar;
    QStatusBar *statusbar;

    void setupUi(QMainWindow *MainWindow)
    {
        if (MainWindow->objectName().isEmpty())
            MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
        MainWindow->resize(800, 600);
        centralwidget = new QWidget(MainWindow);
        centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
        textEdit = new QTextEdit(centralwidget);
        textEdit->setObjectName(QString::fromUtf8("textEdit"));
    

    .
    .
    .
    retranslateUi(MainWindow);

        QMetaObject::connectSlotsByName(MainWindow);
    } // setupUi
    
    void retranslateUi(QMainWindow *MainWindow)
    {
        MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
    } // retranslateUi
    

    };

    namespace Ui {
    class MainWindow: public Ui_MainWindow {};
    } // namespace Ui

    QT_END_NAMESPACE

    #endif // UI_MAINWINDOW_H
    @

    and finally, qt generated main.cpp and
    main.cpp

    @
    #include <QtGui/QApplication>
    #include "mainwindow.h"
    int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    MainWindow w;
    w.show();
    

    textEdit->setText("test"); //i would like to write to the textEdit in the main.cpp not in the
    // mainwindow.cpp and i get error: 'textEdit' was not declared in this scope
    return app.exec();
    }
    @

    again, i know how to user textEdit in mainwindow.cpp, but i dont know how to use it in main.cpp, thats my problem..

    Thansk.



  • No, you don't. You have

    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    private:
    Ui::MainWindow *ui;
    };
    @

    which means, you have a pointer to the ui. This means, you must use text edit like this:

    @
    ui->textEdit->setText("test");
    @


Log in to reply
 

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