快速填写QTableWidget

时间:2018-04-15 04:16:48

标签: qt filesystems

我正在创建一个非常简单的磁盘空间分析器,它显示特定foder及其子文件夹中每个文件的大小。我使用QStack存储路径,使用QTableWidget显示下一个信息(文件名,文件路径,文件大小和上次修改日期)。首先,我使用QDirIterator填充QStack,然后在不同的线程中,我通过QFileInfo获取有关文件的必要信息,并在QTableWidget中添加此信息。填充QStack很快但填充QTableWidget非常慢。问题是如何提高填充QTableWidget的速度。

dialog.h

#include <QDialog>
#include <QStack>
#include "mythread.h"

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    MyThread *mThread;
    QStack<QString> *fileStack;

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

public slots:
    void onFileAdded(QString file);

private:
    Ui::Dialog *ui;
};

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include "mythread.h"
#include <QFileDialog>
#include <QDirIterator>
#include <QMessageBox>
#include <QFileInfo>
#include <QDateTime>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    ui->tableWidget->setColumnCount(4);
    ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "File name" << "File Path" << "File Size (KB)" << "Last Modified Date");
    fileStack = new QStack<QString>();

    ui->tableWidget->setSortingEnabled(true);

    mThread = new MyThread(this);
    connect(mThread, SIGNAL(fileAdded(QString)), this, SLOT(onFileAdded(QString)));
}

Dialog::~Dialog()
{
    delete ui;
    delete mThread;
    delete fileStack;
}

void Dialog::onFileAdded(QString file) {
    QFileInfo fileInfo;
    fileInfo.setFile(file);
    ui->tableWidget->insertRow(ui->tableWidget->rowCount());
    ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1,
                             0,
                             new QTableWidgetItem(fileInfo.fileName()));

    ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1,
                             1,
                             new QTableWidgetItem(fileInfo.filePath()));
    int sss = fileInfo.size() / 1024;
    QTableWidgetItem *item = new QTableWidgetItem;
    item->setData(Qt::EditRole, sss);
    ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1,
                             2,
                             item);
    ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1,
                             3,
                             new QTableWidgetItem(fileInfo.lastModified().toString()));
    //ui->tableWidget->resizeColumnsToContents();
    //ui->tableWidget->resizeRowsToContents();
}

void Dialog::on_pushButton_clicked()
{
    QString dirname = QFileDialog::getExistingDirectory(this, tr("Select a Directory"), QDir::currentPath());
    ui->lineEdit->setText(dirname);
}

void Dialog::on_pushButton_2_clicked()
{
    QDirIterator it(ui->lineEdit->text(), QDir::Files, QDirIterator::Subdirectories);
    while (it.hasNext()) {
        fileStack->push(it.next());
    }
    QMessageBox msgBox;
    msgBox.setText("Completed");
    msgBox.exec();
    mThread->dir = fileStack;
    mThread->start();
}

0 个答案:

没有答案