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 to Dialog from MainWindow

Sending QString to Dialog from MainWindow

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 2.5k Views 1 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.
  • C Offline
    C Offline
    chronix
    wrote on last edited by
    #1

    I have a QString in my MainWindow, now when the user click a button a new dialog popup. This dialog has a QLabel which i need to setText with the QString saved in MainWindow! how do i do it?
    Thanks in advance!

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Either modify the constructor of the dialog to accept additional parameter or add a setter method to your dialog class.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        chronix
        wrote on last edited by
        #3

        ok if go with modifying the constructor, where do i pass the argument?
        in MainWindow.cpp i use the following to launch the dialog
        @Dialog text(this)
        test.exec();@

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qxoz
          wrote on last edited by
          #4

          You can modify constructor like this:
          @Dialog ::Dialog (QWidget *parent, QString text) :
          QDialog(parent),
          ui(new Ui::Dialog)
          {
          ui->setupUi(this);
          ui->label->setText(text);
          }@
          don't forget set default value for text in *.h file
          and
          @Dialog text(this, someText);
          test.exec();@

          or you can add new method for settings text:
          @void Dialog::setLabelText(QString value)
          {
          ui->label->setText(value);
          }@

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            It should rather be:

            @
            Dialog ::Dialog (QString text, QWidget *parent) :
            QDialog(parent),
            ui(new Ui::Dialog)
            {
            ui->setupUi(this);
            ui->label->setText(text);
            }
            @

            to follow Qt's pattern

            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
            0
            • Q Offline
              Q Offline
              qxoz
              wrote on last edited by
              #6

              Yes you're right, thanks :)

              1 Reply Last reply
              0
              • C Offline
                C Offline
                chronix
                wrote on last edited by
                #7

                Thanks guys really. I tried the same thing bt i forgot to make the changes in the .h file! thats way it dint work! :D

                1 Reply Last reply
                0

                • Login

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