如何在Qt中获取文件名

How to get file name in Qt?

本文关键字:获取 文件名 Qt      更新时间:2023-10-16

我修改了从Qt教程中获得的文本查找器示例,并制作了一个文本查看器。在此程序中,用户键入文件的地址并单击"搜索"按钮。然后,程序将显示文本文件的内容。 下面是我的代码。text_finder.cpp:

#include "text_finder.h"
#include "ui_text_finder.h"
#include <QHBoxLayout>
#include <QFile>
#include <QTextStream>
#include <QFileDialog>
Text_Finder::Text_Finder(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Text_Finder)
{
    ui->setupUi(this);
}
Text_Finder::~Text_Finder()
{
    delete ui;
}
void Text_Finder::loadFile(QFile file){ // I have to pass the file name as parameter.
    QFile inputFile(file);
    inputFile.open(QIODevice::ReadOnly);
    QTextStream in(&inputFile);
    QString line = in.readAll();
    inputFile.close();
    ui->read->setText(line);
    QTextCursor cursor = ui->read->textCursor();
    cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
}
void Text_Finder::on_search_clicked()
{
//  Code that gets the path from the text box.
    loadFile();//Parameters not passed yet.
}   

我还没有输入从文本框地址获取文件名的代码。我必须将文件传递给 loadFile(( 函数,该函数会将内容输入到程序中心的文本编辑中。我想要一个解决方案来获取用户输入的文件的名称。例如,用户可以输入"/home/user/input.txt"。程序应该获取该文件的内容并将其转发到 loadFile((。需要解释各个部件如何工作的解决方案。我在Ubuntu 15.04(测试版(上使用Qt Creator。

如果我理解正确,用户在文本框中键入文件的完整路径或地址,并且您希望从用户输入的完整路径中获取文件名。

编辑:我意识到使用"QFileDialog"是获取文件名的理想方式。所以这个是我重新设计整个代码的方式;

text_finder.h

#ifndef TEXT_FINDER_H
#define TEXT_FINDER_H
#include <QDialog>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QTextEdit>
#include <QFile>
#include <QTextStream>
#include <QFileDialog>
class Text_Finder : public QWidget{
    Q_OBJECT
public:
    Text_Finder(QWidget *parent = 0);
    ~Text_Finder();
public slots:
    void on_search_clicked();
    void open();
    //void loadFile(QString const &filename);
private:
    void loadFile(QString const &filename);
    QLineEdit *txtFileName;
    QTextEdit *txtFileContents;
    QString fileName;
    QPushButton *search;
    QPushButton *openFile;
};
#endif

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
};
#endif // MAINWINDOW_H

text_finder.cpp

#include "text_finder.h"
Text_Finder::Text_Finder(QWidget *parent) : QWidget(parent) {
    openFile = new QPushButton("Open File");
    connect(openFile, SIGNAL(clicked()), this, SLOT(open()));
    txtFileName = new QLineEdit;
    search = new QPushButton("&Search");
    txtFileContents = new QTextEdit;
    QHBoxLayout *dialogAndViewLayout = new QHBoxLayout;
    dialogAndViewLayout->addWidget(openFile);
    dialogAndViewLayout->addWidget(txtFileName);
    dialogAndViewLayout->addStretch();
    dialogAndViewLayout->addWidget(search);
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addLayout(dialogAndViewLayout);
    layout->addWidget(txtFileContents);
    connect(search, SIGNAL(clicked()), this, SLOT(on_search_clicked()));
    setLayout(layout);
}
Text_Finder::~Text_Finder(){
    delete txtFileName;
    delete txtFileContents;
    delete search;
    delete openFile;
}
void Text_Finder::loadFile(QString const &filename){
    QFile inputFile(filename);
    inputFile.open(QIODevice::ReadWrite | QIODevice::Text);
    QTextStream textStream(&inputFile);
    QString contents = textStream.readAll();
    inputFile.close();
    txtFileContents->setPlainText(contents);
}
void Text_Finder::on_search_clicked() {
    loadFile(fileName);
}
/*this slot opens a file dialog. After the file has been selected, it sets     
  the file to the text text edit box*/
void Text_Finder::open() {
    fileName = QFileDialog::getOpenFileName(this, "Open text", "/home/",     
    "");
    txtFileName->setText(fileName);
}

主窗口.cpp

#include "mainwindow.h"
#include "text_finder.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
    Text_Finder *textFinder = new Text_Finder;
    setCentralWidget(textFinder);
}
MainWindow::~MainWindow() {
}

最后主.cpp

#include "text_finder.h"
#include <QApplication>
int main(int argc, char **argv){
    QApplication app(argc, argv);
    Text_Finder *window = new Text_Finder;
    window->show();
    return app.exec();
}

除非你要做一些程序化编辑,否则你根本不需要使用QTextCursor

我建议您在尝试继续之前先浏览一下参考手册,因为很明显您不熟悉正在使用的图形小部件的基本知识。

如果您只读取文件名,然后以纯文本格式显示内容,这就是您需要做的。 (我假设您的文件名输入到QLineEdit小部件中,并且ui->read是一个QTextEdit小部件(

void Text_Finder::loadFile(QString const &filename){ // I have to pass the file name as parameter.
    QFile inputFile(filename);
    inputFile.open(QIODevice::ReadOnly);
    QTextStream in(&inputFile);
    QString contents = in.readAll();
    inputFile.close();
    ui->read->setPlainText(contents);
}
void Text_Finder::on_search_clicked()
{
    QString filename = ui->filename->text();
    loadFile(filename);
}

编辑:我已经为您的代码创建了一个功能齐全的重制版,并结合了我建议的更改。我已经编译并测试了它,它正在按照您的描述工作。

注意:在没有 UI 文件的情况下,我手动创建了用户界面。

text_finder.h

#ifndef TEXT_FINDER_H
#define TEXT_FINDER_H
#include <QMainWindow>
class QLineEdit;
class QTextEdit;
class Text_Finder : public QMainWindow{
    Q_OBJECT
public:
    Text_Finder(QWidget *parent = 0);
    ~Text_Finder();
public slots:
    void on_search_clicked();
private:
    void loadFile(QString const &filename);
    QLineEdit *txtFileName;
    QTextEdit *txtFileContents;
};
#endif // TEXT_FINDER_H

主.cpp

#include "text_finder.h"
#include <QApplication>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QTextEdit>
#include <QFile>
#include <QTextStream>
Text_Finder::Text_Finder(QWidget *parent) :
    QMainWindow(parent)
{
    QWidget *ui = new QWidget(this);
    QHBoxLayout *hLayout = new QHBoxLayout;
    txtFileName = new QLineEdit(this);
    QPushButton *loadButton = new QPushButton("Load File", this);
    connect(loadButton, &QPushButton::clicked, this, &Text_Finder::on_search_clicked);
    hLayout->addWidget(txtFileName);
    hLayout->addWidget(loadButton);
    QVBoxLayout *vLayout = new QVBoxLayout(this);
    vLayout->addLayout(hLayout);
    txtFileContents = new QTextEdit(this);
    vLayout->addWidget(txtFileContents);
    ui->setLayout(vLayout);
    setCentralWidget(ui);
}
Text_Finder::~Text_Finder(){
    // It's not necessary to explicitly delete any widgets.
    // QObject implements the Composite Pattern, which takes care of all this automatically
}
void Text_Finder::loadFile(QString const &filename){ 
    QFile inputFile(filename);
    inputFile.open(QIODevice::ReadOnly);
    QTextStream in(&inputFile);
    QString contents = in.readAll();
    inputFile.close();
    txtFileContents->setPlainText(contents);
}
void Text_Finder::on_search_clicked()
{
    QString filename = txtFileName->text();
    loadFile(filename);
}  
int main(int argc, char **argv){
    QApplication app(argc, argv);
    Text_Finder w;
    w.show();
    return app.exec();
}

未来问题的提示:如果您在问题中包含 SSCCE,您将更快地获得更好的答案。我在此编辑中包含的内容是一个合适的示例。