0

I want to display a numerical value in a QPlainTextEdit object. I use below code for this purpose.

QString s;
s.sprintf("%d", deneme); //deneme is an integer value.
ui->results->setPlainText(s);

Is there any other method for displaying integer and float numbers in a QPlainTextEdit without defining a new QString object.

Thanks.

adba
  • 477
  • 9
  • 25

2 Answers2

1
QString::number()

You still have to create a QString instance (that is what QPlainTextEdit requires), but this may be at least a little more convenient.

There are a bunch of overloads of that method to accommodate various input and output formats.

Dave Mateer
  • 17,608
  • 15
  • 96
  • 149
  • `ui->results->setPlainText(QString::number(deneme));` Does this code also create a QString object or not? – adba Dec 25 '11 at 11:00
  • Yes; you can't get around the creation of a QString--that is what the `setPlainText` method requires. The usage above (probably) creates a temporary, and the compiler *might* optimize this away, but there is no guarantee. – Dave Mateer Dec 28 '11 at 14:15
1

How about:

ui->results->setPlainText(QString("%1").arg(deneme));
Phil Hannent
  • 12,047
  • 17
  • 71
  • 118