xPosed
xPosed

Reputation: 29

How do I display a counter function in qt

I know that in c++ you do int counter and when it does something I want its gonna do c++ and then cout<< counter to show the counter but now I want to do a GUI app which will have 20 buttons and when a button is pressed to do the counter++ and print the counter. I have done it but how do I do it to show the counter in a label?

code

        #include "form.h"
        #include "ui_form.h"
        #include <QString>
        #include <QLabel>

        Form::Form(QWidget *parent) :
            QWidget(parent),
            ui(new Ui::Form)
        {
            QString::number();
            QString s = QString::number();
            ui->setupUi(this);

        }

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

        void Form::on_pushButton_clicked()
        {

            counter++;

            ui->label->setText(QString::number(counter));
        }

header:

                           #ifndef FORM_H
        #define FORM_H

        #include <QWidget>
        #include <QString>

        namespace Ui {
        class Form;
        }

        class Form : public QWidget
        {
            Q_OBJECT

        public:
            explicit Form(QWidget *parent = 0);
            ~Form();

        private slots:
            void on_pushButton_clicked();
            int counter=0;

        private:
            Ui::Form *ui;
        };

        #endif // FORM_H

errors I get:

1)

no matching function for call to 'QString::number()' QString::number();

Upvotes: 1

Views: 3365

Answers (1)

retinotop
retinotop

Reputation: 483

#include "form.h"
#include "ui_form.h"
#include <QString>
#include <QLabel>

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    counter = 0;
    ui->setupUi(this);
}

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

void Form::on_pushButton_clicked()
{
    counter++;
    ui->label->setText(QString::number(counter));
}

Initialize your member in the constructor not in the header. And put the definition of your counter member to the private section not to the private slot section.

Upvotes: 2

Related Questions