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. accessing UI component from another class or forms

accessing UI component from another class or forms

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.2k Views
  • 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.
  • QT_QT_QTQ Offline
    QT_QT_QTQ Offline
    QT_QT_QT
    wrote on last edited by
    #1

    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();
    **
        
    
    }
    
    
    
    
    
    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved