Search code examples
c++qtqt4qtablewidgetqtablewidgetitem

Inserting Images from directory in QTableWidget


Well, hi all. I've got a question. I have a QTableWidget where I need put images from directory and retrieve name of image that was in selected cell. How can it automatically generate number of rows and columns depending of number of files in directory? I can add image to table by hand but it's not that i want to do. I suppose it's must be something with

QDir dir("images/");
QFileInfoList dirContent = dir.entryInfoList(QStringList()<< "*.png", QDir::Files | 
QDir::NoDotAndDotDot);

But still can't figure how can I do this.


Solution

  • There are two approaches.

    Non-recursive

    main.cpp

    #include <QDir>
    #include <QFileInfo>
    #include <QTableWidget>
    #include <QPixmap>
    #include <QApplication>
    
    int main(int argc, char **argv)
    {
        QApplication application(argc, argv);
        QTableWidget tableWidget(100, 5);
        QDir dir("images/");
        for (const auto& fileInfo : dir.entryInfoList(QStringList{"*.png"}, QDir::Files | QDir::NoDotAndDotDot))
        {
            static int row = 0, column = 0;
            QTableWidgetItem *newItem = new QTableWidgetItem(QObject::tr("%1").arg((row+1)*(column+1)));
            newItem->setData(Qt::DecorationRole, QPixmap(fileInfo.absoluteFilePath()));
            tableWidget.setItem(row, column, newItem);
            if (column == tableWidget.columnCount()) {
                column = 0;
                row++;
            }
        }
        tableWidget.show();
        return application.exec();
    }
    

    main.pro

    TEMPLATE = app
    TARGET = main
    QT += widgets
    CONFIG += c++11
    SOURCES += main.cpp
    

    Build and Run

    qmake && make && ./main
    

    Recursive

    main.cpp

    #include <QDir>
    #include <QDirIterator>
    #include <QFileInfo>
    #include <QTableWidget>
    #include <QPixmap>
    #include <QApplication>
    
    int main(int argc, char **argv)
    {
        QApplication application(argc, argv);
        QTableWidget tableWidget(100, 5);
        QDir dir("images/");
        dir.setFilter(QDir::NoDotAndDotDot| QDir::Files);
        QDirIterator it(dir, QDirIterator::Subdirectories);
        while (it.hasNext()) {
            static int row = 0, column = 0;
            it.next();
            QFileInfo Info = it.fileInfo();
            QString path = Info.absolutePath();
            if(Info.isFile() && path.endsWith(".png")) {
                QTableWidgetItem *newItem = new QTableWidgetItem(QObject::tr("%1").arg((row+1)*(column+1)));
                newItem->setData(Qt::DecorationRole, QPixmap(path));
                tableWidget.setItem(row, column, newItem);
                if (column == tableWidget.columnCount()) {
                    column = 0;
                    row++;
                }
            }
        }
        tableWidget.show();
        return application.exec();
    }
    

    main.pro

    TEMPLATE = app
    TARGET = main
    QT += widgets
    CONFIG += c++11
    SOURCES += main.cpp
    

    Build and Run

    qmake && make && ./main