How to set icons in QTableWidget based on specific number?
-
Hello, i'm trying to set Icons into a tablewidget based on an integer. I have a function that reads a file where i have strings like:
0;2
1;6
2;1
...
60;95
where the first number is the position of the item in the QtableWidget, and the second number is the icon that it want to draw on the table, so the previous numbers are:
0 (item in position (0,0) ; 2 ( Qicon(":/images/audi"));
every second number on the file is a different car image in my program resources.
So in order to do this i did a switch where i pass the second number, that indicates which icon there is.switch(number){ case 1: ui->tableWidget->item(0,0)->setIcon(QIcon(":/images/mercedes.png")); break; case 2: ui->tableWidget->item(0,0)->setIcon(QIcon(":/images/audi.png")); break; case 3: ui->tableWidget->item(0,0)->setIcon(QIcon(":/images/bmw.png")); break; }
I have 4 more tableWidget
The table has 6rows and 2 column.
How can i assign a number to my specific icon in my resources?
Any suggestion on how can i do this?
Thank you. -
Hello, i'm trying to set Icons into a tablewidget based on an integer. I have a function that reads a file where i have strings like:
0;2
1;6
2;1
...
60;95
where the first number is the position of the item in the QtableWidget, and the second number is the icon that it want to draw on the table, so the previous numbers are:
0 (item in position (0,0) ; 2 ( Qicon(":/images/audi"));
every second number on the file is a different car image in my program resources.
So in order to do this i did a switch where i pass the second number, that indicates which icon there is.switch(number){ case 1: ui->tableWidget->item(0,0)->setIcon(QIcon(":/images/mercedes.png")); break; case 2: ui->tableWidget->item(0,0)->setIcon(QIcon(":/images/audi.png")); break; case 3: ui->tableWidget->item(0,0)->setIcon(QIcon(":/images/bmw.png")); break; }
I have 4 more tableWidget
The table has 6rows and 2 column.
How can i assign a number to my specific icon in my resources?
Any suggestion on how can i do this?
Thank you.@ApprenticeReno
Most obvious would be to store your icons in an array you can index by their number (noswitch
statement)? Or maybe a map if the numbers are not contiguous (but hopefully they are). -
@ApprenticeReno
Most obvious would be to store your icons in an array you can index by their number (noswitch
statement)? Or maybe a map if the numbers are not contiguous (but hopefully they are).@JonB Hi Jon, thank you for your answer.
The numbers of the item of the table are contiguos, where in my file i have like:
0;X
1;X
2;X
..
60;Xwhere in 0;X :
0 represents:
ui->tableWidget->item(0,0);
X should be:
setIcon(Qicon(":/images/ferrari.png"));in 60;X
60 represents:
ui->tableWidget_5->item(5,1);
and X it can be 1 of the 90+ icons that i have in the resource.How can i store icons in array ? and how do i index them by number ? my icons are not connected to numbers.
Can i write something like:
QIcon icon=new QIcon[90];
icon[1]=":/images/ferrari.png";
...
icon[90]=":/images/bugatti.png";and then i start the function that reads the text and i go like:
for example first line of the text is 0;90;
i get the number 90 and i call it n;
n=90;
ui->tableWidget->item(0,0)->setIcon(icon[90]);It gives me error saying i need conversion from 'QIcon' to non-scalar type 'QIcon'.
How do i store the icons in the array ?
@JonB Thank you for your patience. -
@JonB Hi Jon, thank you for your answer.
The numbers of the item of the table are contiguos, where in my file i have like:
0;X
1;X
2;X
..
60;Xwhere in 0;X :
0 represents:
ui->tableWidget->item(0,0);
X should be:
setIcon(Qicon(":/images/ferrari.png"));in 60;X
60 represents:
ui->tableWidget_5->item(5,1);
and X it can be 1 of the 90+ icons that i have in the resource.How can i store icons in array ? and how do i index them by number ? my icons are not connected to numbers.
Can i write something like:
QIcon icon=new QIcon[90];
icon[1]=":/images/ferrari.png";
...
icon[90]=":/images/bugatti.png";and then i start the function that reads the text and i go like:
for example first line of the text is 0;90;
i get the number 90 and i call it n;
n=90;
ui->tableWidget->item(0,0)->setIcon(icon[90]);It gives me error saying i need conversion from 'QIcon' to non-scalar type 'QIcon'.
How do i store the icons in the array ?
@JonB Thank you for your patience.@ApprenticeReno hi,
If you want an array, use QVector.
As for the error you are getting, please check the stack VS heap allocation C++ concept.
-
A ApprenticeReno has marked this topic as solved on