Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Adding multiple items to a QTableWidget without freezing the window on Linux.
-
I have a code to get all the images on computer and list them in a QTableWidget, when I run the code on Windows it works fine, but when I run in Ubuntu the window is frozen. How can I run this code without freezing the window?
Code:
@void MainWindow::on_actionAdd_triggered()
{
connect(this, SIGNAL(add(QString,QString)), SLOT(addlist(QString,QString)), Qt::QueuedConnection);
QtConcurrent::run(this, &MainWindow::list);
}
void MainWindow::list()
{
QFileInfoList drives = QDir::drives();
foreach (QFileInfo drive, drives)
{
listing(drive.absoluteFilePath());
}
}void MainWindow::listing(QString path)
{
QDir dir(path);
QFileInfoList filelist = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
foreach(QFileInfo fileinfo, filelist)
{
if (fileinfo.isDir())
{
listing(fileinfo.absoluteFilePath());
}
if (fileinfo.isFile())
{
QString e = fileinfo.suffix().toLower();
if (e == "jpg" || e == "jpeg" || e == "gif" || e == "png" || e == "bmp")
{
emit add(QDir::convertSeparators(fileinfo.absoluteFilePath()), QString::number((fileinfo.size() / 1024)) + " " + "KB");
}
}
}
}void MainWindow::addlist(QString file, QString size)
{
int row = ui->tableWidget->rowCount();
ui->tableWidget->insertRow(row);
QTableWidgetItem* filename = new QTableWidgetItem(file);
filename->setFlags(filename->flags() ^ Qt::ItemIsEditable);
QTableWidgetItem* filesize = new QTableWidgetItem(size);
filesize->setFlags(filesize->flags() ^ Qt::ItemIsEditable);
ui->tableWidget->setItem(row, 0, filename);
ui->tableWidget->setItem(row, 1, filesize);
}@