Search code examples
c++qtqtableviewqicon

Tableview set a image as a icon without showing the path as text


I have a 4 column QtableView that I am filling from a .csv file. The first column cells shows a custom icon from a folder of images and the rest of the columns are text only. Everything works as expected but the path to the icon image is shown as text beside the image in the same cell. Is there a way to show the image only and not show the path to it?

{
QFile file("/home/image.csv");
    if (file.open(QIODevice::ReadOnly))
    {
        int lineindex = 0;
        QTextStream in(&file);

        while (!in.atEnd()) {

            QStringList lineToken;
            QString fileLine = in.readLine();

            lineToken = fileLine.split(",", QString::SkipEmptyParts);

            for (int j = 0; j < lineToken.size(); j++)
            {
                QString myValue = lineToken.at(j);
                QStandardItem *lineItem = new QStandardItem(myValue);
                lineItem->setIcon(QIcon(myValue));

                modeli->setItem(lineindex, j, lineItem);
            }
            lineindex++;
        }
        file.close();
        ui->tableView->setModel(modeli);
    }
    ui->tableView->resizeRowsToContents();
}

Solution

  • I finally got it working. Thank you musicamante your comment got me thinking about a different way to approach this.

    {
        QFile file("/home/user/images/image.csv");
        if (file.open(QIODevice::ReadOnly))
        {
            int lineindex = 0;          
            QTextStream in(&file);      
    
            while (!in.atEnd()) {
    
                QStringList lineToken;
                QString fileLine = in.readLine();   
    
                lineToken = fileLine.split(",", QString::SkipEmptyParts);
    
                for (int j = 0; j < lineToken.size(); j++)
                {
                    QString myValue = lineToken.at(j);
                    QStandardItem *lineItem = new QStandardItem(myValue);
                    QStandardItem *lineItem1 = new QStandardItem();
    
                    QString myPath = myValue.prepend("/home/user");
    
                    lineItem1->setIcon(QIcon(myPath));
    
                    modeli->setItem(lineindex, 0, lineItem1);
                    modeli->setItem(lineindex, j, lineItem);
                }
                lineindex++;
            }
            file.close();
            ui->tableView->setModel(modeli);
        }
        ui->tableView ->setIconSize(QSize(300, 300));
        ui->tableView->resizeRowsToContents();
        ui->tableView->resizeColumnsToContents();
    }