gpt4 book ai didi

c++ - QDirIterator - 没有 hasNext 不是 FilePath react

转载 作者:太空宇宙 更新时间:2023-11-04 12:44:17 25 4
gpt4 key购买 nike

我想要做的是它给我一个列表,其中包含目录和子目录中的所有 .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-> <-(correct path i but i as input)"

调试输出中的输出是:

" ' ' , This, show in the Output"

代码更新了,问题依旧。

最佳答案

例子

下面是一个如何使槽工作的例子:

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/

关于c++ - QDirIterator - 没有 hasNext 不是 FilePath react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52495337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com