Problem with Qt

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.
I would have thought each button has to have it's own slot. That is what slots are for: defining an action (ie function) to be carried out.

You can have more than one slot for a control, eg to take care of different mouse events.

Also, your buttons do different things - trying to overload a slot for different controls doesn't make any sense to me at all.
Qt is great. I have the Qt Creator open in my other screen right now. I'm also learning.

I wrote a program over the course of a week in pure Win32 with my own wrapper. I still had problems making it OOP, so I tried Qt and did the same thing over 4 hours (learning included).

As for slots, yes you need 1 slot per button. I know, it can be a little verbose. I was reading up on QCommandLinkButton, perhaps that can help. It's a cross between a radio button (mutually exclusive) and a pushbutton. Perhaps you can find a function that will return an index. The function would have to come from the parent window holding these buttons.

http://doc.qt.nokia.com/qt-maemo/qcommandlinkbutton.html
I was working on a number pad for a touchscreen app last night which is similar. Here's the source that I used. Note that I used the creator, so half of the stuff is in ui_numpad.h which has 158 boring lines so I won't post them here unless you are super interested. The only important stuff is that the editor makes a ui class. That class contains my pushbuttons and the QLineEdit called DisplayWindow. I made the slots all call the same function so that I could use Notepad++ to type on multiple lines at once. That made the multiple slots easy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <QDialog>
#include <QTextStream>

class NumPad : public QDialog
{
    Q_OBJECT
public:
    explicit NumPad(QWidget *parent) : QDialog(parent), ui(new Ui::NumPad) { ui->setupUi(this); }
    ~NumPad()                    {    delete ui;       } 
    template <class T> T getNumber()
    {
        T output;
        QTextStream qss(&sNumber);
        qss >> output;
        return output;    
    }
    
private slots:
    void on_one_released()       {    sNumber.append("1");                   ui->DisplayWindow->setText(sNumber); }
    void on_two_released()       {    sNumber.append("2");                   ui->DisplayWindow->setText(sNumber); }
    void on_three_released()     {    sNumber.append("3");                   ui->DisplayWindow->setText(sNumber); }
    void on_four_released()      {    sNumber.append("4");                   ui->DisplayWindow->setText(sNumber); }
    void on_five_released()      {    sNumber.append("5");                   ui->DisplayWindow->setText(sNumber); }
    void on_six_released()       {    sNumber.append("6");                   ui->DisplayWindow->setText(sNumber); }
    void on_seven_released()     {    sNumber.append("7");                   ui->DisplayWindow->setText(sNumber); }
    void on_eight_released()     {    sNumber.append("8");                   ui->DisplayWindow->setText(sNumber); }
    void on_nine_released()      {    sNumber.append("9");                   ui->DisplayWindow->setText(sNumber); }
    void on_zero_released()      {    sNumber.append("0");                   ui->DisplayWindow->setText(sNumber); }
    void on_tripzero_released()  {    sNumber.append("000");                 ui->DisplayWindow->setText(sNumber); }
    void on_dot_released()       {    sNumber.append(".");                   ui->DisplayWindow->setText(sNumber); }
    void on_clear_released()     {    sNumber.clear();                       ui->DisplayWindow->setText(sNumber); }
    void on_backspace_released() {    sNumber.remove(sNumber.length()-1,1);  ui->DisplayWindow->setText(sNumber); }
    void on_ok_released()        {    accept();                                                                   }
    void on_cancel_released()    {    sNumber.clear();                       reject();                            }

private:
    Ui::NumPad *ui;
    QString sNumber;
};
Last edited on
Topic archived. No new replies allowed.