使用QT5实现简单的资源管理器(附完整源码)

1. 最终效果图

qt_simple_brw2

2. 新建QT Widget工程

qt_project

3. 知识要点

qt_browser_frame

 

  • 对于QMainWindow类不能直接使用setLayout(layout);进行布局,因为QMainWindows已经具有了布局,直接设置布局会提示出错信息,需要在Windows中添加QWidget对象,然后将其他的控件添加到QWidget对象上,再调用setLayout操作即可。
  • 对于QTreeView对象实现clicked事件,用于处理展开。
  • 实现QListView的Contextmenu和显示文件相关信息对话框。

4. 源代码

mainwindows.h

[source language="cpp"]
#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class QTreeView;
class QListView;
class QSplitter;
class QFileSystemModel;

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void Onclickedtree(const QModelIndex &idx);
void showContextMenu(const QPoint &pos);
void info();

private:
Ui::MainWindow *ui;

QTreeView *tree;
QListView *list;
QSplitter *splitter;
QFileSystemModel *model;
};

[/source]

mainwindows.cpp

[source language="cpp"]
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QSplitter>
#include <QFileSystemModel>
#include <QTreeView>
#include <QListView>
#include <QVboxLayout>
#include <QTreeWidget>
#include <QMessageBox>

#include "cfileinfodlg.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QWidget *centralWidget = new QWidget(this);
this->setCentralWidget(centralWidget);

resize(1200,600);
splitter = new QSplitter(centralWidget);

// model cls
QString dir = QString("/");
model = new QFileSystemModel;

model->setRootPath(dir);

// view control
tree = new QTreeView(splitter);
tree->setModel(model);
tree->setRootIndex(model->index(dir));

// view
list = new QListView(splitter);
list->setModel(model);
list->setRootIndex(model->index(dir));

// set context menu flag
list->setContextMenuPolicy(Qt::CustomContextMenu);

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(splitter);

// Set central widget layout, not on windows.
centralWidget->setLayout(layout);

connect(tree,SIGNAL(clicked(QModelIndex)),this,SLOT(Onclickedtree(QModelIndex)));

connect(list, SIGNAL(customContextMenuRequested(QPoint)),
this, SLOT(showContextMenu(QPoint)));
}

void MainWindow::info()
{
foreach(const QModelIndex &index, list->selectionModel()->selectedIndexes())
{
QFileInfo file_info = model->fileInfo(index);
CFileInfoDlg dlg(file_info, this);
dlg.exec();
}
}

void MainWindow::showContextMenu(const QPoint &pos)
{
// Handle global position
QPoint globalPos = list->mapToGlobal(pos);

// Create menu and insert some actions
QMenu myMenu;
myMenu.addAction("Detailed Info", this, SLOT(info()));
myMenu.addAction("Others");

// Show context menu at handling position
myMenu.exec(globalPos);
}

void MainWindow::Onclickedtree(const QModelIndex &idx)
{
QMessageBox box;
box.setText("Onclickedtree");
list->setModel(model);
list->setRootIndex(idx);
}

MainWindow::~MainWindow()
{
delete ui;
}

[/source]

cfileinfodlg.h

[source language="cpp"]
#include <QDialog>
#include <QFileInfo>

class CFileInfoDlg : public QDialog
{
Q_OBJECT

public:
CFileInfoDlg(QFileInfo &file_info,
QWidget *parent = 0,
Qt::WindowFlags f = 0);
};

[/source]

cfileinfodlg.cpp

[source language="cpp"]
#include "cfileinfodlg.h"
#include <QLabel>
#include <QLineEdit>
#include <QGridLayout>
#include <QDateTime>

CFileInfoDlg::CFileInfoDlg(QFileInfo &file_info,
QWidget *,
Qt::WindowFlags)
{
resize(300, 150);
QGridLayout *layout = new QGridLayout;

QLabel *lable_kind = new QLabel(QString("kind: "), this);
QLineEdit *kind = new QLineEdit(file_info.isDir()?"dir":"file", this);
kind->setReadOnly(true);

layout->addWidget(lable_kind, 0,0,1,1,Qt::AlignRight);
layout->addWidget(kind,0,1);

QLabel *lable_path = new QLabel(QString("path: "), this);
QLineEdit *path = new QLineEdit(file_info.absoluteFilePath(), this);
path->setReadOnly(true);

layout->addWidget(lable_path,1,0,1,1,Qt::AlignRight);
layout->addWidget(path,1,1);

QLabel *lable_size = new QLabel(QString("size: "), this);
QLineEdit *size = new QLineEdit(QString::number(file_info.size()), this);
size->setReadOnly(true);

layout->addWidget(lable_size,2,0,1,1,Qt::AlignRight);
layout->addWidget(size,2,1);

QLabel *label_modified = new QLabel(QString("modified: "), this);
QDateTime modified_tm = file_info.lastModified();
QLineEdit *tm = new QLineEdit(modified_tm.toString(), this);
tm->setReadOnly(true);

layout->addWidget(label_modified,3,0,1,1,Qt::AlignRight);
layout->addWidget(tm,3,1);

this->setLayout(layout);
}
[/source]

完整工程下载 qt_simple_browse

备注: WordPress 默认不支持tar.gz,可以将代码压缩成zip上传, 压缩命令: zip -q -r qt_simple_browser.zip QTMVC/

发表评论

电子邮件地址不会被公开。 必填项已用*标注