Shortening the code

I have 9 pushbuttons with the same onclick event:

1
2
3
4
void MainWindow::on_pushButton_8_clicked()
{
    podesi(ui->pushButton_8,8);
}

Dose anyone know how how I can put this in less number of lines?

I also have an array of pointers to those buttons, but since it isn't in the scope of mainwindow construcor I had to put it in in statement in podesi() finction whic I call onclick in above code.
Dose anyone know how I can do this in the constructor?
Depending on the API you're using, you can usually map all button presses to the same function, then get the pushbutton's ID within that function to differentiate them.

But it depends on what API you're using.
Last edited on
I'm useing Qt.

I made a button grop but I don't know how to add onclick event with it.

And I have a meny bar with 2 options. One should be a drop down meny with for now only one option and the other one would just be like a push button with onclick event.

Also any idea on how to shorten the sode is welcome.

This is my code:
I think you can quess how the rest of the code looks like...

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

QPushButton *button[9];
char xo[2][2]={"X","O"};
char po[2][2]={">",""};
char pp[9][2]={"1","2","3","4","5","6","7","8","9"};
unsigned int naredu=0,naredu1=0,potez=1,xp=0,op=0,np=0;
int pritisnuto[3][3]={0};
bool kraj=0,pct=1;

void MainWindow::on_startButton_clicked()
{
    if (pct) return;
    naredu1=!naredu1;
    naredu=naredu1;
    potez=1;
    kraj=0;
    int n;
    for (n=0; n<9; n++)
        button[n]->setText("");
    for (n=0; n<3; n++)
        for (int m=0; m<3; m++)
            pritisnuto[n][m]=0;
    if (naredu1)
    {
        ui->label_tx->setText(po[naredu1]);
        ui->label_to->setText(po[!naredu1]);
    }
}

void MainWindow::on_pushButton_0_clicked()
{
    podesi(ui->pushButton_0,0);
}

void MainWindow::on_pushButton_1_clicked()
{
    podesi(ui->pushButton_1,1);
}

void MainWindow::on_pushButton_2_clicked()
{
    podesi(ui->pushButton_2,2);
}

void MainWindow::on_pushButton_3_clicked()
{
    podesi(ui->pushButton_3,3);
}

void MainWindow::on_pushButton_4_clicked()
{
    podesi(ui->pushButton_4,4);
}

void MainWindow::on_pushButton_5_clicked()
{
    podesi(ui->pushButton_5,5);
}

void MainWindow::on_pushButton_6_clicked()
{
    podesi(ui->pushButton_6,6);
}

void MainWindow::on_pushButton_7_clicked()
{
    podesi(ui->pushButton_7,7);
}

void MainWindow::on_pushButton_8_clicked()
{
    podesi(ui->pushButton_8,8);
}

void MainWindow::podesi(QPushButton *pushButton, int dugme)
{
    if (kraj) return;

    if (pct)
    {
        button[0]=ui->pushButton_0;
        button[1]=ui->pushButton_1;
        button[2]=ui->pushButton_2;
        button[3]=ui->pushButton_3;
        button[4]=ui->pushButton_4;
        button[5]=ui->pushButton_5;
        button[6]=ui->pushButton_6;
        button[7]=ui->pushButton_7;
        button[8]=ui->pushButton_8;
        pct=0;
    }

    int n=0,m=dugme;
    while (m>2) { n++; m-=3; }

    if (pritisnuto[n][m]) return;

    pushButton->setText(xo[naredu]);
    pritisnuto[n][m]=naredu+1;
    naredu=!naredu;
    potez++;

    ui->label_tx->setText(po[naredu]);
    ui->label_to->setText(po[!naredu]);

    if (potez>4) {
        if(pritisnuto[0][0]!=0 && pritisnuto[0][0]==pritisnuto[1][1] && pritisnuto[0][0]==pritisnuto[2][2] ||
           pritisnuto[0][2]!=0 && pritisnuto[0][2]==pritisnuto[1][1] && pritisnuto[0][2]==pritisnuto[2][0])
            goto krj;
    for (int a=0; a<3; a++)
        if(pritisnuto[a][0]!=0 && pritisnuto[a][0]==pritisnuto[a][1] && pritisnuto[a][0]==pritisnuto[a][2] ||
           pritisnuto[0][a]!=0 && pritisnuto[0][a]==pritisnuto[1][a] && pritisnuto[0][a]==pritisnuto[2][a])
            goto krj;
    }
    if (potez==10) { ui->label_np->setText(pp[np++]); kraj++; }
    return;
    krj:
    if (naredu) ui->label_xp->setText(pp[xp++]);
    else ui->label_op->setText(pp[op++]);
    kraj++;
}
Unfortunately my knowledge of Qt's API is nonexistent, so I can't be of further help to you here. Sorry. I'll have to let someone else field this one.
Anyone else knows how Qt works?
I use Qt quite a bit, but I don't think that you can shorten this. By definition, each "slot" is a function. So each time you assign a slot to a pushbutton, you are assigning an entire function to that pushbutton. While it's a good idea, you can't assign the same slot with a different argument or something to a pushbutton.

