Can not access the QLineEdit text within the QDialog
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
Code:
{
return cname->text();
}
Also you have use the wrong tags: [CODE][/CODE] is what you are looking for.
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.
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
Re: Can not access the QLineEdit text within the QDialog
why not? What do you get and what are you expecting. Also note that
Code:
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:
Code:
this->setMinimumSize(250,100);
this->setMaximumSize(250,100);
See QWidget::setFixedSize().
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.
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.
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.
Code:
void finder::on_New_Button_clicked()
{
Dialog = new NameDialog();
newCleterName = Dialog->getCname();
}
{
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:
QString name;
// Coverletter name from the lineedit };
NameDialog
::NameDialog(QWidget *parent
) :{
Warning
= new QLabel(tr
("Plase provide the name to new CoverLetter"));
Warning->hide();
leftLayout->addWidget(cname);
leftLayout->addWidget(button);
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
}
}
{
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
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.
Quote:
I am not providing the full code for "Finder" sicne it has hunderds of lines.
Therefor I asked for a minimal compilable example
Code:
#include <QtGui>
{
Q_OBJECT
private:
QString name;
// Coverletter name from the lineedit
public:
{
Warning
= new QLabel(tr
("Plase provide the name to new CoverLetter"));
Warning->hide();
leftLayout->addWidget(cname);
leftLayout->addWidget(button);
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()));
}
{
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[])
{
NameDialog Dialog(&w);
Dialog.exec();
qDebug() << Dialog.getCname();
// w.show();
return a.exec();
}
#include "main.moc"
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.
Code:
#include <QMainWindow>
#include "name.h"
namespace Ui {
class MainWindow;
}
Q_OBJECT
public:
~MainWindow();
protected:
private:
Ui::MainWindow *ui;
name* Dname;
private slots:
void on_pushButton_clicked();
};
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *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>
{
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:
QString newname;
// Cname from the lineedit
{
Warning
= new QLabel(tr
("Please provide the name to new CoverLetter"));
Warning->hide();
leftLayout->addWidget(cname);
leftLayout->addWidget(button);
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();
}
}
{
return cname->text();
}
/* Main class */
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
Thank you,
Baluk
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
Code:
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.
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
Re: Can not access the QLineEdit text within the QDialog
There is but should be like you have declared it.
Re: Can not access the QLineEdit text within the QDialog
Is wrong. You need to inherit from QDialog to get the exec() function.
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.
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
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:
Code:
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;
}
Code:
void teacherChatMain::initialize( void )
{
}
void teacherChatMain::startWorking( void )
{
winChatWidget->setMaximumWidth(350);
stuNameLbl
= new QLabel("",winChatWidget
);
stuNameLbl->setMinimumSize(368,20);
gridLayout->addWidget(stuNameLbl, 0, 0, 0, 1);
closeBt->setFixedSize(20, 20);
closeBt
->setIcon
(QIcon(":/resources/cancelar.png"));
closeBt
->setIconSize
(QSize(16,
16));
gridLayout->addWidget(closeBt, 0, 2);
textEdit->setFocusPolicy(Qt::NoFocus);
textEdit->setMinimumSize(394,450);
gridLayout->addWidget(textEdit, 1, 0, 1, 1);
messageLbl
= new QLabel(tr
("Mensagem: "),winChatWidget
);
messageLbl->setMinimumWidth(80);
messageLbl->setMaximumWidth(80);
gridLayout->addWidget(messageLbl, 2, 0);
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);
stGridDlg = new studentsGridDialog(this, studentsScrollArea,_parent->getLoggedStudents(), _parent->getTeacherName(), windowChatScrollArea);
QObject::connect(stGridDlg,
SIGNAL(newMessageReceived
()),
this,
SLOT(newMessageReceived
()));
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!!!