[英]Why does my treeview in GUI with QFileSystemModel sometimes freezing when I copying file in separate thread (Qt)?
[英]Qt 5 running file from treeView and QFileSystemModel
問題是我最近才開始c ++編程。 我的問題如下:
如何使在mainwindow / treeview中查看的文件運行?
要查看的文檔是帶有靜態路徑的純文本文檔。 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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.