QDirIterator-没有hasNext不是FilePath反应

时间:2018-09-25 09:53:45

标签: c++ qt iterator

我想做的是给我一个目录和子目录中所有.cpp文件的列表。问题 it.HasNext 保持为false, it.filePath 保持为空。

main.cpp

#include "QtSignal_Slot.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QtSignal_Slot w;
    w.show();
    return a.exec();
}

ui_QtSignal_Slot.h

#ifndef UI_QTSIGNAL_SLOT_H
#define UI_QTSIGNAL_SLOT_H

#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QSpacerItem>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QTextEdit>
#include <QtWidgets/QToolBar>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_QtSignal_SlotClass
{
public:
    QWidget *centralWidget;
    QGridLayout *gridLayout;
    QPushButton *ButtonStart;
    QSpacerItem *horizontalSpacer_2;
    QSpacerItem *horizontalSpacer_4;
    QTextEdit *textEdit;
    QSpacerItem *verticalSpacer_2;
    QSpacerItem *verticalSpacer_3;
    QSpacerItem *horizontalSpacer;
    QSpacerItem *horizontalSpacer_3;
    QLineEdit *PathEdit;
    QMenuBar *menuBar;
    QToolBar *mainToolBar;
    QStatusBar *statusBar;

    void setupUi(QMainWindow *QtSignal_SlotClass)
    {
        if (QtSignal_SlotClass->objectName().isEmpty())
            QtSignal_SlotClass->setObjectName(QStringLiteral("QtSignal_SlotClass"));
        QtSignal_SlotClass->setEnabled(true);
        QtSignal_SlotClass->resize(380, 250);
        QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        sizePolicy.setHorizontalStretch(0);
        sizePolicy.setVerticalStretch(0);
        sizePolicy.setHeightForWidth(QtSignal_SlotClass->sizePolicy().hasHeightForWidth());
        QtSignal_SlotClass->setSizePolicy(sizePolicy);
        QtSignal_SlotClass->setMinimumSize(QSize(380, 250));
        QtSignal_SlotClass->setMaximumSize(QSize(700, 300));
        QtSignal_SlotClass->setSizeIncrement(QSize(400, 250));
        QtSignal_SlotClass->setBaseSize(QSize(200, 250));
        centralWidget = new QWidget(QtSignal_SlotClass);
        centralWidget->setObjectName(QStringLiteral("centralWidget"));
        gridLayout = new QGridLayout(centralWidget);
        gridLayout->setSpacing(6);
        gridLayout->setContentsMargins(11, 11, 11, 11);
        gridLayout->setObjectName(QStringLiteral("gridLayout"));
        ButtonStart = new QPushButton(centralWidget);
        ButtonStart->setObjectName(QStringLiteral("ButtonStart"));
        ButtonStart->setStyleSheet(QStringLiteral("background-color: rgb(200,200,255); color: rgb(0,0,0);"));

        gridLayout->addWidget(ButtonStart, 7, 1, 1, 1);

        horizontalSpacer_2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

        gridLayout->addItem(horizontalSpacer_2, 7, 2, 1, 1);

        horizontalSpacer_4 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

        gridLayout->addItem(horizontalSpacer_4, 0, 2, 1, 1);

        textEdit = new QTextEdit(centralWidget);
        textEdit->setObjectName(QStringLiteral("textEdit"));
        textEdit->setStyleSheet(QStringLiteral("color: rgb(0,0,0);"));

        gridLayout->addWidget(textEdit, 3, 1, 1, 1);

        verticalSpacer_2 = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);

        gridLayout->addItem(verticalSpacer_2, 1, 1, 1, 1);

        verticalSpacer_3 = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);

        gridLayout->addItem(verticalSpacer_3, 2, 1, 1, 1);

        horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

        gridLayout->addItem(horizontalSpacer, 7, 0, 1, 1);

        horizontalSpacer_3 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

        gridLayout->addItem(horizontalSpacer_3, 0, 0, 1, 1);

        PathEdit = new QLineEdit(centralWidget);
        PathEdit->setObjectName(QStringLiteral("PathEdit"));
        QSizePolicy sizePolicy1(QSizePolicy::Expanding, QSizePolicy::Preferred);
        sizePolicy1.setHorizontalStretch(0);
        sizePolicy1.setVerticalStretch(0);
        sizePolicy1.setHeightForWidth(PathEdit->sizePolicy().hasHeightForWidth());
        PathEdit->setSizePolicy(sizePolicy1);
        PathEdit->setMinimumSize(QSize(200, 30));
        PathEdit->setStyleSheet(QStringLiteral("background-color: rgb(240,240,240); color: rgb(0,0,0);"));

        gridLayout->addWidget(PathEdit, 0, 1, 1, 1);

        QtSignal_SlotClass->setCentralWidget(centralWidget);
        menuBar = new QMenuBar(QtSignal_SlotClass);
        menuBar->setObjectName(QStringLiteral("menuBar"));
        menuBar->setGeometry(QRect(0, 0, 380, 21));
        QtSignal_SlotClass->setMenuBar(menuBar);
        mainToolBar = new QToolBar(QtSignal_SlotClass);
        mainToolBar->setObjectName(QStringLiteral("mainToolBar"));
        QtSignal_SlotClass->addToolBar(Qt::TopToolBarArea, mainToolBar);
        statusBar = new QStatusBar(QtSignal_SlotClass);
        statusBar->setObjectName(QStringLiteral("statusBar"));
        QtSignal_SlotClass->setStatusBar(statusBar);

        retranslateUi(QtSignal_SlotClass);

        QMetaObject::connectSlotsByName(QtSignal_SlotClass);
    } // setupUi

    void retranslateUi(QMainWindow *QtSignal_SlotClass)
    {
        QtSignal_SlotClass->setWindowTitle(QApplication::translate("QtSignal_SlotClass", "QtSignal_Slot", Q_NULLPTR));
        ButtonStart->setText(QApplication::translate("QtSignal_SlotClass", "Generate graphicfile", Q_NULLPTR));
        PathEdit->setText(QString());
    } // retranslateUi

};

