PDA

View Full Version : How to display the numbers in a QList<QString> ?



harish
2nd January 2012, 07:50
Hi all,
I am trying to display the multiples of a number using QList<QString> but i was not able to do it on my own.


In my coding when the user enters any number in the input it should display its multiple which means:for example,if the user enters number 4 the output should display the output as:4 8 12 16 20 24 28 32 36 40.

Likewise whenever the user enters any number it should display the output.

This is my code:

void MainWindow:n_pushButton_clicked()
{
int result;
bool ok;
int n1 = ui->lineEdit->text().toInt(&ok,10);

for (int i=1;i<= 10;i++)
{
result = n1 * i;
QList<QString> list ;


list << result;

}

I know that i am wrong somewhere anyone please help me with this?
So that i can know the answer.

Thanks in advance.



Regards,
Harish

Lesiok
2nd January 2012, 07:55
Every loop iteration creates new list. These are the basics of C++.

harish
2nd January 2012, 08:06
I know that every loop iteration creates new list but i am looking to display each and every value when printed.

I had tried this one now:Is this right ?

void MainWindow:n_pushButton_clicked()
{
int result;
bool ok;
int n1 = ui->lineEdit->text().toInt(&ok,10);
QList<QString> list ;
for (int i=1;i<= 10;i++)
{
result = n1 * i;
QString str;
str.append(QString("%1").arg(result));
list << result;
}

It shows the error: invalid conversion from 'int' to 'const char*' .

Any Solution for this?No one is there to help me?

Lesiok
2nd January 2012, 14:17
Bacuse QList<T> have only operator <<(const T & value). So for QList<QString> You can do only <<(QString) not <<(int).
Your code should looks like :
for (int i=1;i<= 10;i++)
{
result = n1 * i;
list << QString::number(result);
}
And please use CODE tags in Your post.

harish
3rd January 2012, 05:26
Ok Lesiok thank you for your reply and i will use CODE tags from my post hereafter.

Thank you again..