accessing UI component from another class or forms
-
Greetings,
I have two UI forms over here. The first UI forms has a line edit box, this is to prompt user to type some string in line edit box in the first UI forms. I wish to take the string to pass in to my second UI forms. How do I access UI component from another class or another form? Here my codes:
FYI, the line edit box is in the first UI form(mainwindow.cpp), and i wish to access the line edit box in "mainwindowA.cpp"****mainwindow.cpp**** #include "mainwindow.h" #include "ui_mainwindow.h" #include "mainwindowA.h" #include <Qstring> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_NextToButton_clicked() { **QString File = ui->File_lineEdit->text();** A = new MainWindowA (this); A -> show(); }
****mainwindowA.cpp**** #include "mainwindow.h" #include "ui_mainwindow.h" #include <QByteArray> #include <QProcess> #include <Windows.h> #include <iostream> #include <string> #include <cstdlib> #include <stdlib.h> #include <QDebug> #include <QString> #include <QMessageBox> #include <QWidget> #include "mainwindowA.h" #include "ui_mainwindowa.h" using namespace std; MainWindowA::MainWindowA(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindowA) { ui->setupUi(this); MainWindow *mainWindow = new MainWindow(); } MainWindowA::~MainWindowA() { delete ui; } void MainWindowA::on_Git_Button_clicked() { ** QString *File = new QString(); ** }
-
This question was asked many times already.
Short answer: don't do this!
If you do it then your classes will be tightly coupled, this is bad design. If you change the UI in one class then you have to change the other class. Different classes should not know anything about UIs in other classes.
Instead you emit a signal in one class and connect a slot to it in other class. This other class can then do what ever it needs to do and the first class do not even need to know there is another class.