Set icon to QTreeWidgetItem causes crash



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


  • Lifetime Qt Champion

    Hi,

    Did you allocate treeItem before using it ?



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



  • 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?



  • @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?


  • Lifetime Qt Champion

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



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



  • @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);
    


  • 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
    

  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

    What is the stack trace when this happens ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.