Hi,
I tried programming some GUI in Linux with C++ for the first time. Qt4 was recommended to me. Is that a good choice or does anybody have some better alternatives?
Now to my problem. I'm trying to program a calculator. First I built a little GUI with Qt-Designer and here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
//--- main.cpp - start ---
#include "calculator.h"
#include <QApplication>
int main( int argc, char* argv[])
{
QApplication a(argc, argv);
Calculator w;
w.show();
return a.exec();
}
//--- main.cpp - end ---
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
//--- calculator.h - start ---
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include "ui_calculator.h"
class Calculator : public QMainWindow, public Ui::MainWindow{
Q_OBJECT
public:
Calculator (QMainWindow *parent = 0);
~Calculator();
private slots:
void insert0();
};
#endif //CALCULATOR_H
//--- calculator.h - end ---
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
//--- calculator.cpp - start ---
#include "calculator.h"
Calculator::Calculator(QMainWindow *parent) : QMainWindow(parent){
setupUi(this);
connect(actCalQuit,SIGNAL (triggered()), qApp, SLOT(quit()));
connect(Button0, SIGNAL(clicked()), this, SLOT(insert0()));
}
Calculator::~Calculator(){}
void Calculator::insert0(){
LineEdit -> insert("0");
}
//--- calculator.cpp - end ---
|
LineEdit is the Line where you see the calculation. Button0 is a button with a zero which should insert a 0 in LineEdit. That's what the code does. But now I have also buttons with a 1,2,...,+,-,²,... and I don't want to create a new slot for each one.
Can the slots, which I defined, have arguments? like:
1 2 3
|
void Calculator::insertChar(const QString& c){
LineEdit -> insert(c);
}
|
in combination with
connect(Button0, SIGNAL(clicked()), this, SLOT(insertChar("0")));
At least that doesn't work, but I guess there is a way.
Thanks a lot for your help.