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. Sending QString value from one form class to another
QtWS25 Last Chance

Sending QString value from one form class to another

Scheduled Pinned Locked Moved Unsolved General and Desktop
stringlabelsendform
9 Posts 5 Posters 4.0k 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.
  • K Offline
    K Offline
    Kasun
    wrote on last edited by Kasun
    #1

    I have declared a QString in mainwindow.h as

    public : QString str;

    I have used the str inside a method of mainwindow.cpp and it now has a string value. I have another form named dialog.cpp I want to display the contents of str in a label named label1 in dialog.cpp!

    Even though i imported mainwindow.h and mainwindow.cpp to dialog.cpp and used the following code to print the contents of str!

    ui->label->setText(str);

    It returns the error str as an undecalred identifier! How can i correct this?

    1 Reply Last reply
    0
    • Pradeep KumarP Offline
      Pradeep KumarP Offline
      Pradeep Kumar
      wrote on last edited by
      #2

      HI,

      have u declared str in dialog.cpp where u can show values to label,
      and can u post the code of how u made a string value avialable in dialog.cpp from mainwindow.cpp.

      Thanks,

      Pradeep Kumar
      Qt,QML Developer

      K 1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        Based on your question, you seem to be new to C++. I highly recommend that you start by reading a good book on the subject and then follow Qt's tutorial and examples.

        Also, there are already a lot of similar questions on this forum. Did you try to search it first ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • Pradeep KumarP Pradeep Kumar

          HI,

          have u declared str in dialog.cpp where u can show values to label,
          and can u post the code of how u made a string value avialable in dialog.cpp from mainwindow.cpp.

          Thanks,

          K Offline
          K Offline
          Kasun
          wrote on last edited by
          #4

          @Pradeep-Kumar

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

          ui->label->setText(str);
          

          }

          mrjjM 1 Reply Last reply
          0
          • Pradeep KumarP Offline
            Pradeep KumarP Offline
            Pradeep Kumar
            wrote on last edited by
            #5

            Hi,

            u have not declared str, so the reason getting error.

            Thanks,

            Pradeep Kumar
            Qt,QML Developer

            1 Reply Last reply
            0
            • K Kasun

              @Pradeep-Kumar

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

              ui->label->setText(str);
              

              }

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Kasun
              Hi, you create another instance of mainwindow.
              Not sure that is what you want..
              anyway, since str is inside the class, you must do

              MainWindow m;
              ui->label->setText(m.str);

              But the m is not the running mainwindow. Its a new one.

              K 1 Reply Last reply
              1
              • mrjjM mrjj

                @Kasun
                Hi, you create another instance of mainwindow.
                Not sure that is what you want..
                anyway, since str is inside the class, you must do

                MainWindow m;
                ui->label->setText(m.str);

                But the m is not the running mainwindow. Its a new one.

                K Offline
                K Offline
                Kasun
                wrote on last edited by Kasun
                #7

                @mrjj I need from the old window! How can I do it?

                mrjjM 1 Reply Last reply
                0
                • K Kasun

                  @mrjj I need from the old window! How can I do it?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @Kasun

                  Often its easy to share from mainwindow since it opens the dialog

                  So if you add function to set the text to dialog.
                  public:
                  void Dialog::SetMyText(QString text) {
                  ui->label->setText(text);
                  }

                  Then in mainwin where u show dialog
                  void Mainwindow::ShowDia() {
                  Dialog dia;
                  dia.setMyText(str);
                  dia.exec();
                  }

                  1 Reply Last reply
                  3
                  • AmoghA Offline
                    AmoghA Offline
                    Amogh
                    wrote on last edited by
                    #9

                    you can send the string to other class by simply placing it in the Signal, and catch the signal at the other class.

                    1 Reply Last reply
                    1

                    • Login

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