user3183586
user3183586

Reputation: 167

Displaying Integers on QLabels?

Basically i'm making a simple calculator program to understand the basics of C++ GUI however I get an error message leading to the line of code I have in the void MainWindow::addx() to label -> setText(c); with an error message of:

invalid user-defined conversion from 'int' to 'const QString&'[-fpermissive]

I really don't know what that error message means I assume it means I cant display an integer on a label. I just wanted to know if I am able to display an integer on my label or do I have to use a different widget.

class MainWindow() {
   int a, b, c;
   QLabel * label;
   ...
};

void MainWindow::addx()
    c = a + b;
    label -> setText(c);
}

Upvotes: 2

Views: 23158

Answers (3)

Kiribati
Kiribati

Reputation: 21

QLabel has setNum() methods taking int or double arguments:

label->setNum(c);

The setNum() methods format the supplied number and then set the text property with the formatted value. The documentation does not specify which locale is used for formatting, so you may wish to experiment.

Upvotes: 2

prajmus
prajmus

Reputation: 3271

You have to convert it:

firstnumberx();
secondnumberx();
c = a+b;
label->setText(QString::number(c));

Upvotes: 5

Digital_Reality
Digital_Reality

Reputation: 4738

This should work..

label -> setText(QString::number(c));

If you need to add multiple number inside some string you can try below..

label -> setText(QString("%1").arg(c));

Upvotes: 5

Related Questions