PDA

View Full Version : Input Dialog



Enoctaffie
13th February 2012, 13:49
Hi All

Can someone please assist me with this one. I am trying to use the Input dialog box using the statement QInputDialog::getInteger(0, "Title", "label", 1). The Input box comes with two push buttons, the "Cancel" and the "Ok" buttons. The "Ok" button is working properly but the cancel button is not, it is doing exaclty the same as the OK button.

KillGabio
13th February 2012, 14:25
int QInputDialog::getInt ( QWidget * parent, const QString & title, const QString & label, int value = 0, int min = -2147483647, int max = 2147483647, int step = 1, bool * ok = 0, Qt::WindowFlags flags = 0 ) [static]

Static convenience function to get an integer input from the user.

title is the text which is displayed in the title bar of the dialog. label is the text which is shown to the user (it should say what should be entered). value is the default integer which the spinbox will be set to. min and max are the minimum and maximum values the user may choose. step is the amount by which the values change as the user presses the arrow buttons to increment or decrement the value.

If ok is nonnull *ok will be set to true if the user pressed OK and to false if the user pressed Cancel. The dialog's parent is parent. The dialog will be modal and uses the widget flags.

On success, this function returns the integer which has been entered by the user; on failure, it returns the initial value.

Use this static function like this:

bool ok;
int i = QInputDialog::getInt(this, tr("QInputDialog::getInteger()"),
tr("Percentage:"), 25, 0, 100, 1, &ok);
if (ok)
integerLabel->setText(tr("%1%").arg(i));

Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QInputDialog constructors.

This function was introduced in Qt 4.5.



//------------All that said you have to use a bool value to know if the user pressed ok or cancel....

Enoctaffie
13th February 2012, 15:28
Thanks KillGabio

I am getting the following error messages;
1. invalid use of 'this' innon-member function
2. 'tr' was not declared in this scope
3. 'ok' was not declared in this scope

KillGabio
13th February 2012, 15:44
That was just an example extrated from the documentation of qt,...

1. it`s not working because you are probably calling "this" in main.cpp....gotta do it on another class (ie a Q_OBJECT)

2. to use tr you need to include #include <QTranslator>.......an example here: http://developer.qt.nokia.com/doc/qt-4.8/linguist-hellotr.html

3. ok was not declared because maybe you didnt declare: bool ok = false; *-*


HTH

Enoctaffie
13th February 2012, 16:01
Hi KillGabio

can I send you my code to your inbox so that you see what I am doind and going wrong

KillGabio
13th February 2012, 16:08
posted under the "[ CODE ][ /CODE ]" function of the forum so everyone can help you :D

Enoctaffie
13th February 2012, 16:17
#include <QtGui>
#include <QTranslator>

int main(int argc, char * argv[]){
QApplication app(argc, argv);
QTextStream cout(stdout, QIODevice::WriteOnly);

int answer;

do{
float fahrenehit, celcius;
celcius = QInputDialog::getDouble(0, ("Temperature Convertor"), "Temperature in Degrees Celcius:",1,-10, 100,1, 0, 0);
fahrenehit = (9 * celcius)/5 + 32;

QString response = QString("%1 Degrees Celcius converted to Fahrenehit is %2. \n%3")
.arg(celcius).arg(fahrenehit)
.arg("Do you want to convert again?");
answer = QMessageBox::question(0,"Convert again?",response,
QMessageBox::Yes | QMessageBox::No);
}while(answer == QMessageBox::Yes);
return EXIT_SUCCESS;
}

KillGabio
13th February 2012, 16:22
if you click go advanced when replying you have a # icon click it then it appears
so we can read the code and copy it easily ;) i `ll have a look

Enoctaffie
13th February 2012, 16:30
#include <QtGui>
#include <QTranslator>

int main(int argc, char * argv[]){
QApplication app(argc, argv);
QTextStream cout(stdout, QIODevice::WriteOnly);

int answer;

do{
float fahrenehit, celcius;
celcius = QInputDialog::getDouble(0, ("Temperature Convertor"), "Temperature in Degrees Celcius:",1,-10, 100,1, 0, 0);
fahrenehit = (9 * celcius)/5 + 32;

QString response = QString("%1 Degrees Celcius converted to Fahrenehit is %2. \n%3")
.arg(celcius).arg(fahrenehit)
.arg("Do you want to convert again?");
answer = QMessageBox::question(0,"Convert again?",response,
QMessageBox::Yes | QMessageBox::No);
}while(answer == QMessageBox::Yes);
return EXIT_SUCCESS;
}

KillGabio
13th February 2012, 16:42
I recommend you first to create and empty project, choose a template, in this case Qt Widget Project->Qt GUI Application...as you are intending to interact with the user..