namespace Ui {
    class QtSignal_SlotClass: public Ui_QtSignal_SlotClass {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_QTSIGNAL_SLOT_H

QtSignal_Slot.h

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_QtSignal_Slot.h"

class QtSignal_Slot : public QMainWindow
{
    Q_OBJECT

public:
    QString m_Path;

    QtSignal_Slot(QWidget *parent = Q_NULLPTR);

public slots:
    void m_StartButtonpressed();

private:
    Ui::QtSignal_SlotClass ui;
};

QtSolts_Signel.cpp

#include "QtSignal_Slot.h"
#include <ui_QtSignal_Slot.h>
#include <QDirIterator>
#include <QDebug>

QtSignal_Slot::QtSignal_Slot(QWidget *parent) : QMainWindow(nullptr)
{
    ui.setupUi(this);
    connect(ui.ButtonStart, SIGNAL(clicked()), this, SLOT(m_StartButtonpressed()));
}

void QtSignal_Slot::m_StartButton()
{
    m_Path = (ui.PathEdit->text());

    QDirIterator it(m_Path, QStringList() << "*.cpp" , QDir::Files, QDirIterator::Subdirectories);

    do
    {
        qDebug() << it.next();

    } while (it.hasNext());

    qDebug() << "This, show in the Output";
    QString Buffer = QString("%1").arg(QString::number(it.hasNext()));
    ui.textEdit->setText(Buffer +"->" + it.filePath() + "<-" + m_Path);
}

textEdit中的输出为:

  

“ 0-> <-(正确的路径i,但我作为输入)”

,并且在debug-output中的输出是:

  

“'',这将显示在输出中”

代码已更新,但问题仍然存在。

1 个答案:

答案 0 :(得分:0)

示例

以下是如何使插槽正常​​工作的示例:

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QLineEdit;

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = nullptr);

private:
    QLineEdit *m_pathEdit;
    QLineEdit *m_textEdit;

private slots:
    void onButtonClicked();
};

#endif // MAINWINDOW_H

MainWindow.cpp

#include "MainWindow.h"
#include <QDirIterator>
#include <QBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    m_pathEdit(new QLineEdit(this)),
    m_textEdit(new QLineEdit(this))
{
    auto *widget = new QWidget(this);
    auto *layoutMain = new QVBoxLayout(widget);
    auto *button = new QPushButton(tr("Click me"), this);

    layoutMain->addWidget(m_pathEdit);
    layoutMain->addWidget(m_textEdit);
    layoutMain->addWidget(button);

    setCentralWidget(widget);

    connect(button, &QPushButton::clicked, this, &MainWindow::onButtonClicked);
}

void MainWindow::onButtonClicked()
{
    const QString &path(m_pathEdit->text());
    QDirIterator it(path, QStringList() << "*.cpp" , QDir::Files, QDirIterator::Subdirectories);

    do
    {
        qDebug() << it.next();

    } while (it.hasNext());

    qDebug() << "This, show in the Output";

    QString buffer(QString("%1").arg(QString::number(it.hasNext())));

    m_textEdit->setText(buffer +"->" + it.filePath() + "<-" + path);
}

结果

对于以下输入(在m_pathEdit中输入,上面的QLineEdit):

  

/ Users / Michael Scopchanov / Desktop / untitled9 /

控制台中的结果是:

"/Users/Michael Scopchanov/Desktop/untitled9/main.cpp"
"/Users/Michael Scopchanov/Desktop/untitled9/MainWindow.cpp"
"/Users/Michael Scopchanov/Desktop/untitled9/MyLabel.cpp"
This, show in the Output

m_textEdit的文本设置为:

  

0-> / Users / Michael Scopchanov / Desktop / untitled9 / MyLabel.cpp <-/ Users / Michael Scopchanov / Desktop / untitled9 /