Results 1 to 17 of 17

Thread: Can not access the QLineEdit text within the QDialog

  1. #1
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Can not access the QLineEdit text within the QDialog

    What is "this->name"? Also there is no need for such a local variable you can just call
    Qt Code:
    1. QString NameDialog::getCname()
    2. {
    3. return cname->text();
    4. }
    To copy to clipboard, switch view to plain text mode 

    Also you have use the wrong tags: [CODE][/CODE] is what you are looking for.

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Can not access the QLineEdit text within the QDialog

    A couple of techniques that you can apply:

    1. Use a settings class (singleton) or file (I prefer the file). In your dialog, for the Ok and Apply button, write the settings to the file (or set them in the settings class). When opening the dialog read the settings from the file or settings class and fill in the fields of your dialog.

    2. Use signals and slots. Again, when clicking the Ok or Apply button in your dialog, emit a signal that sends the new data to anything that is connected to the signal. I do not prefer this as when the dialog is complex, there needs to be a monstrous amount of signals or one monster of a signal.

    3. Return the data from the exec() function. But again, like in point 2, if there's a lot of data, this is not a fun technique.

  4. #4
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can not access the QLineEdit text within the QDialog

    Hi Lykurg,

    Thank you for the reply. I did try with no local variable like you suggested, but I couldn't get succeeded.

    Regards,
    Baluk

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Can not access the QLineEdit text within the QDialog

    why not? What do you get and what are you expecting. Also note that
    Qt Code:
    1. void finder::on_New_Button_clicked()
    2. {
    3. Dialog = new NameDialog();
    4.  
    5. newName = Dialog->getCname();
    6. }
    To copy to clipboard, switch view to plain text mode 
    will return an empty string since you do not show your dialog.

    EDIT:
    Qt Code:
    1. this->setMinimumSize(250,100);
    2. this->setMaximumSize(250,100);
    To copy to clipboard, switch view to plain text mode 
    See QWidget::setFixedSize().

  6. #6
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can not access the QLineEdit text within the QDialog

    Hi,

    Yes, I am getting an empty string but I am supposed to see some string which entered by the user in the LineEdit.

    I do showing the dialog from the Dialog constructor "this->show()" and closing the dialog "this->close()".

    Thank you,
    Baluk.

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Can not access the QLineEdit text within the QDialog

    Please make a minimal compilable example reproducing your problem that we can see what you are doing exactly.

  8. #8
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can not access the QLineEdit text within the QDialog

    Hi,

    Sorry, if I am not clear. I will now try to explain the problem in detail. I am implementing an application In which when a user clicks on the "New" button , a dialog box will be shown to enter the "name" of the new document to save. Now I have to catch that name from the LineEdit widget in the Dialog box. For this I have created a Dialog box with a class name "NameDialog". And I call this class from the "Finder" class.

    Qt Code:
    1. void finder::on_New_Button_clicked()
    2. {
    3.  
    4. Dialog = new NameDialog();
    5.  
    6. newCleterName = Dialog->getCname();
    7.  
    8. }
    9.  
    10. class NameDialog : public QDialog
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit NameDialog(QWidget *parent = 0);
    15. QString getCname(); /* To retrun the name to main class*/
    16.  
    17. signals:
    18.  
    19. public slots:
    20. void readCname(); /*To read the text from the LineEdit widget*/
    21.  
    22. private:
    23. QLineEdit* cname;
    24. QPushButton* button;
    25. QHBoxLayout *leftLayout;
    26. QLabel* Warning;
    27. QVBoxLayout* vlayout;
    28. QString name; // Coverletter name from the lineedit
    29. };
    30.  
    31. NameDialog::NameDialog(QWidget *parent) :
    32. QDialog(parent)
    33. {
    34. cname = new QLineEdit;
    35. button = new QPushButton(tr("ok"));
    36. Warning = new QLabel(tr("Plase provide the name to new CoverLetter"));
    37. Warning->hide();
    38. leftLayout = new QHBoxLayout;
    39. leftLayout->addWidget(cname);
    40. leftLayout->addWidget(button);
    41. vlayout = new QVBoxLayout;
    42. vlayout->addLayout(leftLayout);
    43. vlayout->addWidget(Warning);
    44. setLayout(vlayout);
    45. this->setFixedSize(250,100);
    46. this->show(); // Showing the Dialog box to the user
    47. connect(button,SIGNAL(clicked()), this, SLOT(readCname()));
    48. }
    49.  
    50. void NameDialog ::readCname()
    51. {
    52. if(cname->text().isEmpty())
    53. Warning->show();
    54. else{
    55. this->name = cname->text();
    56. this->close(); // Closing the Dialog after saving the LineEdit text to the "Name" variable
    57. }
    58. }
    59.  
    60.  
    61.  
    62. QString NameDialog::getCname()
    63. {
    64. return cname->text();
    65. }
    To copy to clipboard, switch view to plain text mode 

    The program is running fine with no errors except that I am getting a null string from the line "newName = Dialog->getCname();". I am not providing the full code for "Finder" sicne it has hunderds of lines.

    I hope you will understand my problem now.

    Thank you,
    Baluk

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Can not access the QLineEdit text within the QDialog

    You have to use QDialog::exec() or use show with a proper connection where you fetch the result after the dialog was closed.

    I am not providing the full code for "Finder" sicne it has hunderds of lines.
    Therefor I asked for a minimal compilable example

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class NameDialog : public QDialog
    4. {
    5. Q_OBJECT
    6.  
    7. private:
    8. QLineEdit* cname;
    9. QPushButton* button;
    10. QHBoxLayout *leftLayout;
    11. QLabel* Warning;
    12. QVBoxLayout* vlayout;
    13. QString name; // Coverletter name from the lineedit
    14.  
    15. public:
    16.  
    17. NameDialog(QWidget *parent) :
    18. QDialog(parent)
    19. {
    20. cname = new QLineEdit;
    21. button = new QPushButton(tr("ok"));
    22. Warning = new QLabel(tr("Plase provide the name to new CoverLetter"));
    23. Warning->hide();
    24. leftLayout = new QHBoxLayout;
    25. leftLayout->addWidget(cname);
    26. leftLayout->addWidget(button);
    27. vlayout = new QVBoxLayout;
    28. vlayout->addLayout(leftLayout);
    29. vlayout->addWidget(Warning);
    30. setLayout(vlayout);
    31. this->setFixedSize(250,100);
    32. this->show(); // Showing the Dialog box to the user
    33. connect(button,SIGNAL(clicked()), this, SLOT(readCname()));
    34. }
    35.  
    36. QString getCname()
    37. {
    38. return cname->text();
    39. }
    40.  
    41. public slots:
    42. void readCname()
    43. {
    44. if(cname->text().isEmpty())
    45. Warning->show();
    46. else{
    47. this->name = cname->text();
    48. this->close(); // Closing the Dialog after saving the LineEdit text to the "Name" variable
    49. }
    50. }
    51.  
    52.  
    53. };
    54.  
    55. int main(int argc, char *argv[])
    56. {
    57. QApplication a(argc, argv);
    58.  
    59.  
    60. NameDialog Dialog(&w);
    61. Dialog.exec();
    62. qDebug() << Dialog.getCname();
    63.  
    64. // w.show();
    65.  
    66.  
    67. return a.exec();
    68. }
    69.  
    70. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can not access the QLineEdit text within the QDialog

    Hi,

    Here I am providing the compiling code. Can you please go through it and tell me why I am not able to fetch the string.

    Qt Code:
    1. #include <QMainWindow>
    2. #include "name.h"
    3. namespace Ui {
    4. class MainWindow;
    5. }
    6.  
    7. class MainWindow : public QMainWindow {
    8. Q_OBJECT
    9. public:
    10. MainWindow(QWidget *parent = 0);
    11. ~MainWindow();
    12.  
    13. protected:
    14. void changeEvent(QEvent *e);
    15.  
    16. private:
    17. Ui::MainWindow *ui;
    18. name* Dname;
    19. private slots:
    20. void on_pushButton_clicked();
    21. };
    22.  
    23. #include "mainwindow.h"
    24. #include "ui_mainwindow.h"
    25.  
    26. MainWindow::MainWindow(QWidget *parent) :
    27. QMainWindow(parent),
    28. ui(new Ui::MainWindow)
    29. {
    30. ui->setupUi(this);
    31. }
    32.  
    33. MainWindow::~MainWindow()
    34. {
    35. delete ui;
    36. }
    37.  
    38. void MainWindow::on_pushButton_clicked()
    39. {
    40. Dname = new name();
    41. name1->func();
    42. ui->label->setText(Dname->getCname()); // Here I am getting the problem (returning null string)
    43. }
    44.  
    45. /*Dialog class */
    46.  
    47. #include <QObject>
    48. #include <QDialog>
    49. #include <QLineEdit>
    50. #include <QPushButton>
    51. #include <QLabel>
    52. #include <QLayout>
    53.  
    54. class name : public QDialog
    55. {
    56. Q_OBJECT
    57. public:
    58. explicit name(QWidget *parent = 0);
    59. QString getCname(); /* To retrun the name to main class*/
    60. void func();
    61. signals:
    62.  
    63. public slots:
    64. void readCname(); /*To read the text from the LineEdit widget*/
    65.  
    66. private:
    67.  
    68. QLineEdit* cname;
    69. QPushButton* button;
    70. QHBoxLayout *leftLayout;
    71. QLabel* Warning;
    72. QVBoxLayout* vlayout;
    73. QString newname; // Cname from the lineedit
    74.  
    75. name::name(QWidget *parent) :
    76. QObject(parent)
    77. {
    78.  
    79.  
    80. cname = new QLineEdit;
    81. button = new QPushButton(tr("ok"));
    82. Warning = new QLabel(tr("Please provide the name to new CoverLetter"));
    83. Warning->hide();
    84. leftLayout = new QHBoxLayout;
    85. leftLayout->addWidget(cname);
    86. leftLayout->addWidget(button);
    87. vlayout = new QVBoxLayout;
    88. vlayout->addLayout(leftLayout);
    89. vlayout->addWidget(Warning);
    90. this->setLayout(vlayout);
    91. this->setFixedSize(250,100);
    92.  
    93. connect(button,SIGNAL(clicked()),this, SLOT(readCname()));
    94. }
    95.  
    96.  
    97. void name ::readCname()
    98. {
    99. if(cname->text().isEmpty())
    100. Warning->show();
    101. else{
    102. this->newname = cname->text();
    103. this->close();
    104. }
    105. }
    106.  
    107. QString name::getCname()
    108. {
    109. return cname->text();
    110. }
    111.  
    112. /* Main class */
    113.  
    114. #include <QtGui/QApplication>
    115. #include "mainwindow.h"
    116.  
    117. int main(int argc, char *argv[])
    118. {
    119. QApplication a(argc, argv);
    120. MainWindow w;
    121. w.show();
    122. return a.exec();
    123. }
    To copy to clipboard, switch view to plain text mode 

    Thank you,
    Baluk
    Last edited by baluk; 7th September 2010 at 14:05.

  11. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Can not access the QLineEdit text within the QDialog

    What does your func() function? As said in the last answer you have to call exec, in your case
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. Dname = new name();
    4. name1->func(); // ? that won't compile...
    5. Dname->exec(); // <- new
    6. ui->label->setText(Dname->getCname()); // Here I am getting the problem (returning null string)
    7. }
    To copy to clipboard, switch view to plain text mode 
    or call show, but then you have to emit a signal from your dialog and fetch it in a slot of your main window where you then read the value of your line edit.

  12. #12
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can not access the QLineEdit text within the QDialog

    Hi,

    There is no use of "func()" it was included by mistake. I have included the line "Dname->exec();", But i get the error "the class has no member named exec(). I am trying to understand the second method.

    Thank you,
    Baluk

  13. #13
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Can not access the QLineEdit text within the QDialog

    There is but
    Qt Code:
    1. name::name(QWidget *parent) :
    2. QObject(parent)
    To copy to clipboard, switch view to plain text mode 
    should be
    Qt Code:
    1. name::name(QWidget *parent) :
    2. QDialog(parent)
    To copy to clipboard, switch view to plain text mode 
    like you have declared it.

  14. #14
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Can not access the QLineEdit text within the QDialog

    Qt Code:
    1. name::name(QWidget *parent) :
    2. QObject(parent)
    To copy to clipboard, switch view to plain text mode 

    Is wrong. You need to inherit from QDialog to get the exec() function.

  15. #15
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Can not access the QLineEdit text within the QDialog

    Use show() in the place for exec() (QMainWindow doesn't have exec() only QDialog has) and use setWindowModality(...) to make it Modal if you really need a modal window.

  16. #16
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can not access the QLineEdit text within the QDialog

    Hi,

    Now I got it working very fine . I am thankful to all of you especially to Lykurg for bearing me .

    Thank you,
    Baluk

  17. #17
    Join Date
    Dec 2010
    Posts
    71
    Thanks
    26
    Platforms
    Windows

    Default Re: Can not access the QLineEdit text within the QDialog

    Hi,

    I'm having a similar problem over a week... I tried everything... Created an Ui Form, created that form on code and both ways I can't get the QLineEdit to show it's text() value.

    Everything compiles fine and when running a function within the same class a the Widget was created, I keep getting an empty string when something is written on the lineEdit field on this function here:

    Qt Code:
    1. void teacherChatMain::returnPressedFunc()
    2. {
    3. QString text = lineEdit->text(); //allways returns ""
    4. if (text.isEmpty())
    5. return;
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 

    here is my constructor: (this is a plugin, I'll post 1st the mainWindow code then the plug in code)

    //MainWindow code

    bool mainWindow::instantiateTeacherChatPlugin( QObject * plugin )
    {
    iTChat = qobject_cast<ITeacherChat *>( plugin );
    if(iTChat)
    {
    iTChat->initialize();
    iTChat->setParentWidget(this);
    m_iTChat = plugin;
    chatTimerPull = new QTimer();
    return true;
    }
    return false;
    }

    Qt Code:
    1. void teacherChatMain::initialize( void )
    2. {
    3. scrollArea = new QScrollArea();
    4. }
    5.  
    6. void teacherChatMain::startWorking( void )
    7. {
    8. winChatWidget = new QWidget;
    9. winChatWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    10. winChatWidget->setMaximumWidth(350);
    11.  
    12. gridLayout = new QGridLayout;
    13.  
    14. stuNameLbl = new QLabel("",winChatWidget);
    15. stuNameLbl->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    16. stuNameLbl->setMinimumSize(368,20);
    17.  
    18. gridLayout->addWidget(stuNameLbl, 0, 0, 0, 1);
    19.  
    20. closeBt = new QPushButton(winChatWidget);
    21. closeBt->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    22. closeBt->setFixedSize(20, 20);
    23.  
    24. closeBt->setIcon(QIcon(":/resources/cancelar.png"));
    25. closeBt->setIconSize(QSize(16, 16));
    26.  
    27. gridLayout->addWidget(closeBt, 0, 2);
    28.  
    29. textEdit = new QTextEdit(winChatWidget);
    30. textEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    31. textEdit->setFocusPolicy(Qt::NoFocus);
    32. textEdit->setMinimumSize(394,450);
    33.  
    34. gridLayout->addWidget(textEdit, 1, 0, 1, 1);
    35.  
    36. messageLbl = new QLabel(tr("Mensagem: "),winChatWidget);
    37. messageLbl->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    38. messageLbl->setMinimumWidth(80);
    39. messageLbl->setMaximumWidth(80);
    40. gridLayout->addWidget(messageLbl, 2, 0);
    41.  
    42. lineEdit = new QLineEdit(winChatWidget);
    43. messageLbl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    44. lineEdit->setFocusPolicy(Qt::StrongFocus);
    45. lineEdit->setMinimumSize(307,20);
    46. gridLayout->addWidget(lineEdit, 2, 1);
    47.  
    48. closeBt->setVisible(false);
    49. stuNameLbl->setVisible(false);
    50.  
    51. tableFormat.setBorder(0);
    52.  
    53. connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressedFunc()));
    54.  
    55.  
    56. winChatWidget->setLayout(gridLayout);
    57. setTeacherName(_parent->getTeacherName());
    58.  
    59. setActive(false);
    60.  
    61. connect(this, SIGNAL(newMessage(QString,QString)),
    62. this, SLOT(appendMessage(QString,QString)));
    63.  
    64. stGridDlg = new studentsGridDialog(this, studentsScrollArea,_parent->getLoggedStudents(), _parent->getTeacherName(), windowChatScrollArea);
    65. QObject::connect(stGridDlg, SIGNAL(newMessageReceived()), this, SLOT(newMessageReceived()));
    66. connect(this, SIGNAL(returnPressedMsg(QString, QString)), stGridDlg, SIGNAL(returnPressedSignal(QString, QString)));
    67.  
    68.  
    69. contentsLayout->addWidget(studentsScrollArea,0,0);
    70. contentsLayout->addWidget(winChatWidget,0,1);
    71.  
    72. scrollArea->setLayout(contentsLayout);
    73. }
    To copy to clipboard, switch view to plain text mode 

    Please can some one help me? I'm about to try my next step which is throwing the computer out of the window!!!

Similar Threads

  1. I cannot get the string from QLineEdit->text()!
    By MIH1406 in forum Qt Programming
    Replies: 0
    Last Post: 3rd June 2010, 12:26
  2. cannot access QLineEdit text in other methods of the class...
    By Leoha_Zveri in forum Qt Programming
    Replies: 2
    Last Post: 29th September 2009, 13:07
  3. Setting the text of a QLineEdit
    By td in forum Newbie
    Replies: 2
    Last Post: 11th September 2008, 12:03
  4. Text before editting on QLineEdit
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 8th September 2008, 16:51
  5. QLineEdit text() crash - Qt3
    By user_mail07 in forum Qt Programming
    Replies: 7
    Last Post: 10th June 2008, 10:42

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.