Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set icons in QTableWidget based on specific number?
QtWS25 Last Chance

How to set icons in QTableWidget based on specific number?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 489 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ApprenticeRenoA Offline
    ApprenticeRenoA Offline
    ApprenticeReno
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • ApprenticeRenoA ApprenticeReno

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @ApprenticeReno
      Most obvious would be to store your icons in an array you can index by their number (no switch statement)? Or maybe a map if the numbers are not contiguous (but hopefully they are).

      ApprenticeRenoA 1 Reply Last reply
      0
      • JonBJ JonB

        @ApprenticeReno
        Most obvious would be to store your icons in an array you can index by their number (no switch statement)? Or maybe a map if the numbers are not contiguous (but hopefully they are).

        ApprenticeRenoA Offline
        ApprenticeRenoA Offline
        ApprenticeReno
        wrote on last edited by
        #3

        @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;X

        where 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.

        SGaistS 1 Reply Last reply
        0
        • ApprenticeRenoA ApprenticeReno

          @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;X

          where 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.

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • ApprenticeRenoA ApprenticeReno has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved