PDA

View Full Version : MessageBox Validation



fortyhideout12
1st September 2009, 22:36
All,

I have a QTextEdit box that is filled with text. I also have a button which clears the text when clicked.

How would I make it so a message box appears when the button is clicked that would display "Are you sure you want to clear the log?" With a OK or Cancel button under it?



void Autotester::clearlogs()
{
uip.AutoLog->clear();

}


Heres the connect for the button, the button that clears the text is named uip.autoClear.


connect( uip.autoClear, SIGNAL ( clicked() ), this, SLOT( clearlogs() )); //clears Autotester log when clicked

Thanks guys

victor.fernandez
2nd September 2009, 08:36
void Autotester::clearlogs()
{
QMessageBox::StandardButton answer = QMessageBox::question(this, tr("Clear the textbox"), tr("Are you sure?"), QMessageBox::Yes, QMessageBox::Cancel);
if(answer == QMessageBox::Yes)
uip.AutoLog->clear();
}

fortyhideout12
2nd September 2009, 16:56
Thanks Victor.

Heres my final code that works --
void Autotester::uipAutoClearlogs()
{
int answer = QMessageBox::question(this, tr("Autotester Log"), tr("Are you sure you want to clear the current Autotester log?"), QMessageBox::Yes, QMessageBox::Cancel);
if (answer == QMessageBox::Yes)
uip.AutoLog->clear();


Now, my next question is. I want to put a validation in there to check if a user clicks on the clear button when there is no text available?

I'd want a QMessageBox to appear that would state "There is no text to clear".

Would I use another if on top of the statement?

victor.fernandez
2nd September 2009, 17:01
Would I use another if on top of the statement?

Yes. To display the message, you may use QMessageBox::information().

fortyhideout12
2nd September 2009, 17:04
void tester::uipEulpClearlogs()
{
if (uip.ECULPLog.isEmpty())
{
QMessageBox::question(this, tr("BLANK LOG"), tr("It's already cleared dumbo"),QMessageBox::Yes);
break;
}
else

int answer = QMessageBox::question(this, tr("ECULP Log"), tr("Are you sure you want to clear the current ECULP log?"), QMessageBox::Yes, QMessageBox::Cancel);
if (answer == QMessageBox::Yes)
uip.ECULPLog->clear();

}

I'm getting the error
-


1>.\tester.cpp(1431) : error C2228: left of '.isEmpty' must have class/struct/union
1> type is 'QTextEdit *'
1> did you intend to use '->' instead?

victor.fernandez
2nd September 2009, 17:05
As the compiler suggests, you have to use -> because uip.ECULPLog is a pointer to an object, not an object itself.


if (uip.ECULPLog->isEmpty())

fortyhideout12
2nd September 2009, 17:07
I tried that before and got this error.

1>.\tester.cpp(1431) : error C2039: 'isEmpty' : is not a member of 'QTextEdit'
1> c:\qt\4.5.1\include\qtgui\../../src/gui/widgets/qtextedit.h(70) : see declaration of 'QTextEdit'
1>.\tester.cpp(1434) : error C2043: illegal break
1>.\tester.cpp(1439) : error C2065: 'answer' : undeclared identifier

victor.fernandez
2nd September 2009, 17:07
You also forgot the brackets:


void tester::uipEulpClearlogs()
{
if (uip.ECULPLog->isEmpty())
{
QMessageBox::question(this, tr("BLANK LOG"), tr("It's already cleared dumbo"),QMessageBox::Yes);
break;
}
else {
int answer = QMessageBox::question(this, tr("ECULP Log"), tr("Are you sure you want to clear the current ECULP log?"), QMessageBox::Yes, QMessageBox::Cancel);
if (answer == QMessageBox::Yes)
uip.ECULPLog->clear();
}
}

victor.fernandez
2nd September 2009, 17:09
I tried that before and got this error.

Oh, that's true. If it's a QLineEdit you need to call the text() method:


if (uip.ECULPLog->text().isEmpty())

fortyhideout12
2nd September 2009, 17:11
It's a QTextEdit.


1>.\tester.cpp(1431) : error C2039: 'text' : is not a member of 'QTextEdit'
1> c:\qt\4.5.1\include\qtgui\../../src/gui/widgets/qtextedit.h(70) : see declaration of 'QTextEdit'
1>.\tester.cpp(1431) : error C2228: left of '.isEmpty' must have class/struct/union
1>.\tester.cpp(1434) : error C2043: illegal break

victor.fernandez
2nd September 2009, 17:14
Then it's either toPlainText() or toHtml() instead of text(), depending on whether you're using rich text or plain text.