Results 1 to 16 of 16

Thread: Input Dialog

  1. #1
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Input Dialog

    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.

  2. #2
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Input Dialog

    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....

  3. #3
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Input Dialog

    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

  4. #4
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Input Dialog

    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...t-hellotr.html

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


    HTH

  5. #5
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Input Dialog

    Hi KillGabio

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

  6. #6
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Input Dialog

    posted under the "[ CODE ][ /CODE ]" function of the forum so everyone can help you

  7. #7
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Input Dialog

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

  8. #8
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Input Dialog

    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

  9. #9
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Input Dialog

    Qt Code:
    1. #include <QtGui>
    2. #include <QTranslator>
    3.  
    4. int main(int argc, char * argv[]){
    5. QApplication app(argc, argv);
    6. QTextStream cout(stdout, QIODevice::WriteOnly);
    7.  
    8. int answer;
    9.  
    10. do{
    11. float fahrenehit, celcius;
    12. celcius = QInputDialog::getDouble(0, ("Temperature Convertor"), "Temperature in Degrees Celcius:",1,-10, 100,1, 0, 0);
    13. fahrenehit = (9 * celcius)/5 + 32;
    14.  
    15. QString response = QString("%1 Degrees Celcius converted to Fahrenehit is %2. \n%3")
    16. .arg(celcius).arg(fahrenehit)
    17. .arg("Do you want to convert again?");
    18. answer = QMessageBox::question(0,"Convert again?",response,
    19. QMessageBox::Yes | QMessageBox::No);
    20. }while(answer == QMessageBox::Yes);
    21. return EXIT_SUCCESS;
    22. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Input Dialog

    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:


    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QMessageBox>
    4. #include <QInputDialog>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. //QTextStream cout(stdout, QIODevice::WriteOnly);
    12.  
    13. int answer;
    14.  
    15. do{
    16. float fahrenehit, celcius;
    17. celcius = QInputDialog::getDouble(0, ("Temperature Convertor"), "Temperature in Degrees Celcius:",1,-10, 100,1, 0, 0);
    18. fahrenehit = (9 * celcius)/5 + 32;
    19.  
    20. QString response = QString("%1 Degrees Celcius converted to Fahrenehit is %2. \n%3").arg(celcius).arg(fahrenehit).arg("Do you want to convert again?");
    21. answer = QMessageBox::question(0,"Convert again?",response, QMessageBox::Yes | QMessageBox::No);
    22. }while(answer == QMessageBox::Yes);
    23.  
    24. this->close ();
    25. }
    26.  
    27. MainWindow::~MainWindow()
    28. {
    29. delete ui;
    30. }
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. return a.exec();
    To copy to clipboard, switch view to plain text mode 

    Got it?
    Last edited by KillGabio; 13th February 2012 at 15:42.

  11. #11
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Input Dialog

    I am confussed now, we are not on the same wavelength

  12. #12
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Input Dialog

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

  13. #13
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Input Dialog

    Thanks, thats my version but my problem is not solved because the cancel button still behaves like the on button

  14. #14
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Input Dialog

    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...

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. QMessageBox::StandardButton answer = QMessageBox::Yes;
    5. do{
    6. float fahrenehit, celcius;
    7. celcius = QInputDialog::getDouble(0, ("Temperature Convertor"), "Temperature in Degrees Celcius:",1,-10, 100,1, 0, 0);
    8. fahrenehit = (9 * celcius)/5 + 32;
    9.  
    10. QString response = QString("%1 Degrees Celcius converted to Fahrenehit is %2. \n%3").arg(celcius).arg(fahrenehit).arg("Do you want to convert again?");
    11. answer = QMessageBox::question(0,"Convert again?",response, QMessageBox::Yes | QMessageBox::No);
    12. }while(answer == QMessageBox::Yes);
    13.  
    14. return EXIT_SUCCESS;
    15. }
    To copy to clipboard, switch view to plain text mode 

    hope it works now
    Last edited by KillGabio; 13th February 2012 at 16:09.

  15. #15
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Input Dialog

    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.

  16. #16
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Input Dialog

    *-*

    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:


    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. QMessageBox::StandardButton answer = QMessageBox::Yes;
    5. bool keep_asking = true;
    6. do{
    7. float fahrenehit, celcius;
    8. celcius = QInputDialog::getDouble(0, ("Temperature Convertor"), "Temperature in Degrees Celcius:",1,-10, 100,1, &keep_asking, 0);
    9. if (keep_asking){
    10. fahrenehit = (9 * celcius)/5 + 32;
    11.  
    12. QString response = QString("%1 Degrees Celcius converted to Fahrenehit is %2. \n%3").arg(celcius).arg(fahrenehit).arg("Do you want to convert again?");
    13. answer = QMessageBox::question(0,"Convert again?",response, QMessageBox::Yes | QMessageBox::No);
    14. }
    15. }while(answer == QMessageBox::Yes && keep_asking);
    16.  
    17. return EXIT_SUCCESS;
    18. }
    To copy to clipboard, switch view to plain text mode 

    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...
    Last edited by KillGabio; 13th February 2012 at 17:02.

  17. The following user says thank you to KillGabio for this useful post:

    Enoctaffie (14th February 2012)

Similar Threads

  1. Input Dialog
    By Enoctaffie in forum Qt Programming
    Replies: 1
    Last Post: 15th February 2012, 13:25
  2. Replies: 3
    Last Post: 19th October 2011, 13:08
  3. taking input from the input box
    By neutrino in forum Qt Programming
    Replies: 7
    Last Post: 30th September 2011, 17:09
  4. Input Dialog with Check Box?
    By PeterThePuter in forum Newbie
    Replies: 5
    Last Post: 26th October 2010, 05:30
  5. Why modeless dialog block keyboard input
    By franco.amato in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2010, 02:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.