我创建了一个应用程序,其中目录中的文件显示在 qlistwidget 中。
现在的问题是我希望这个列表显示为单选按钮列表,以便用户可以选择单选按钮并保存所选文件的地址。如果用户没有选择单选按钮并单击下一步,则还需要显示错误。
我正在使用 Visual Studio 来编写 gui 而不是 qt creator。
到目前为止,我的代码是:
.hpp 文件
#pragma once
#include <QWidget>
#include "ui_secondform.h"
#include "thirdform.hpp"
#include <QRegExp>
#include <QDir>
#include <QDebug>
class SecondForm : public QWidget {
Q_OBJECT
public:
SecondForm(QWidget * parent = Q_NULLPTR);
~SecondForm();
QString processText();
signals:
void firstform();
public slots:
void on_pushButton_next2_clicked();
void on_pushButton_back2_clicked();
void on_lineEdit_textChanged(const QString &arg1);
//void onNewTextEntered(const QString &text);
//void on_lineEdit_textChanged(const QString &arg1);
private:
Ui::SecondForm ui;
ThirdForm *third;
QStringList fileList;
};
.cpp 文件:
#include "secondform.hpp"
#include "firstform.h"
SecondForm::SecondForm(QWidget * parent) : QWidget(parent) {
ui.setupUi(this);
third = new ThirdForm();
// connected to the slot start the main window on the button in the second window
connect(third, &ThirdForm::secondform, this, &SecondForm::show);
//QString dir = processText();
QDir testPath("D://");
testPath.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
fileList = testPath.entryList();
ui.listWidget->addItems(fileList);
}
SecondForm::~SecondForm() {
}
void SecondForm::on_pushButton_back2_clicked() {
this->hide();
emit firstform();
}
void SecondForm::on_pushButton_next2_clicked() {
this->close();
third->show();
}
void SecondForm::on_lineEdit_textChanged(const QString &arg1) {
QRegExp regExp(arg1, Qt::CaseInsensitive, QRegExp::Wildcard);
ui.listWidget->clear();
ui.listWidget->addItems(fileList.filter(regExp));
}
QString SecondForm::processText()
{
FirstForm first;
const QString dir = first.lineEdit()->text();
return dir;
// do something with the text
}
输出: