PDA

View Full Version : Can not access the QLineEdit text within the QDialog



baluk
7th September 2010, 07:19
Hi,

I am new to Qt programming. I am having a difficulty accessing the QDialog widgets data from the class where QDialog is initiated.

QDialog class has one button and a LineEdit widgets and this class is called from the another class where the LineEdit text is supposed to be used. But I am not able to get the QLineEdit text (Entered by the user) into the variable "newName" shown in the code below. I would be grateful to any help.

My code:



void finder::on_New_Button_clicked()
{
Dialog = new NameDialog();

newName = Dialog->getCname();
}

NameDialog::NameDialog(QWidget *parent) :
QDialog(parent)
{
cname = new QLineEdit;
button = new QPushButton(tr("ok"));
Warning = new QLabel(tr("Plase provide the name to new CoverLetter"));
Warning->hide();
leftLayout = new QHBoxLayout;
leftLayout->addWidget(cname);
leftLayout->addWidget(button);
vlayout = new QVBoxLayout;
vlayout->addLayout(leftLayout);
vlayout->addWidget(Warning);
setLayout(vlayout);
this->setMinimumSize(250,100);
this->setMaximumSize(250,100);
this->show();
connect(button,SIGNAL(clicked()), this, SLOT(readCname()));
}

void NameDialog ::readCname()
{
if(cname->text().isEmpty())
Warning->show();
else{
this->name = cname->text();

this->close();
}
}



QString NameDialog::getCname()
{
return name;
}

Lykurg
7th September 2010, 07:25
What is "this->name"? Also there is no need for such a local variable you can just call
QString NameDialog::getCname()
{
return cname->text();
}

Also you have use the wrong tags:
is what you are looking for.

tbscope
7th September 2010, 07:37
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.

baluk
7th September 2010, 08:03
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

Lykurg
7th September 2010, 08:06
why not? What do you get and what are you expecting. Also note that
void finder::on_New_Button_clicked()
{
Dialog = new NameDialog();

newName = Dialog->getCname();
} will return an empty string since you do not show your dialog.

EDIT:
this->setMinimumSize(250,100);
this->setMaximumSize(250,100);See QWidget::setFixedSize().

baluk
7th September 2010, 08:23
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.

Lykurg
7th September 2010, 09:13
Please make a minimal compilable example reproducing your problem that we can see what you are doing exactly.

baluk
7th September 2010, 10:15
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.




void finder::on_New_Button_clicked()
{

Dialog = new NameDialog();

newCleterName = Dialog->getCname();

}

class NameDialog : public QDialog
{
Q_OBJECT
public:
explicit NameDialog(QWidget *parent = 0);
QString getCname(); /* To retrun the name to main class*/

signals:

public slots:
void readCname(); /*To read the text from the LineEdit widget*/

private:
QLineEdit* cname;
QPushButton* button;
QHBoxLayout *leftLayout;
QLabel* Warning;
QVBoxLayout* vlayout;
QString name; // Coverletter name from the lineedit
};

NameDialog::NameDialog(QWidget *parent) :
QDialog(parent)
{
cname = new QLineEdit;
button = new QPushButton(tr("ok"));
Warning = new QLabel(tr("Plase provide the name to new CoverLetter"));
Warning->hide();
leftLayout = new QHBoxLayout;
leftLayout->addWidget(cname);
leftLayout->addWidget(button);
vlayout = new QVBoxLayout;
vlayout->addLayout(leftLayout);
vlayout->addWidget(Warning);
setLayout(vlayout);
this->setFixedSize(250,100);
this->show(); // Showing the Dialog box to the user
connect(button,SIGNAL(clicked()), this, SLOT(readCname()));
}

void NameDialog ::readCname()
{
if(cname->text().isEmpty())
Warning->show();
else{
this->name = cname->text();
this->close(); // Closing the Dialog after saving the LineEdit text to the "Name" variable
}
}



QString NameDialog::getCname()
{
return cname->text();
}




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

Lykurg
7th September 2010, 10:33
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


#include <QtGui>

class NameDialog : public QDialog
{
Q_OBJECT

private:
QLineEdit* cname;
QPushButton* button;
QHBoxLayout *leftLayout;
QLabel* Warning;
QVBoxLayout* vlayout;
QString name; // Coverletter name from the lineedit

public:

NameDialog(QWidget *parent) :
QDialog(parent)
{
cname = new QLineEdit;
button = new QPushButton(tr("ok"));
Warning = new QLabel(tr("Plase provide the name to new CoverLetter"));
Warning->hide();
leftLayout = new QHBoxLayout;
leftLayout->addWidget(cname);
leftLayout->addWidget(button);
vlayout = new QVBoxLayout;
vlayout->addLayout(leftLayout);
vlayout->addWidget(Warning);
setLayout(vlayout);
this->setFixedSize(250,100);
this->show(); // Showing the Dialog box to the user
connect(button,SIGNAL(clicked()), this, SLOT(readCname()));
}

QString getCname()
{
return cname->text();
}

public slots:
void readCname()
{
if(cname->text().isEmpty())
Warning->show();
else{
this->name = cname->text();
this->close(); // Closing the Dialog after saving the LineEdit text to the "Name" variable
}
}


};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);


QWidget w;
NameDialog Dialog(&w);
Dialog.exec();
qDebug() << Dialog.getCname();

// w.show();


return a.exec();
}

#include "main.moc"

baluk
7th September 2010, 12:36
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.



#include <QMainWindow>
#include "name.h"
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();

