QDir 提供了正确的文件,但显示的数量不正确
QDir is giving correct files, but shows inncorrect amount
在我的Qt程序中,我基本上选择一个文件夹,查看内容,然后得到索引列表(0, 1, 2, 3, .......)。它显示了正确的文件(左),但转换的文件超出了预期。这是错误和代码的图片:
MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileDialog>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
void check();
void verify();
void update();
void makeList();
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_toolButton_clicked();
void on_pushButton_clicked();
void on_checkBoxDateOrdered_clicked()
{
check();
}
void on_checkBoxInverted_clicked()
{
check();
}
void on_lineEdit_textChanged(const QString &arg1);
void on_pushButtonClear_clicked();
void on_listWidget_currentTextChanged()
{
update();
}
void on_listWidget_2_currentTextChanged()
{
update();
}
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
QString path;
QStringList before;
QString suffix;
QDir folder;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->checkBoxDateOrdered->setEnabled(false);
ui->checkBoxInverted->setEnabled(false);
ui->pushButton->setEnabled(false);
ui->pushButtonClear->setEnabled(false);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::check()
{
ui->listWidget->clear();
if (ui->checkBoxDateOrdered->isChecked() && !ui->checkBoxInverted->isChecked())
{
before = folder.entryList(QDir::Files, QDir::Time);
}
else if (ui->checkBoxInverted->isChecked() && !ui->checkBoxDateOrdered->isChecked())
{
before = folder.entryList(QDir::Files, QDir::Reversed);
}
else if (ui->checkBoxInverted->isChecked() && ui->checkBoxDateOrdered->isChecked())
{
before = folder.entryList(QDir::Files, QDir::Time | QDir::Reversed);
}
else
{
before = folder.entryList(QDir::Files);
}
ui->listWidget->addItems(before);
}
void MainWindow::verify()
{
if (ui->listWidget->count() == 0 && ui->listWidget_2->count() > 0)
{
ui->listWidget_2->clear();
}
else if (ui->listWidget->count() > 0 && ui->listWidget_2->count() == 0)
{
ui->listWidget->clear();
}
else if (ui->listWidget->count() > 0 && ui->listWidget->count() > 0)
{
ui->listWidget->clear();
ui->listWidget_2->clear();
}
}
void MainWindow::update()
{
if (ui->listWidget->count() == 0 && ui->listWidget_2->count() > 0)
{
ui->pushButtonClear->setEnabled(true);
}
else if (ui->listWidget->count() > 0 && ui->listWidget_2->count() == 0)
{
ui->pushButtonClear->setEnabled(true);
}
else if (ui->listWidget->count() > 0 && ui->listWidget->count() > 0)
{
ui->pushButtonClear->setEnabled(true);
}
else
{
ui->pushButtonClear->setEnabled(false);
}
}
void MainWindow::makeList()
{
ui->listWidget->clear();
ui->listWidget_2->clear();
ui->lineEdit->setText(path);
folder.setPath(path);
check();
QString temp = before.at(0);
QStringList pieces = temp.split(".");
suffix = pieces.value(pieces.length() - 1);
int amount = folder.count();
QStringList after;
for (int x = 0; x < amount; x++)
{
after << QString::number(x) + "." + suffix;
}
ui->listWidget_2->addItems(after);
}
void MainWindow::on_toolButton_clicked()
{
path = QFileDialog::getExistingDirectory(this, tr("Open Directory"), "/home", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (path != "")
{
makeList();
}
}
void MainWindow::on_pushButton_clicked()
{
for (int x = 0; x < before.count(); x++)
{
int i = x;
folder.rename(before.at(x), QString::number(i++) + "." + suffix);
}
}
void MainWindow::on_lineEdit_textChanged(const QString &arg1)
{
if (arg1 == "")
{
ui->checkBoxDateOrdered->setEnabled(false);
ui->checkBoxInverted->setEnabled(false);
ui->pushButton->setEnabled(false);
ui->pushButtonClear->setEnabled(false);
}
else if (arg1 != "")
{
ui->checkBoxDateOrdered->setEnabled(true);
ui->checkBoxInverted->setEnabled(true);
ui->pushButton->setEnabled(true);
ui->pushButtonClear->setEnabled(true);
}
}
void MainWindow::on_pushButtonClear_clicked()
{
ui->lineEdit->clear();
verify();
}
Main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
目录里好像有3个non-files。您只对文件感兴趣,但 QDir::count
参考状态:
Returns the total number of directories and files in the directory
Equivalent to entryList().count()
.
注意参数是默认的,这就是 count
returns 更多的原因。 before
包含文件列表,那么为什么不使用它的 count
/size
/length
?
int amount = before.size();
在我的Qt程序中,我基本上选择一个文件夹,查看内容,然后得到索引列表(0, 1, 2, 3, .......)。它显示了正确的文件(左),但转换的文件超出了预期。这是错误和代码的图片:
MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileDialog>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
void check();
void verify();
void update();
void makeList();
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_toolButton_clicked();
void on_pushButton_clicked();
void on_checkBoxDateOrdered_clicked()
{
check();
}
void on_checkBoxInverted_clicked()
{
check();
}
void on_lineEdit_textChanged(const QString &arg1);
void on_pushButtonClear_clicked();
void on_listWidget_currentTextChanged()
{
update();
}
void on_listWidget_2_currentTextChanged()
{
update();
}
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
QString path;
QStringList before;
QString suffix;
QDir folder;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->checkBoxDateOrdered->setEnabled(false);
ui->checkBoxInverted->setEnabled(false);
ui->pushButton->setEnabled(false);
ui->pushButtonClear->setEnabled(false);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::check()
{
ui->listWidget->clear();
if (ui->checkBoxDateOrdered->isChecked() && !ui->checkBoxInverted->isChecked())
{
before = folder.entryList(QDir::Files, QDir::Time);
}
else if (ui->checkBoxInverted->isChecked() && !ui->checkBoxDateOrdered->isChecked())
{
before = folder.entryList(QDir::Files, QDir::Reversed);
}
else if (ui->checkBoxInverted->isChecked() && ui->checkBoxDateOrdered->isChecked())
{
before = folder.entryList(QDir::Files, QDir::Time | QDir::Reversed);
}
else
{
before = folder.entryList(QDir::Files);
}
ui->listWidget->addItems(before);
}
void MainWindow::verify()
{
if (ui->listWidget->count() == 0 && ui->listWidget_2->count() > 0)
{
ui->listWidget_2->clear();
}
else if (ui->listWidget->count() > 0 && ui->listWidget_2->count() == 0)
{
ui->listWidget->clear();
}
else if (ui->listWidget->count() > 0 && ui->listWidget->count() > 0)
{
ui->listWidget->clear();
ui->listWidget_2->clear();
}
}
void MainWindow::update()
{
if (ui->listWidget->count() == 0 && ui->listWidget_2->count() > 0)
{
ui->pushButtonClear->setEnabled(true);
}
else if (ui->listWidget->count() > 0 && ui->listWidget_2->count() == 0)
{
ui->pushButtonClear->setEnabled(true);
}
else if (ui->listWidget->count() > 0 && ui->listWidget->count() > 0)
{
ui->pushButtonClear->setEnabled(true);
}
else
{
ui->pushButtonClear->setEnabled(false);
}
}
void MainWindow::makeList()
{
ui->listWidget->clear();
ui->listWidget_2->clear();
ui->lineEdit->setText(path);
folder.setPath(path);
check();
QString temp = before.at(0);
QStringList pieces = temp.split(".");
suffix = pieces.value(pieces.length() - 1);
int amount = folder.count();
QStringList after;
for (int x = 0; x < amount; x++)
{
after << QString::number(x) + "." + suffix;
}
ui->listWidget_2->addItems(after);
}
void MainWindow::on_toolButton_clicked()
{
path = QFileDialog::getExistingDirectory(this, tr("Open Directory"), "/home", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (path != "")
{
makeList();
}
}
void MainWindow::on_pushButton_clicked()
{
for (int x = 0; x < before.count(); x++)
{
int i = x;
folder.rename(before.at(x), QString::number(i++) + "." + suffix);
}
}
void MainWindow::on_lineEdit_textChanged(const QString &arg1)
{
if (arg1 == "")
{
ui->checkBoxDateOrdered->setEnabled(false);
ui->checkBoxInverted->setEnabled(false);
ui->pushButton->setEnabled(false);
ui->pushButtonClear->setEnabled(false);
}
else if (arg1 != "")
{
ui->checkBoxDateOrdered->setEnabled(true);
ui->checkBoxInverted->setEnabled(true);
ui->pushButton->setEnabled(true);
ui->pushButtonClear->setEnabled(true);
}
}
void MainWindow::on_pushButtonClear_clicked()
{
ui->lineEdit->clear();
verify();
}
Main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
目录里好像有3个non-files。您只对文件感兴趣,但 QDir::count
参考状态:
Returns the total number of directories and files in the directory
Equivalent to
entryList().count()
.
注意参数是默认的,这就是 count
returns 更多的原因。 before
包含文件列表,那么为什么不使用它的 count
/size
/length
?
int amount = before.size();