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. Set icon to QTreeWidgetItem causes crash
Forum Update on Monday, May 27th 2025

Set icon to QTreeWidgetItem causes crash

Scheduled Pinned Locked Moved General and Desktop
qtreewidgetitemcrashicon
12 Posts 4 Posters 5.8k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    Did you allocate treeItem before using it ?

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

    M 1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      Did you allocate treeItem before using it ?

      M Offline
      M Offline
      MohammadReza
      wrote on last edited by MohammadReza
      #3

      @SGaist Yes. Here is the code:
      treeItem = new QTreeWidgetItem(ui->treeWidget);
      (Create new instance in a loop)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alex_malyu
        wrote on last edited by
        #4

        Did you try to take any QTreeWidget example (or write your own which create QTreeWidget, adds one item) and add the code which sets icon to a single item?
        Can you post it?

        1 Reply Last reply
        0
        • M MohammadReza

          Hi to all,
          I get icon of a file by this code:

          QFileInfo info(path);
          QFileIconProvider ip;
          QIcon icon=ip.icon(info);
          

          But when I set this icon to a column of a QTreeWidgetItem, it causes crash. Here is the code:
          treeItem->setIcon(0,icon);
          Thanks.

          R Offline
          R Offline
          raf924
          wrote on last edited by
          #5

          @MohammadReza I don't know if it'll help but why don't you just write

          treeItem->setIcon(0,QIcon(path));
          

          Also what's the crash error?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #6

            Additionally, can you share the complete code snippet where you create and setup these items ?

            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
            0
            • M Offline
              M Offline
              MohammadReza
              wrote on last edited by
              #7

              Here is the code:

                  QDir dir;
                  QFileInfoList qFIList = dir.drives();
              
                  QTreeWidgetItem *treeItem;
              
                  for(int i=0; i < qFIList.size(); i++)
                  {
                      treeItem = new QTreeWidgetItem(ui->treeWidget);
                      treeItem->setText(0, qFIList.at(i).absolutePath());
              
                      QFileInfo info(qFIList.at(i).absolutePath());
                      QFileIconProvider ip;
                      QIcon icon=ip.icon(info);
                      treeItem->setIcon(0,icon);
              
                      treeItem->setText(1, "description");
                  }
              

              This codes causes "has stopped working" crash but when I comment treeItem->setIcon(0,icon);
              everything works correctly.

              R 1 Reply Last reply
              0
              • M MohammadReza

                Here is the code:

                    QDir dir;
                    QFileInfoList qFIList = dir.drives();
                
                    QTreeWidgetItem *treeItem;
                
                    for(int i=0; i < qFIList.size(); i++)
                    {
                        treeItem = new QTreeWidgetItem(ui->treeWidget);
                        treeItem->setText(0, qFIList.at(i).absolutePath());
                
                        QFileInfo info(qFIList.at(i).absolutePath());
                        QFileIconProvider ip;
                        QIcon icon=ip.icon(info);
                        treeItem->setIcon(0,icon);
                
                        treeItem->setText(1, "description");
                    }
                

                This codes causes "has stopped working" crash but when I comment treeItem->setIcon(0,icon);
                everything works correctly.

                R Offline
                R Offline
                raf924
                wrote on last edited by raf924
                #8

                @MohammadReza

                QFileInfoList qFIList = dir.drives();
                

                that is your problem. what you want to do is

                QDir dir("/path/to/folder");
                QFileInfoList qFIList = dir.entryInfoList(QStringList()<<"*.jpg"<<"*.png",QDir::Files);
                
                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  MohammadReza
                  wrote on last edited by
                  #9

                  I filter directories and drives and remove them but the problem still exist for files.

                  When I use my code, most of the time icons of drive and files appear but after some second the stop working window error occurs:

                  Problem signature:
                  
                  
                  Problem Event Name: APPCRASH
                    Application Name: test-QTree.exe
                    Application Version:  0.0.0.0
                    Application Timestamp:    55e3f36b
                    Fault Module Name:    Qt5Widgetsd.dll
                    Fault Module Version: 5.5.0.0
                    Fault Module Timestamp:   559122fa
                    Exception Code:   c0000005
                    Exception Offset: 0000000000589022
                    OS Version:   6.1.7601.2.1.0.256.1
                    Locale ID:    1033
                    Additional Information 1: d87f
                    Additional Information 2: d87fc971aeb59f6c7e25e02ab99a0244
                    Additional Information 3: 1b94
                    Additional Information 4: 1b94c7629fda85c2d6bb0c6eacbea27e
                  
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    What do you get if you run that through the debugger ?

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

                    M 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      What do you get if you run that through the debugger ?

                      M Offline
                      M Offline
                      MohammadReza
                      wrote on last edited by
                      #11

                      @SGaist
                      There is no problem in debugging. I found an interesting thing:
                      I use this code to set an icon for a drive

                          QTreeWidgetItem *twi = ui->treeWidget->currentItem();
                          QString path = twi->text(0); //for example path is  L:/
                      
                          QFileInfo info(path);
                          QFileIconProvider ip;
                          QIcon icon=ip.icon(info);
                          twi->setIcon(0,icon);
                      

                      Everything is ok and the icon has been set and no crash anymore. But When the mouse cursor go around this item (twi) in QTreeList, the "has stopped working" error occurs.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        What is the stack trace when this happens ?

                        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
                        0

                        • Login

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