protected:
void changeEvent(QEvent *e);

private:
Ui::MainWindow *ui;
name* Dname;
private slots:
void on_pushButton_clicked();
};

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

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

void MainWindow::on_pushButton_clicked()
{
Dname = new name();
name1->func();
ui->label->setText(Dname->getCname()); // Here I am getting the problem (returning null string)
}

/*Dialog class */

#include <QObject>
#include <QDialog>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QLayout>

class name : public QDialog
{
Q_OBJECT
public:
explicit name(QWidget *parent = 0);
QString getCname(); /* To retrun the name to main class*/
void func();
signals:

public slots:
void readCname(); /*To read the text from the LineEdit widget*/

private:

QLineEdit* cname;
QPushButton* button;
QHBoxLayout *leftLayout;
QLabel* Warning;
QVBoxLayout* vlayout;
QString newname; // Cname from the lineedit

name::name(QWidget *parent) :
QObject(parent)
{


cname = new QLineEdit;
button = new QPushButton(tr("ok"));
Warning = new QLabel(tr("Please provide the name to new CoverLetter"));
Warning->hide();
leftLayout = new QHBoxLayout;
leftLayout->addWidget(cname);
leftLayout->addWidget(button);
vlayout = new QVBoxLayout;
vlayout->addLayout(leftLayout);
vlayout->addWidget(Warning);
this->setLayout(vlayout);
this->setFixedSize(250,100);

connect(button,SIGNAL(clicked()),this, SLOT(readCname()));
}


void name ::readCname()
{
if(cname->text().isEmpty())
Warning->show();
else{
this->newname = cname->text();
this->close();
}
}

QString name::getCname()
{
return cname->text();
}

/* Main class */

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}


Thank you,
Baluk

Lykurg
7th September 2010, 13:35
What does your func() function? As said in the last answer you have to call exec, in your case
void MainWindow::on_pushButton_clicked()
{
Dname = new name();
name1->func(); // ? that won't compile...
Dname->exec(); // <- new
ui->label->setText(Dname->getCname()); // Here I am getting the problem (returning null string)
}

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.

baluk
7th September 2010, 14:00
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

Lykurg
7th September 2010, 14:08
There is but
name::name(QWidget *parent) :
QObject(parent)
should be
name::name(QWidget *parent) :
QDialog(parent)
like you have declared it.

tbscope
7th September 2010, 14:08
name::name(QWidget *parent) :
QObject(parent)

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

Zlatomir
7th September 2010, 14:10
Use show() in the place for exec() (QMainWindow doesn't have exec() only QDialog has) and use setWindowModality(...) (http://doc.qt.nokia.com/4.6/qwidget.html#windowModality-prop) to make it Modal if you really need a modal window.

baluk
7th September 2010, 14:26
Hi,

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

Thank you,
Baluk

rickrvo
8th April 2011, 16:56
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:



void teacherChatMain::returnPressedFunc()
{
QString text = lineEdit->text(); //allways returns ""
if (text.isEmpty())
return;
...
}

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;
}



void teacherChatMain::initialize( void )
{
scrollArea = new QScrollArea();
}

void teacherChatMain::startWorking( void )
{
winChatWidget = new QWidget;
winChatWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
winChatWidget->setMaximumWidth(350);

gridLayout = new QGridLayout;

stuNameLbl = new QLabel("",winChatWidget);
stuNameLbl->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
stuNameLbl->setMinimumSize(368,20);

gridLayout->addWidget(stuNameLbl, 0, 0, 0, 1);

closeBt = new QPushButton(winChatWidget);
closeBt->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
closeBt->setFixedSize(20, 20);

closeBt->setIcon(QIcon(":/resources/cancelar.png"));
closeBt->setIconSize(QSize(16, 16));

gridLayout->addWidget(closeBt, 0, 2);

textEdit = new QTextEdit(winChatWidget);
textEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
textEdit->setFocusPolicy(Qt::NoFocus);
textEdit->setMinimumSize(394,450);

gridLayout->addWidget(textEdit, 1, 0, 1, 1);

messageLbl = new QLabel(tr("Mensagem: "),winChatWidget);
messageLbl->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
messageLbl->setMinimumWidth(80);
messageLbl->setMaximumWidth(80);
gridLayout->addWidget(messageLbl, 2, 0);

lineEdit = new QLineEdit(winChatWidget);
messageLbl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
lineEdit->setFocusPolicy(Qt::StrongFocus);
lineEdit->setMinimumSize(307,20);
gridLayout->addWidget(lineEdit, 2, 1);

closeBt->setVisible(false);
stuNameLbl->setVisible(false);

tableFormat.setBorder(0);

connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressedFunc()));


winChatWidget->setLayout(gridLayout);
setTeacherName(_parent->getTeacherName());

setActive(false);

connect(this, SIGNAL(newMessage(QString,QString)),
this, SLOT(appendMessage(QString,QString)));

stGridDlg = new studentsGridDialog(this, studentsScrollArea,_parent->getLoggedStudents(), _parent->getTeacherName(), windowChatScrollArea);
QObject::connect(stGridDlg, SIGNAL(newMessageReceived()), this, SLOT(newMessageReceived()));
connect(this, SIGNAL(returnPressedMsg(QString, QString)), stGridDlg, SIGNAL(returnPressedSignal(QString, QString)));


contentsLayout->addWidget(studentsScrollArea,0,0);
contentsLayout->addWidget(winChatWidget,0,1);

scrollArea->setLayout(contentsLayout);
}


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