Here's an example of a NumPad class that I made for a touchscreen. It has everything but the form generating the ui_numpad.h:

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <QDialog>
#include <QTextStream>
#include "ui_numpad.h" // Auto generated from UI.

namespace Ui {
class NumPad;
}

class NumPad : public QDialog
{
    Q_OBJECT
    
public:
    explicit NumPad(QWidget *parent = 0);
    ~NumPad();
    template <class T> T getNumber();
    QString getString();
    
private slots:
    void on_one_released();
    void on_two_released();
    void on_three_released();
    void on_four_released();
    void on_five_released();
    void on_six_released();
    void on_seven_released();
    void on_eight_released();
    void on_nine_released();
    void on_zero_released();
    void on_tripzero_released();
    void on_dot_released();
    void on_clear_released();
    void on_backspace_released();
    void on_cancel_released();
    void on_ok_released();

private:
    Ui::NumPad *ui;
    QString sNumber;
    void addNumber(QString str);
};

template <class T>
T NumPad::getNumber()
{
    T output;
    QTextStream qss(&sNumber);
    qss >> output;
    return output;
}

///////////////
// Source file
//////////////
NumPad::NumPad(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NumPad)
{
    ui->setupUi(this);
}

NumPad::~NumPad()
{
    delete ui;
}

void NumPad::addNumber(QString str)
{
    sNumber.append(str);
    ui->DisplayWindow->setText(sNumber);
}

void NumPad::on_one_released()     { addNumber("1");}
void NumPad::on_two_released()     { addNumber("2");}
void NumPad::on_three_released()   { addNumber("3");}
void NumPad::on_four_released()    { addNumber("4");}
void NumPad::on_five_released()    { addNumber("5");}
void NumPad::on_six_released()     { addNumber("6");}
void NumPad::on_seven_released()   { addNumber("7");}
void NumPad::on_eight_released()   { addNumber("8");}
void NumPad::on_nine_released()    { addNumber("9");}
void NumPad::on_zero_released()    { addNumber("0");}
void NumPad::on_tripzero_released(){ addNumber("000");}
void NumPad::on_dot_released()     { addNumber(".");}

void NumPad::on_clear_released()
{
    sNumber.clear();
    ui->DisplayWindow->setText(sNumber);
}

void NumPad::on_backspace_released()
{
    sNumber.remove(sNumber.length()-1,1);
    ui->DisplayWindow->setText(sNumber);
}

void NumPad::on_cancel_released()
{
    sNumber.clear();
    reject();
}

void NumPad::on_ok_released()
{
    if (sNumber.size())
        accept();
    else
        reject();
}

QString NumPad::getString()
{
    return sNumber;
}
Actually I take it back,

You can define arguments for slots, but you have to connect the signals manually (this means manually redoing a lot of the stuff that Qt Creator is automatically doing for you).
http://doc.qt.digia.com/qt/signalsandslots.html

To do this, I think the easiest way is to implement QSignalMapper. You'd have to map each pushbutton's signal to the slot. You'd be able to use a generic slot for this, but you'd be defining each signal separately in order to specify the argument for the slot, so I'm not sure if you're actually saving any typing by doing it this way.
http://qt-project.org/doc/qt-4.8/qsignalmapper.html
Conecting each object seperatedly, I don't think that would help either.
I quess I wont be being shortening this code, but nwm, I have better things to think about...
Topic archived. No new replies allowed.