0

I am trying to count how many buttons have been checked

    void pracownik2::on_pushButton_4_clicked()
{

    this->setWindowTitle("EKRAN");
    QWidget *centralWidget = new QWidget;
            int licznik=1;
            QString licz;
            QString kolumny = ui->lineEdit->text();
            QString wiersze = ui->lineEdit_2->text();
            QPushButton *button[wiersze.toInt()][kolumny.toInt()];

            QGridLayout *controlsLayout = new QGridLayout;
            for(int i=0;i<wiersze.toInt();i++)
            {
                for(int j=0;j<kolumny.toInt();j++)
                {
                        licz = QString::number(licznik);
                        licznik++;
                        button[i][j] = new QPushButton(licz);
                        button[i][j]->setCheckable(1);
                        controlsLayout->addWidget(button[i][j], i, j);
                }
            }

            QPushButton *okej = new QPushButton("Zatwierdź");
            QPushButton *anul = new QPushButton("Anuluj");

            controlsLayout->addWidget(okej, wiersze.toInt(), 0);
            controlsLayout->addWidget(anul, wiersze.toInt(), 1);

            controlsLayout->setHorizontalSpacing(0);
            controlsLayout->setVerticalSpacing(0);
            centralWidget->setLayout(controlsLayout);

            setCentralWidget(centralWidget);



            for(int i=0;i<wiersze.toInt();i++)
            {
                for(int j=0;j<kolumny.toInt();j++)
                {
                        connect(button[i][j],SIGNAL(toggled(bool)),this,SLOT(tescik()));
                }
            }
            connect(anul,SIGNAL(clicked()),this,SLOT(close()));

            connect(okej,SIGNAL(clicked()),this,SLOT(okay2()));


}





    void pracownik2::tescik()
{

miejsca++;

}

    void pracownik2::okay2()
{

    QString m=QString::number(miejsca);
    QMessageBox::information(this,"elo","wybranych miejsc: " + m);
}

If i check buttons number 1,2,3 and press okay button it shows that 3 buttons have been checked BUT if I check button number 1 and uncheck it and press okay it shows that 2 buttons have been checked. How to make my variable increment only if I check button, not also when I uncheck it? I'm sorry for the code edit, just couldn't make it look better

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
CentusDBWA
  • 11
  • 4
  • Perhaps but I have no idea where to put this 'if' statement – CentusDBWA Jan 24 '15 at 10:38
  • You now have "increase count" in your code. Replace that with "if button was checked, then increase count, else decrease count". Or whatever you need to happen. – hyde Jan 24 '15 at 10:43
  • but there's a function for increasing and i just can't put if(button->isChecked()==true) there because buttons are made dynamically – CentusDBWA Jan 24 '15 at 10:45
  • `QPushButton` inherits this signal from `QAbstractButton`: http://doc.qt.io/qt-5/qabstractbutton.html#clicked – hyde Jan 24 '15 at 10:48
  • I tried different SIGNALS, I mean clicked(), pressed(), released() and it didn't work – CentusDBWA Jan 24 '15 at 10:50

1 Answers1

0

The signal toggled has a boolean parameter, add it to your slot, and adjust count according to it. Change connect:

connect(button[i][j],SIGNAL(toggled(bool)),this,SLOT(tescik(bool)));

And change slot:

void pracownik2::tescik(bool t) {
    if (t) miejsca++;
    else miejsca--;
}
hyde
  • 60,639
  • 21
  • 115
  • 176