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
Forum Updated to NodeBB v4.3 + New Features

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 2 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.
  • K Offline
    K Offline
    Kasun
    wrote on 4 Sept 2017, 10:08 last edited by Kasun 9 Apr 2017, 10:11
    #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
    • P Offline
      P Offline
      Pradeep Kumar
      wrote on 4 Sept 2017, 10:22 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 4 Sept 2017, 10:48
      2
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 4 Sept 2017, 10:24 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
        • P Pradeep Kumar
          4 Sept 2017, 10:22

          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 4 Sept 2017, 10:48 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);
          

          }

          M 1 Reply Last reply 4 Sept 2017, 10:52
          0
          • P Offline
            P Offline
            Pradeep Kumar
            wrote on 4 Sept 2017, 10:49 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
              4 Sept 2017, 10:48

              @Pradeep-Kumar

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

              ui->label->setText(str);
              

              }

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 4 Sept 2017, 10:52 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 4 Sept 2017, 10:54
              1
              • M mrjj
                4 Sept 2017, 10:52

                @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 4 Sept 2017, 10:54 last edited by Kasun 9 Apr 2017, 10:54
                #7

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

                M 1 Reply Last reply 4 Sept 2017, 10:57
                0
                • K Kasun
                  4 Sept 2017, 10:54

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

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 4 Sept 2017, 10:57 last edited by mrjj 9 Apr 2017, 11:02
                  #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 4 Sept 2017, 12:28 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

                    1/9

                    4 Sept 2017, 10:08

                    • Login

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