在我的 Qt 程序中,我基本上选择一个文件夹,查看内容,然后获取索引列表(0、1、2、3、........)。它显示了正确的文件(左),但转换的次数超出了预期。这是错误和代码的图像:
主窗口.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
主窗口.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();
}
主要.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}