PDA

View Full Version : Problem dealing with QDialog



baluk
16th January 2011, 09:51
Hi,

I have come across an issue with showing dialog box in my application. The problem is, I have a main application from where a dialog box is showed when a user clicks on a button. And my dialog box comprises of 2 LineEdit widgets and two Button widgets "OK" & "Cancel". I have connected the accept and reject result codes to respective buttons.

During the validation I have to check if the two lineedits are not empty when the user clicks on the "OK" button. If they are empty I have to keep the dialog box active otherwise close it. My problem is the dialog box is closed even if the fields are empty. I am confused on how to make this with my written logic code.



void MainWindow::getFormdetails()
{
fd = new formDialog();
QStringList data;

if(fd->exec() == 1)
{
data = fd->returnFormdata();
qDebug() << data;
}
}

formDialog::formDIalog()
{
connect(okButton, SIGNAL(clicked()), this, SLOT(formFieldCheck()));
connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
}
void formDialog::formFieldCheck()
{
if(formLedit[0]->text().isEmpty() || formLedit[1]->text().isEmpty())
{ QMessageBox::information(this, tr("Empty Field"),
tr("Please enter a name."));

}
else {list.append(formLedit[0]->text());
list.append(formLedit[1]->text());
list.append(age->text());
list.append(gender->currentText());
this->hide();}
}

QStringList formDialog::returnFormdata()
{
return list;
}

I would really appreciate if any one can help me with this.

tbscope
16th January 2011, 09:54
void MainWindow::getFormdetails()
{
fd = new formDialog();
QStringList data;

if(fd->exec() == 1)
{
data = fd->returnFormdata();
qDebug() << data;
}
}

formDialog::formDIalog()
{
connect(okButton, SIGNAL(clicked()), this, SLOT(formFieldCheck()));
//connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
}
void formDialog::formFieldCheck()
{
if(formLedit[0]->text().isEmpty() || formLedit[1]->text().isEmpty())
{ QMessageBox::information(this, tr("Empty Field"),
tr("Please enter a name."));

}
else {list.append(formLedit[0]->text());
list.append(formLedit[1]->text());
list.append(age->text());
list.append(gender->currentText());
accept(); // Call accept here
//this->hide();
}
}

QStringList formDialog::returnFormdata()
{
return list;
}

baluk
16th January 2011, 15:01
Yes it's working. Thank you tbscope. I understood the problem.