如何在Qt中获取文件名?

时间:2023-01-26 16:16:49

I have modified the Text Finder example which I got from a Qt Tutorial and made a Text Viewer. In this program, the user types in the address of the file and clicks the Search button. The program then displays the content of the text file. Below is my code. text_finder.cpp:

我已经修改了我从Qt教程得到的文本查找器示例,并创建了一个文本查看器。在此程序中,用户在文件的地址中键入并单击Search按钮。然后程序将显示文本文件的内容。下面是我的代码。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.
}   

I have not yet entered the code which gets the name of the file from the address of the text box. I will have to pass the file to the loadFile() function which will enter the contents into the Text Edit in the center of the program. I want a solution to get the name of the file of which the user enters. For example, the user might enter, "/home/user/input.txt". The program should get the contents of that file and forward it to loadFile(). An solution with an explanation on how the various parts work is needed. I am using Qt Creator on Ubuntu 15.04 (Beta).

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

2 个解决方案

#1


1  

If i understand you correctly, the user types the full path or address of the file in the text box and you want to get just the name of the file out of the full path the user entered.

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

EDIT: I realized using 'QFileDialog' was the ideal way to get the file name. So this is how i redesigned the whole code;

编辑:我意识到使用“QFileDialog”是获取文件名的理想方法。这就是我重新设计整个代码的方法;

text_finder.h

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

mainwindow.h

mainwindow.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

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);
}

mainwindow.cpp

mainwindow.cpp

#include "mainwindow.h"
#include "text_finder.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
    Text_Finder *textFinder = new Text_Finder;

    setCentralWidget(textFinder);
}

MainWindow::~MainWindow() {

}

Finally main.cpp

最后main.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();
}

#2


0  

Unless you're going to do some programmatic editing, you do not need to use QTextCursor at all.

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

I suggest you have a browse through the reference manual before trying to carry on since it's clear that you are not familiar with the bare-basics of the graphical widgets you're using.

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

If you're only reading in a file name and then displaying the contents in plain text format, this is all you need to do. (I'm assuming your filename is entered into a QLineEdit widget and ui->read is a QTextEdit widget)

如果您只读取文件名,然后以纯文本格式显示内容,那么这就是您所需要做的。(我假设您的文件名被输入到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);
}

EDIT: I've created a fully-functional remake of your code, incorporating the changes I suggested. I have compiled and tested it and it is working as per your description.

编辑:我已经创建了你的代码的完全功能的重拍,包含了我建议的修改。我已经编译并测试了它,它按照您的描述工作。

Note: In the absence of your UI file, I've created the user interface manually.

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

text_finder.h

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

main.cpp

main.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();
}

HINT for future questions: You will get better answers quicker if you include an SSCCE in your question. What I've included in this edit is a suitable example.

暗示未来的问题:如果你在你的问题中包含了一个SSCCE,你将会得到更好的答案。我在这个编辑中包含的是一个合适的例子。

#1


1  

If i understand you correctly, the user types the full path or address of the file in the text box and you want to get just the name of the file out of the full path the user entered.

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

EDIT: I realized using 'QFileDialog' was the ideal way to get the file name. So this is how i redesigned the whole code;

编辑:我意识到使用“QFileDialog”是获取文件名的理想方法。这就是我重新设计整个代码的方法;

text_finder.h

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

mainwindow.h

mainwindow.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

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);
}

mainwindow.cpp

mainwindow.cpp

#include "mainwindow.h"
#include "text_finder.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
    Text_Finder *textFinder = new Text_Finder;

    setCentralWidget(textFinder);
}

MainWindow::~MainWindow() {

}

Finally main.cpp

最后main.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();
}

#2


0  

Unless you're going to do some programmatic editing, you do not need to use QTextCursor at all.

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

I suggest you have a browse through the reference manual before trying to carry on since it's clear that you are not familiar with the bare-basics of the graphical widgets you're using.

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

If you're only reading in a file name and then displaying the contents in plain text format, this is all you need to do. (I'm assuming your filename is entered into a QLineEdit widget and ui->read is a QTextEdit widget)

如果您只读取文件名,然后以纯文本格式显示内容,那么这就是您所需要做的。(我假设您的文件名被输入到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);
}

EDIT: I've created a fully-functional remake of your code, incorporating the changes I suggested. I have compiled and tested it and it is working as per your description.

编辑:我已经创建了你的代码的完全功能的重拍,包含了我建议的修改。我已经编译并测试了它,它按照您的描述工作。

Note: In the absence of your UI file, I've created the user interface manually.

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

text_finder.h

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

main.cpp

main.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();
}

HINT for future questions: You will get better answers quicker if you include an SSCCE in your question. What I've included in this edit is a suitable example.

暗示未来的问题:如果你在你的问题中包含了一个SSCCE,你将会得到更好的答案。我在这个编辑中包含的是一个合适的例子。