如何在Qt中获取文件名?

时间:2015-03-29 07:18:14

标签: c++ qt qt-creator

我修改了我从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()函数,该函数将把内容输入到程序中心的文本编辑中。我想要一个解决方案来获取用户输入的文件的名称。例如,用户可以输入&#34; /home/user/input.txt"。程序应该获取该文件的内容并将其转发到loadFile()。需要一个解释各部分如何工作的解决方案。我在Ubuntu 15.04(Beta)上使用Qt Creator。

2 个答案:

答案 0 :(得分:1)

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

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

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

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

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() {

}

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

答案 1 :(得分:0)

除非您要进行某些程序化编辑,否则根本不需要使用QTextCursor

我建议您在尝试继续之前浏览参考手册,因为它很清楚您不熟悉您正在使用的图形小部件的基础知识。

如果您只读取文件名,然后以纯文本格式显示内容,则只需执行此操作即可。 (我假设您的文件名已输入QLineEdit小部件而ui->readQTextEdit小部件)

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

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

未来问题的提示:如果在问题中加入SSCCE,您会得到更快的答案。我在此编辑中包含的内容就是一个合适的例子。