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. [Solved] Passing data to dialog

[Solved] Passing data to dialog

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 2.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.
  • J Offline
    J Offline
    jocala
    wrote on last edited by
    #1

    I'm getting a value from a dialog, put sending data does not work.

    First the mainwindow: it passes the string "this is a test" to the dialog and receives the data into a qstring "fileName"

    @
    usbfileDialog sddialog;
    sddialog.setModal(true);
    sddialog.setCstring("this is a test");

          if(sddialog.exec() == QDialog::Accepted)
            fileName = sddialog.binfileName();  // this works great!
            else return;
    

    @

    usbfiledialog.h:

    @
    public:
    void setCstring(const QString &text);

    public:
    QString binfileName();

    @

    usbfiledialog.cpp

    @

    QString cstr1; //declared after the #includes, so it's global to the dialog.

    QString usbfileDialog::binfileName() {
    return ui->usblistWidget->currentItem()->text();
    }

    void usbfileDialog::setCstring(const QString &text) {
    cstr1 = text;
    }

    usbfileDialog::usbfileDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::usbfileDialog)
    {

    ui->setupUi(this);
    QMessageBox::critical(this,"",cstr1);  // always blank!
    

    }

    usbfileDialog::~usbfileDialog()
    {
    delete ui;
    }

    @

    the cstring cstr1 is always empty. I'm puzzled as to why.

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

      That's because usbfileDialog(QWidget * parent) is a constructor and it called first of all when you create object of usbfileDialog. If you want use cstr1 do this in setCstring(const QString &text) or after calling it.

      By the way try not use global variables, make cstr1 as member of class.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jocala
        wrote on last edited by
        #3

        I confess, I'm a total c++ noob. Could you please illustrate with some code?

        I gather I need to change my .h? And how does one avoid using globals by adding variables to the class?

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

          Ok. Can you explain what do you want to do?

          1 Reply Last reply
          0
          • JeroentjehomeJ Offline
            JeroentjehomeJ Offline
            Jeroentjehome
            wrote on last edited by
            #5

            Hi,
            In your setString update the ui member holding the string data. That should do the trick for now. What qxoz says is very important. Place the QString in your class definition as private member. Or in your case, delete in total this string. You should place it direct in your ui string.
            Something like this:
            @
            void usbFileDialog::setString(QString &NewString)
            {
            QMessageBox::critical(this, NewString);
            }
            @
            But maybe this should be placed before generating the Dialog as a child of MainWindow instead of the usbFileDialog. Then when the user might be able to call <cancel> and doesn't execute the dialog.

            Greetz, Jeroen

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jocala
              wrote on last edited by
              #6

              qxoz, Jeroentje, thanks for the replies.

              Ok. Can you explain what do you want to do?

              Sure. As seen in the code in the OP, I want to pass text to a dialog's qstring when the dialog is called.

              I took the:

              @
              QString cstr1; //declared after the #includes, so it's global to the dialog.
              @

              And placed it in my .h file

              @
              private:
              QString cstr1;
              @

              but I don't know what the following means, a code example would be very helpful:

              "That’s because usbfileDialog(QWidget * parent) is a constructor and it called first of all when you create object of usbfileDialog. If you want use cstr1 do this in setCstring(const QString &text) or after calling it."

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

                Well, sorry for my English :)
                First of all, remove:
                @ QMessageBox::critical(this,"",cstr1); // always blank!@

                Other parts of your code are ok for now.
                When you use your dialog
                @usbfileDialog sddialog;//usbfileDialog(QWidget * parent) called here
                sddialog.setModal(true);
                sddialog.setCstring("this is a test");
                //now cstr1 == "this is a test"
                sddialog.doSomeActionWithCstr1();//
                if(sddialog.exec() == QDialog::Accepted)
                fileName = sddialog.binfileName(); // this works great!
                else return;@
                i hope it is clearer now. However, i recommend you read some C++ book.
                for example
                "Introduction to Design Patterns in C++ with Qt":https://qt-project.org/books/view/introduction-to-design-patterns-in-c-with-qt
                "http://www.cplusplus.com/":http://www.cplusplus.com/
                "http://voidrealms.com/":http://voidrealms.com/

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jocala
                  wrote on last edited by
                  #8

                  Thanks for the reply. Problem solved.

                  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