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] Accessing QLineEdit in QDialog
QtWS25 Last Chance

[Solved] Accessing QLineEdit in QDialog

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 9.6k 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
    kbt90
    wrote on last edited by
    #1

    Hello -

    I created a custom dialog that contains a QLineEdit. When a user presses a button on the main screen, the dialog opens up and allows the user to enter input. Upon returning the the main screen, that input needs to be grabbed and used.

    In QtDesigner, I created I dialog (with the Ok and Cancel buttons already on it) and added a QLineEdit with an object name of lineEdit. Here is my .h file for the dialog class:

    @class NewFolderDialog : public QDialog, private Ui::NewFolderDialog
    {
    Q_OBJECT
    public:
    NewFolderDialog (QWidget *parent = 0);
    };@

    Here is my .cpp file:
    @/******************************************************************************

    • NewFolderDialog constructor
      *****************************************************************************/
      NewFolderDialog::NewFolderDialog (QWidget *parent) : QDialog (parent)
      {
      setupUi (this);

    // show keyboard when QLineEdit is clicked on
    connect (lineEdit, SIGNAL (selectionChanged ()), this, SLOT (handleShowKeyboard ()));
    connect (lineEdit, SIGNAL (editingFinished ()), this, SLOT (handleHideKeyboard ()));
    }@

    The Ok and Cancel buttons are hooked up to accept () and reject () slots respectively. (This appears to happen automatically.)

    Here is where I use the dialog in main.cpp:
    @NewFolderDialog dialog (this);
    bool ok = false;
    ok = dialog.exec ();
    if (ok) {
    QString text = lineEdit->text ();
    }@

    The error I receive is "error: lineEdit was not declared in this scope".

    If I do @QString text = NewFolderDialog::lineEdit->text ();@

    The error I receive is "error: 'QLineEdit* Ui_NewFolderDialog::lineEdit' is inaccessible
    "error: object missing in reference to 'Ui_NewFolderDialog::lineEdit'"
    both in NewFolderDialog.h

    How do I correctly access the lineEdit?

    Thanks,
    Kate

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      Use

      @
      QString text = dialog.lineEdit->text();
      @

      The :: you tried doesn't refer to the instance of NewFolderDialog called "dialog" but rather the class itself. The dot operator (.) does.

      Edit: Oh, also to do this, you'd want to use the declaration
      @
      class NewFolderDialog : public QDialog, public Ui::NewFolderDialog
      @
      to make the lineEdit accessible outside the class. (Or write a public accessor method for NewFolderDialog that gives access to the lineEdit pointer.)

      Edit again: Or you could just write a NewFolderDialog::getTextFromLineEdit() that returns the string.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kbt90
        wrote on last edited by
        #3

        Thanks for the quick response!

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

          No problem! Glad to help!

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            I think this is the wrong solution. Please don't expose internals of classes, it leads to bad design.

            Instead, simply add a method like this to your NewFolderDialog:

            @
            //in header, in public section:
            QString folderName() const;

            //in implementation
            QString NewFolderDialog::folderName() const
            {
            return lineEdit->text();
            }
            @

            Then, in your code calling the dialog, you can just use
            @
            QString theFolder = theNewFolderDialogPtr->folderName();
            @

            That way, you don't expose that your dialog uses a line edit. What if you decide that you no longer want to use a line edit in that dialog, but you decide that instead, it should use an editable combobox that also presents the ten most recent choices? Do you really want to go and change code outside of the actual dialog, because you let other classes pry into the internals of your dialog class?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #6

              I agree that it wasn't the best solution. I originally was just helping to debug the syntax problems in the code that Kate had posted. You'll notice on my (second) edit of my post I did suggest creating an method for doing what you suggested. Just not as eloquently or in as much detail. :-)

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kbt90
                wrote on last edited by
                #7

                Thank you both for the responses.

                I have updated the code to keep all the internals of the dialog private, with public accessors if needed.

                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