Please nominate your Qt Champions for 2021!

How to get the ui filed from one cpp file to another cpp file?

  • How to get the ui filed values ,from one cpp file to another cpp file?

  • Lifetime Qt Champion

    Normally the first UI would store the data to some data structure and
    the second UI would just read from there. Like database system.

    You have to give more details on how you mean / what you are trying.
    as input of a say a name
    could be made as one operation. ( pop dialog, and return name to mainwind for use)
    but other types of input need to be saved and access later.

    If you have a live floating dialog, (like a tool window for imaging program), one can easy use signal and slots to
    send data.

  • i design one editfield (take for example username) in sqlDesigner.ui and i can able to get that field value in sqlDesigner.cpp by using (ui->username).
    but i want that field value in mainWindow.cpp file?
    so how could i get that value ?

  • Lifetime Qt Champion

    Well you can just add a new public function.

    QString sqlDesigner::GetUserName() {
    return ui->username->text();

    and then just call that from main on your sqlDesigner instance to get the string.

    Its not a good idea to let other classes have direct access to the inner UI as it leads
    to a program that is hard to change and a very messy structure.

  • MainWindow* MainWindow::mw;
    MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent), ui(new Ui::MainWindow)
    ,mDbConnection(ip,"SQL Server",username,passwd,Dbname,true)
    i have above code in mainWindow.cpp
    ip,username,passwd,Dbname are the parameters ,i have these parameter values in SqlDesigner.cpp and
    i want these parameter values to mainWindow.cpp
    but problem is first mainWindow.cpp is run and it take the null values.after that parameter values are passed form sqlDesigner.

  • Lifetime Qt Champion

    You could change main.cpp to first show SqlDesigner and then when closed
    get the paramters from it and give it to mainwindow where its constructed.

    int main(int argc, char* argv[]) {
      QApplication a(argc, argv);
      SqlDesigner sdd;
      sdd.exec(); // assumes its dialog. 
      // get the parameters here ? Maybe use a struct.
      MainWindow w(0,ip,"SQL Server",username,passwd,Dbname);;
      return a.exec();

Log in to reply