Results 1 to 4 of 4

Thread: How to call member function from int main()

  1. #1
    Join Date
    Jan 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default How to call member function from int main()

    Hi I am a beginner in programming in C++ with Qt 4, I must write a program to ask the user for a Temperature in Celcius using QInputDialog, I wrote the header with the class and .cpp wiht the fucnction file, but are battling calling the function in the main.cpp. Please advice me on where I went wrong:

    Header:

    #ifndef INPUTDIALOG_H
    #define INPUTDIALOG_H

    class InputDialog
    {
    public:
    ~InputDialog();
    void getTemp();
    private slots:
    private:
    };
    #endif // INPUTDIALOG_H

    InputDialog.cpp:

    #include "inputdialog.h"
    #include <QWidget>
    #include <QInputDialog>
    #include <QMessageBox>
    #include <QObject>
    #include <QLabel>


    InputDialog::~InputDialog()
    {

    }

    void InputDialog::getTemp()
    {
    bool (ok);
    int *temperature = QInputDialog::getInt(temperature,tr("Enter a Temperature in Celsius"),tr("Celsius"),0,-30,100,1,&ok);
    if (ok)
    integerLable->setText(tr("%1%").arg(temperature));
    }

    Main.cpp:

    #include <QtGui/QApplication>
    #include "inputdialog.h"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    InputDialog.getTemp();

    return app.exec();
    }

  2. #2
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to call member function from int main()

    Hi,

    Roughly said, in a GUI application, you can't show anything to the user before your app.exec(). Create a dialog and call show() before the app.exec(). The dialog will be shown when app.exec() is called.

    In the dialog code, you can write stuff (create a button and attach a slot to its clicked event) to show the input dialog.

    See http://doc.trolltech.com/3.0/t1.html for a (very old) example.

    Best regards,
    Marc

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to call member function from int main()

    InputDialog dialog;
    dialog.getTemp();

    you need to learn c++ before you can use Qt.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to call member function from int main()

    Quote Originally Posted by marcvanriet View Post
    Roughly said, in a GUI application, you can't show anything to the user before your app.exec(). Create a dialog and call show() before the app.exec(). The dialog will be shown when app.exec() is called.
    Actually in this situation it would work provided the syntax was correct. QInputDialog launches a modal dialog, just like you would have called QDialog::exec() which spins an event loop and is able to show the dialog.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. to call member function
    By vinayaka in forum General Programming
    Replies: 5
    Last Post: 1st July 2011, 14:48
  2. cannot call member function without object
    By been_1990 in forum Qt Programming
    Replies: 11
    Last Post: 23rd October 2010, 18:12
  3. Replies: 2
    Last Post: 7th July 2009, 18:44
  4. Replies: 10
    Last Post: 29th May 2009, 10:06
  5. How to call the C++ member function from the JScript
    By parusri in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2008, 11:13

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.