PDA

View Full Version : How to call member function from int main()



GussieBartlett
5th January 2012, 12:30
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();
}

marcvanriet
5th January 2012, 12:52
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

amleto
5th January 2012, 20:53
InputDialog dialog;
dialog.getTemp();

you need to learn c++ before you can use Qt.

wysota
5th January 2012, 21:06
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.