#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle(tr("FileSystemBrowers"));
m_pMainSplitter = new QZZSplitter(Qt::Horizontal);
m_pDicTree = new QTreeWidget(m_pMainSplitter);
m_pRightSplitter = new QZZSplitter(Qt::Vertical, m_pMainSplitter);
m_pDicTable = new QTableWidget(m_pRightSplitter);
m_pFileInfo = new QLabel(m_pRightSplitter);
m_pFileInfo->setText("我是文件信息窗口");
m_pMainSplitter->setStretchFactor(1, 1);
m_pMainLayout = new QHBoxLayout();
m_pMainLayout->addWidget(m_pMainSplitter);
InitDicTree();
InitDicTable();
SignalSlotInit();
this->centralWidget()->setLayout(m_pMainLayout); //在MainWindow 窗口中需要调用这样调用来设置
}
void MainWindow::SignalSlotInit()
{
connect(m_pDicTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int )), this, SLOT(DirTreeDBClicked(QTreeWidgetItem *, int )));
connect(m_pDicTree, SIGNAL(itemClicked(QTreeWidgetItem *, int )), this, SLOT(DirTreeClicked(QTreeWidgetItem *, int )));
connect(m_pDicTable, SIGNAL(itemClicked(QTableWidgetItem *)), this, SLOT(DicTableClicked(QTableWidgetItem *)));
}
void MainWindow::DirTreeClicked(QTreeWidgetItem *item, int column)
{
QVariant var= item->data(column, Qt::UserRole);
DicTreeData dtd = var.value<DicTreeData>();
if(!dtd.isClicked)
{
dtd.isClicked = true;
var=QVariant::fromValue(dtd);
item->setData(0, Qt::UserRole, var);
//qDebug()<<dtd.strPath<<endl;
AddDicTreeChild(item, column); //遍历目录
}
if(!dtd.isRoot)
{
BrowFileOfDIC(item, column); //遍历文件
}
if(!item->isExpanded())
{
item->setExpanded(true);
}
else
{
item->setExpanded(false);
}
}
void MainWindow::DirTreeDBClicked(QTreeWidgetItem *item, int column)
{
}
void MainWindow::DicTableClicked(QTableWidgetItem *item)
{
QList<QTableWidgetItem*> items = m_pDicTable->selectedItems();
QString strName = items.at(0)->text();
strName = m_strPrePath + strName;
m_pFileInfo->setText(strName);
//qDebug()<<m_strPrePath<<strName;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::InitDicTree()
{
m_pDicTree->clear();
m_pDicTree->setHeaderHidden(true);
QFileIconProvider fileIconProvider;
DicTreeData dtd;
QVariant vdtd;
dtd.isClicked = true;
dtd.isRoot = true;
vdtd=QVariant::fromValue(dtd);
QTreeWidgetItem *pRootItem = new QTreeWidgetItem(m_pDicTree);
QString strValue= tr("计算机");
pRootItem->setText(0, strValue);
pRootItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
pRootItem->setIcon(0,fileIconProvider.icon(QFileIconProvider::Computer));
pRootItem->setData(0, Qt::UserRole, vdtd);
pRootItem->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
QTreeWidgetItem *pItem = NULL;
dtd.isClicked = false;
dtd.isRoot = false;
foreach(QFileInfo fileInfo, m_qLogicDir.drives())
{
pItem =new QTreeWidgetItem(pRootItem);
dtd.strPath = fileInfo.absoluteFilePath();
vdtd=QVariant::fromValue(dtd);
pItem->setText(0, dtd.strPath.replace(2,1,'\\'));
pItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
pItem->setData(0, Qt::UserRole, vdtd);
pItem->setIcon(0, fileIconProvider.icon(QFileIconProvider::Drive));
pItem->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator); //显示左边三角形
}
}
void MainWindow::InitDicTable()
{
m_strPrePath = "";
QTableWidgetItem *pItem = NULL;
QStringList headerList;
headerList<<"文件名"<<"创建时间"<<"最后修改时间";
m_pDicTable->setColumnCount(headerList.count());
for (int i = 0; i < m_pDicTable->columnCount() ; i++)
{
pItem = new QTableWidgetItem(headerList.at(i));
m_pDicTable->setHorizontalHeaderItem(i, pItem);
}
//设置列宽自适应
m_pDicTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
m_pDicTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
m_pDicTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_pDicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
m_pDicTable->setAlternatingRowColors(true);
m_pDicTable->setRowCount(0);
}
void MainWindow::AddDicTreeChild(QTreeWidgetItem *item, int column)
{
QTreeWidgetItem *PRoot = item;
QFileIconProvider fileIconProvider;
QTreeWidgetItem *pItem = NULL;
DicTreeData dtdSubDic;
QVariant vdtd;
QVariant var= item->data(column, Qt::UserRole);
DicTreeData dtd = var.value<DicTreeData>();
QString strPath = dtd.strPath;
if(dtd.strPath.right(1) != '/')
{
strPath = strPath +'/';
}
QDir tmpDir(strPath);
DicFileData dfd;
foreach(QFileInfo fileInfo, tmpDir.entryInfoList())
{
if(fileInfo.isDir() && fileInfo.fileName().compare(".") && fileInfo.fileName().compare("..")) //目录添加到目录树上
{
pItem =new QTreeWidgetItem(PRoot);
dtdSubDic.strPath = fileInfo.absoluteFilePath();
vdtd=QVariant::fromValue(dtdSubDic);
pItem->setText(0, fileInfo.fileName());
pItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
pItem->setData(0, Qt::UserRole, vdtd);
pItem->setIcon(0, fileIconProvider.icon(QFileIconProvider::Folder));
pItem->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);//显示左边三角形
//qDebug()<<fileInfo.fileName()<<endl;
}
}
}
void MainWindow::BrowFileOfDIC(QTreeWidgetItem *item, int column)
{
DicTreeData dtdSubDic;
QVariant vdtd;
QVariant var= item->data(column, Qt::UserRole);
DicTreeData dtd = var.value<DicTreeData>();
QString strPath = dtd.strPath;
if(dtd.strPath.right(1) != '/')
{
strPath = strPath +'/';
}
if(m_strPrePath.compare(strPath))
{
m_strPrePath = strPath;
QDir tmpDir(strPath);
DicFileData dfd;
RemoveAllRow();
foreach(QFileInfo fileInfo, tmpDir.entryInfoList())
{
if(fileInfo.isFile())//文件添加到右边列表中
{
//文件添加到右边list控件中
dfd.strName = fileInfo.fileName();
dfd.strTime = fileInfo.birthTime().toString("yyyy-MM-dd hh:mm:ss");
dfd.strLastModify = fileInfo.lastModified().toString("yyyy-MM-dd hh:mm:ss");
AddFileList(dfd);
}
}
}
}
void MainWindow::AddFileList(DicFileData dfd)
{
//m_pDicTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
int iCurRow = m_pDicTable->rowCount();
m_pDicTable->insertRow(iCurRow);
m_pDicTable->setItem(iCurRow, 0, new QTableWidgetItem(dfd.strName));
m_pDicTable->setItem(iCurRow, 1, new QTableWidgetItem(dfd.strTime));
m_pDicTable->setItem(iCurRow, 2, new QTableWidgetItem(dfd.strLastModify));
}
void MainWindow::RemoveAllRow()
{
int iRowCount = m_pDicTable->rowCount();
for (int i = 0; i <= iRowCount ;i++)
{
m_pDicTable->removeRow(i);
}
m_pDicTable->setRowCount(0);
}
没有合适的资源?快使用搜索试试~ 我知道了~
QT编写文件系统浏览器
共9个文件
cpp:3个
h:3个
pro:1个
需积分: 5 1 下载量 189 浏览量
2023-04-04
17:47:56
上传
评论
收藏 7KB ZIP 举报
温馨提示
1、 使用到:QZZSplitter,QHBoxLayout,QTreeWidget,QTableWidget 四个控件
资源推荐
资源详情
资源评论
收起资源包目录
FileSystemBrower.zip (9个子文件)
DataStructs.h 500B
mainwindow.h 2KB
mainwindow.cpp 7KB
FileSystemBrower.pro 679B
main.cpp 331B
qzzsplitter.cpp 219B
mainwindow.ui 558B
qzzsplitter.h 284B
FileSystemBrower.pro.user 19KB
共 9 条
- 1
资源评论
林森之
- 粉丝: 58
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功