that way you are going to have a mainWindow that displays everything you need...leave the main.cpp to load the thing the program is going to need..

Added after 6 minutes:



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QInputDialog>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//QTextStream cout(stdout, QIODevice::WriteOnly);

int answer;

do{
float fahrenehit, celcius;
celcius = QInputDialog::getDouble(0, ("Temperature Convertor"), "Temperature in Degrees Celcius:",1,-10, 100,1, 0, 0);
fahrenehit = (9 * celcius)/5 + 32;

QString response = QString("%1 Degrees Celcius converted to Fahrenehit is %2. \n%3").arg(celcius).arg(fahrenehit).arg("Do you want to convert again?");
answer = QMessageBox::question(0,"Convert again?",response, QMessageBox::Yes | QMessageBox::No);
}while(answer == QMessageBox::Yes);

this->close ();
}

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

this is almost the same code you posted....I left the QTextStream as I dont use it but everything worked fine....if you dont want to create a new project instead of

return EXIT_SUCCESS
do this


return a.exec();

Got it?

Enoctaffie
13th February 2012, 16:44
I am confussed now, we are not on the same wavelength

KillGabio
13th February 2012, 16:44
Your version would be this one


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
int answer;
do{
float fahrenehit, celcius;
celcius = QInputDialog::getDouble(0, ("Temperature Convertor"), "Temperature in Degrees Celcius:",1,-10, 100,1, 0, 0);
fahrenehit = (9 * celcius)/5 + 32;

QString response = QString("%1 Degrees Celcius converted to Fahrenehit is %2. \n%3").arg(celcius).arg(fahrenehit).arg("Do you want to convert again?");
answer = QMessageBox::question(0,"Convert again?",response, QMessageBox::Yes | QMessageBox::No);
}while(answer == QMessageBox::Yes);
return a.exec();
}

Enoctaffie
13th February 2012, 16:55
Thanks, thats my version but my problem is not solved because the cancel button still behaves like the on button

KillGabio
13th February 2012, 17:09
ok, sorry thought it didnt compile...let me have another look

Added after 9 minutes:

Here is the code, i was wrong about changing the return value as you are not opening any widget, everything happens in the main function..so return EXIT_SUCCESS was right. The only thing you were missing was the point that you were assigning an int as Standardbutton of the message box, so it was going to fail the conversion...


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMessageBox::StandardButton answer = QMessageBox::Yes;
do{
float fahrenehit, celcius;
celcius = QInputDialog::getDouble(0, ("Temperature Convertor"), "Temperature in Degrees Celcius:",1,-10, 100,1, 0, 0);
fahrenehit = (9 * celcius)/5 + 32;

QString response = QString("%1 Degrees Celcius converted to Fahrenehit is %2. \n%3").arg(celcius).arg(fahrenehit).arg("Do you want to convert again?");
answer = QMessageBox::question(0,"Convert again?",response, QMessageBox::Yes | QMessageBox::No);
}while(answer == QMessageBox::Yes);

return EXIT_SUCCESS;
}

hope it works now :D

Enoctaffie
13th February 2012, 17:22
Hi Kill Gabio

The same thing happens, if I click the ok button it continues to do the conversion and that is what the cancel button is doing as well. I want the cancel button to exit the application.

KillGabio
13th February 2012, 18:02
*-*

Do you have something else I`m missing? cause I run that code and works just fine!


Starting C:\Documents and Settings\home\...\debug\TestSomething.exe...
C:\Documents and Settings\home\...\debug\TestSomething.exe exited with code 0

Grrrr now I get what you mean....that if cancel is pressed the conversion isn`t shown...let me think

Added after 8 minutes:



int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMessageBox::StandardButton answer = QMessageBox::Yes;
bool keep_asking = true;
do{
float fahrenehit, celcius;
celcius = QInputDialog::getDouble(0, ("Temperature Convertor"), "Temperature in Degrees Celcius:",1,-10, 100,1, &keep_asking, 0);
if (keep_asking){
fahrenehit = (9 * celcius)/5 + 32;

QString response = QString("%1 Degrees Celcius converted to Fahrenehit is %2. \n%3").arg(celcius).arg(fahrenehit).arg("Do you want to convert again?");
answer = QMessageBox::question(0,"Convert again?",response, QMessageBox::Yes | QMessageBox::No);
}
}while(answer == QMessageBox::Yes && keep_asking);

return EXIT_SUCCESS;
}

This will do the trick ....you confused me lol, the answer was in the first post i made here...you gotta pass a bool variable to know what key (cancel/ok) was pressed...if it was ok you do the math, if not program exits...