Inserting images from directory into QTableWidget

OK Hello. I have a question. I have QTableWidget

where I need to put images from a directory and get the name of the image that was in the selected cell. How can it automatically generate the number of rows and columns based on the number of files in the directory? I can add the image to the table manually, but that is not what I want to do. I suppose it must be something with

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

      

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

+3


source to share


2 answers


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

      



+3


source


QFileInfoList

- it's simple QList<QFileInfo>

, so you can get the size of this list easily. Use something like this:

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

ui->tableWidget->setColumnCount(1);
ui->tableWidget->setRowCount(dirContent.size());

for(int i=0; i < dirContent.size(); i++)
{
    qDebug() << dirContent.at(i).absoluteFilePath();
    ui->tableWidget->item(i,0)->setData(Qt::DecorationRole, QPixmap(dirContent.at(i).absoluteFilePath()));
}

      



Also you can scale the image with the method scale()

and use:

ui->tableWidget->resizeRowsToContents();

      

+2


source







All Articles