Please nominate your Qt Champions for 2021!

[Solved] Accessing QLineEdit in QDialog

  • 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
    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?


  • Moderators


    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.

  • Thanks for the quick response!

  • Moderators

    No problem! Glad to help!

  • 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?

  • Moderators

    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. :-)

  • 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.

Log in to reply