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. How to access QT element from different file
Forum Updated to NodeBB v4.3 + New Features

How to access QT element from different file

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 5.2k 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.
  • A Offline
    A Offline
    andyc
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      ShadowMoses
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andyc
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          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");
          @

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          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