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. setIcon crash when i'm using tableView

setIcon crash when i'm using tableView

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 246 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.
  • F Offline
    F Offline
    fres
    wrote on last edited by fres
    #1

    Hi!
    I created a Tableview 4x4 with QT design.
    I have an icon in the (0,0) cell. I want to put this same icon into the (2,2) cell only when I click on the (0,0) cell. When I run this code, it crashes. I don't know why.

    image.png

    But, when I do this
    a.png

    it works. I don't know why I have to create another item... Is there a way to use setIcon() without it crashes?
    Thank you !

    eyllanescE 1 Reply Last reply
    0
    • F fres

      Hi!
      I created a Tableview 4x4 with QT design.
      I have an icon in the (0,0) cell. I want to put this same icon into the (2,2) cell only when I click on the (0,0) cell. When I run this code, it crashes. I don't know why.

      image.png

      But, when I do this
      a.png

      it works. I don't know why I have to create another item... Is there a way to use setIcon() without it crashes?
      Thank you !

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #2

      @fres cellClicked is emitted when the cell has been clicked even when there is no associated QTableWidgetItem so in your case the item probably does not exist, the solution is to check and if it does not exist then you must create it:

      QTableWidgetItem *item = ui->tableWidget->item(a, b);
      if(!item){
          item = new QTableWidgetItem;
          ui->tableWidget->setItem(a,  b, item);
      } 
      
      item->setIcon(ui->tableWidget->item(0, 0)->icon());

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      F 1 Reply Last reply
      2
      • eyllanescE eyllanesc

        @fres cellClicked is emitted when the cell has been clicked even when there is no associated QTableWidgetItem so in your case the item probably does not exist, the solution is to check and if it does not exist then you must create it:

        QTableWidgetItem *item = ui->tableWidget->item(a, b);
        if(!item){
            item = new QTableWidgetItem;
            ui->tableWidget->setItem(a,  b, item);
        } 
        
        item->setIcon(ui->tableWidget->item(0, 0)->icon());
        F Offline
        F Offline
        fres
        wrote on last edited by
        #3

        @eyllanesc Thank you! Indeed, the (2,2) case was empty...

        1 Reply Last reply
        0

        • Login

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