PDA

View Full Version : Need help with a basic calculator



Morea
24th March 2006, 08:59
I have attached the code, the problem is: it doesn't show anything. I can't figure out why. I think I've done it right, but obviously I haven't.
Can you see why I don't get any out put?

zlatko
24th March 2006, 09:06
Yes you forgot set main widget for your app..look in next code


#include <QApplication>

#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow *MainForm = new MainWindow;
app.setMainWidget(MainForm);
MainForm->show();
return app.exec();

}

jpn
24th March 2006, 09:09
I have attached the code, the problem is: it doesn't show anything. I can't figure out why. I think I've done it right, but obviously I haven't.
Can you see why I don't get any out put?
You can't add parameter values to signal-slot connections.
In the constructor of MainWindow:

change:


connect(ui.siffra9,SIGNAL(clicked()),&C,SLOT(addToCurrentNumber(9)));
[...]
connect(ui.siffra0,SIGNAL(clicked()),&C,SLOT(addToCurrentNumber(0)));


to:


#include <QSignalMapper>

QSignalMapper* mapper = new QSignalMapper(this);

connect(ui.siffra9, SIGNAL(clicked()), mapper, SLOT(map()));
mapper->setMapping(ui.siffra9, 9);
[...]
connect(ui.siffra0, SIGNAL(clicked()), mapper, SLOT(map()));
mapper->setMapping(ui.siffra0, 0);

connect(mapper, SIGNAL(mapped(int)), &C, SLOT(addToCurrentNumber(int)));

Morea
24th March 2006, 09:11
Yes you forgot set main widget for your app..look in next code


#include <QApplication>

#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow *MainForm = new MainWindow;
app.setMainWidget(MainForm);
MainForm->show();
return app.exec();

}

Strange, I've managed without that before.

Morea
24th March 2006, 09:13
You can't add parameter values to signal-slot connections.
In the constructor of MainWindow:

change:


connect(ui.siffra9,SIGNAL(clicked()),&C,SLOT(addToCurrentNumber(9)));
[...]
connect(ui.siffra0,SIGNAL(clicked()),&C,SLOT(addToCurrentNumber(0)));


to:


#include <QSignalMapper>

QSignalMapper* mapper = new QSignalMapper(this);

connect(ui.siffra9, SIGNAL(clicked()), mapper, SLOT(map()));
mapper->setMapping(ui.siffra9, 9);
[...]
connect(ui.siffra0, SIGNAL(clicked()), mapper, SLOT(map()));
mapper->setMapping(ui.siffra0, 0);

connect(mapper, SIGNAL(mapped(int)), &C, SLOT(addToCurrentNumber(int)));


I will try that, thank you.
Are there any other way of getting the same functionality? I mean, a different design, or you will most likely need to use the mapper unless you wish to create 10 different slots, one for each digit?

jpn
24th March 2006, 09:16
I will try that, thank you.
Are there any other way of getting the same functionality? I mean, a different design, or you will most likely need to use the mapper unless you wish to create 10 different slots, one for each digit?
The signal mapper is intended for situations like yours.
Yes, one approach could be to create a bunch of slots, a different slot for each digit.. Not very handy, right ;)

jpn
24th March 2006, 09:20
Yes you forgot set main widget for your app..look in next code
Isn't application's main widget obsolete in Qt 4..

wysota
25th March 2006, 20:18
One could use a QButtonGroup instead of a signal mapper too. It would be more natural than using QSignalMapper.