从treeView和QFileSystemModel运行Qt5文件
Qt 5 running file from treeView and QFileSystemModel
问题是我最近才开始c++编程。我的问题如下:
如何使在主窗口/树视图中查看的文件运行?
要查看的文档是具有静态路径的纯文本文档。sPath是文件所在目录的路径。
下面是我的"mainwindow.cpp"文件。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDirModel"
#include "QTreeView"
#include "QFileSystemModel"
#include "QtGui"
#include "QtCore"
#include "QDir"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString sPath ="/home/simon/QT Projects/Bra_Programmering/utlatanden/";
filemodel = new QFileSystemModel(this);
filemodel->setFilter(QDir::Files | QDir::NoDotAndDotDot);
filemodel->setNameFilterDisables(false);
filemodel->setRootPath(sPath);
ui->treeView->setModel(filemodel);
ui->treeView->setRootIndex(filemodel->setRootPath(sPath));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_treeView_doubleClicked(const QModelIndex &index)
{
};
下面是我的"mainwindow.h"文件。
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <mainwindow.h>
#include <QtCore>
#include <QtGui>
#include <QDirModel>
#include <QFileSystemModel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_treeView_doubleClicked(const QModelIndex &index);
private:
Ui::MainWindow *ui;
QFileSystemModel *filemodel;
};
#endif // MAINWINDOW_H
如果您想用默认文本查看器打开文件:
void MainWindow::on_treeView_doubleClicked(const QModelIndex &index)
{
QDesktopServices::openUrl(QUrl::fromLocalFile(filemodel->filePath(index)));
}
或者,如果你想通过你的Qt应用程序打开文本文件,它应该是:
void MainWindow::on_treeView_doubleClicked(const QModelIndex &index)
{
QFile file(filemodel->filePath(index));
if(file.open(QFile::ReadOnly | QFile::Text))
{
QTextStream in(&file);
QString text = in.readAll();
// Do something with the text
file.close();
}
}
相关文章:
- Qt5 复制(复制)可执行文件
- 如何使用QJSONARRAY在QT5(C )中解析JSON文件(数组)
- 在 Windows 10 上找不到 ./****.h 文件 qt5,C++
- 将 .ui 文件导入 Qt5 C++时出错"was not declared in this scope"
- 将文件拖放到 Qt5 应用程序外部
- 如果文件中不存在qt5中的文件,如何原子创建该文件
- QT5.2资源文件
- 在任何平台上执行任何Qt5应用程序的重要dll文件是什么(运行时文件dll)
- qt5设计器,使用fileopen,在lineedit中显示文件路径,这样做是否有问题
- 如何在Qt5中的特定条目打开/关闭文件
- 从treeView和QFileSystemModel运行Qt5文件
- Qt5:如何在本地文件系统中读取/写入文件
- Qt5 Designer Ui文件中的QSystemTrayIcon
- 无法加载Qt5/cmake项目中Qt资源文件中指定的图像
- CMake & QT5 - QT5_WRAP_UI不生成 UI 头文件
- 在Qt5/C++中向后查找文本文件(尾随文件)
- ui_*文件不是由QT5.5生成的
- Qt5到Qt4 UI文件兼容性
- 如何在qt5中使用QSound播放.wav文件
- 使用CMakeLists.txt设置Qt5 -文件未发现错误