#include <QtWidgets>
#include "filesearchdialog.h"
#include "optiondialog.h"
#include "aboutdialog.h"
#include "itemdelegate.h"
FileSearchDialog::FileSearchDialog(QWidget *parent) :
QDialog(parent)
{
this->setWindowFlags(Qt::Window);
this->resize(1153, 694);
initFileSearchDialog();
createMenus();
createActions();
connect(&m_timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
m_timer.start(1000);
m_thread = new Thread(this);
connect(m_thread, SIGNAL(showFiles(QString)), this, SLOT(onShowFiles(QString)));
}
FileSearchDialog::~FileSearchDialog()
{
// m_thread->quit();
// m_thread->wait();
delete m_thread;
m_thread = NULL;
}
void FileSearchDialog::initFileSearchDialog()
{
m_menuBar = new QMenuBar(this);
m_menuBar->setFixedHeight(23);
m_searchEdit = new QLineEdit(this);
m_searchEdit->setFixedHeight(23);
m_searchEdit->setStyleSheet("QLineEdit{border:1px solid rgb(130, 135, 144)}");
m_searchBtn = new QToolButton(m_searchEdit);
m_searchBtn->setFixedSize(20, m_searchEdit->sizeHint().height());
m_searchBtn->setIcon(QIcon(":/images/search.png"));
m_searchBtn->setCursor(Qt::ArrowCursor);
m_searchBtn->setStyleSheet("QToolButton{background:transparent;}");
connect(m_searchBtn, SIGNAL(clicked(bool)), this, SLOT(onSearch()));
//防止文本位于按钮之下
QMargins margins = m_searchEdit->textMargins();
m_searchEdit->setTextMargins(margins.left(), margins.top(), m_searchBtn->width(), margins.bottom());
QHBoxLayout *hLayout2 = new QHBoxLayout();
hLayout2->setContentsMargins(0, 0, 0, 0);
hLayout2->addStretch();
hLayout2->addWidget(m_searchBtn);
m_searchEdit->setLayout(hLayout2);
m_tableWidget = new QTableWidget(this);
m_tableWidget->setColumnCount(5);
m_tableWidget->setColumnWidth(0, 130);
m_tableWidget->setColumnWidth(1, 550);
m_tableWidget->setColumnWidth(2, 130);
m_tableWidget->setColumnWidth(3, 130);
m_tableWidget->setColumnWidth(4, 130);
m_tableWidget->horizontalHeader()->setFixedHeight(25); //设置水平表头的高度
m_tableWidget->horizontalHeader()->setSectionsClickable(false); //禁止水平表头被点击
m_tableWidget->verticalHeader()->setVisible(false); //设置隐藏垂直表头
m_tableWidget->verticalHeader()->setDefaultSectionSize(25); //设置表格的行高
m_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选中一行
m_tableWidget->setSelectionMode(QAbstractItemView::SingleSelection); //设置为单选模式
m_tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //禁止编辑
m_tableWidget->setItemDelegate(new ItemDelegate()); //设置委托
connect(m_tableWidget, SIGNAL(itemDoubleClicked(QTableWidgetItem*)), this, SLOT(onTableWidgetItemDoubleClicked(QTableWidgetItem*)));
QStringList strList;
strList << "Name" << "Path" << "Size" << "Modify Time" << "Permit";
m_tableWidget->setHorizontalHeaderLabels(strList);
m_statusBar = new QStatusBar(this);
m_statusLabel = new QLabel("");
m_statusLabel->setFrameShape(QFrame::WinPanel);
m_statusLabel->setFrameShadow(QFrame::Sunken);
m_statusBar->addWidget(m_statusLabel, 1);
QString strTime = QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss");
m_timeLabel = new QLabel(strTime);
m_timeLabel->setFixedWidth(125);
m_timeLabel->setAlignment(Qt::AlignRight);
m_timeLabel->setFrameShape(QFrame::WinPanel);
m_timeLabel->setFrameShadow(QFrame::Sunken);
m_statusBar->addWidget(m_timeLabel, 1);
QVBoxLayout *mainLayout = new QVBoxLayout();
mainLayout->addWidget(m_menuBar);
mainLayout->addWidget(m_searchEdit);
mainLayout->addWidget(m_tableWidget);
mainLayout->addWidget(m_statusBar);
this->setLayout(mainLayout);
}
void FileSearchDialog::createMenus()
{
m_fileMenu = m_menuBar->addMenu("File");
m_editMenu = m_menuBar->addMenu("Edit");
m_viewMenu = m_menuBar->addMenu("View");
m_searchMenu = m_menuBar->addMenu("Search");
m_toolMenu = m_menuBar->addMenu("Tool");
m_helpMenu = m_menuBar->addMenu("Help");
}
void FileSearchDialog::createActions()
{
m_actOpen = new QAction("Open", this);
m_actOpen->setShortcut(tr("Ctrl+O"));
connect(m_actOpen, SIGNAL(triggered(bool)), this, SLOT(onOpen()));
m_fileMenu->addAction(m_actOpen);
m_fileMenu->addSeparator();
m_actExit = new QAction("Exit", this);
m_actExit->setShortcut(tr("Ctrl+Shift+X"));
connect(m_actExit, SIGNAL(triggered(bool)), this, SLOT(onExit()));
m_fileMenu->addAction(m_actExit);
m_actCopyTo = new QAction("Copy To Folder", this);
connect(m_actCopyTo, SIGNAL(triggered(bool)), this, SLOT(onCopyTo()));
m_editMenu->addAction(m_actCopyTo);
m_actRemoveTo = new QAction("Remove To Folder", this);
connect(m_actRemoveTo, SIGNAL(triggered(bool)), this, SLOT(onRemoveTo()));
m_editMenu->addAction(m_actRemoveTo);
m_actStatusBar = new QAction("Status Bar", this);
m_actStatusBar->setCheckable(true);
m_actStatusBar->setChecked(true);
connect(m_actStatusBar, SIGNAL(triggered(bool)), this, SLOT(onStatusBar()));
m_viewMenu->addAction(m_actStatusBar);
m_actCaseSensitive = new QAction("Case Sensitive", this);
m_actCaseSensitive->setCheckable(true);
m_searchMenu->addAction(m_actCaseSensitive);
m_actWholeWords = new QAction("Whole Words", this);
m_actWholeWords->setCheckable(true);
m_searchMenu->addAction(m_actWholeWords);
m_actOption = new QAction("Option", this);
connect(m_actOption, SIGNAL(triggered(bool)), this, SLOT(onOption()));
m_toolMenu->addAction(m_actOption);
m_actAbout = new QAction("About", this);
m_actAbout->setShortcut(tr("F1"));
connect(m_actAbout, SIGNAL(triggered(bool)), this, SLOT(onAbout()));
m_helpMenu->addAction(m_actAbout);
}
QString FileSearchDialog::getSearchFileName()
{
return m_searchEdit->text();
}
bool FileSearchDialog::getIsCaseSensitive()
{
return m_actCaseSensitive->isChecked() ? true : false;
}
bool FileSearchDialog::getIsWholeWords()
{
return m_actWholeWords->isChecked() ? true : false;
}
void FileSearchDialog::onTimeout()
{
m_timer.stop();
QString strTime = QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss");
m_timeLabel->setText(strTime);
m_timer.start(1000);
}
void FileSearchDialog::onShowFiles(const QString &file)
{
QFileInfo fileInfo(file);
int rowCount = m_tableWidget->rowCount();
m_tableWidget->insertRow(rowCount);
QString fileName = fileInfo.fileName();
QTableWidgetItem *item = new QTableWidgetItem;
item->setText(fileName);
item->setTextAlignment(Qt::AlignCenter);
m_tableWidget->setItem(rowCount, 0, item);
QString filePath = fileInfo.filePath();
item = new QTableWidgetItem;
item->setText(filePath);
item->setTextAlignment(Qt::AlignCenter);
m_tableWidget->setItem(rowCount, 1, item);
int fileSize = fileInfo.size();
item = new QTableWidgetItem;
item->setText(tr("%1k").arg(fileSize / 1024.0, 0, 'f', 2));
item->setTextAlignment(Qt::AlignCenter);
m_tableWidget->setItem(rowCount, 2, item);
QString modifyTime = fileInfo.lastModified().toString("yyyy.MM.dd hh:mm:ss");
item = new QTableWidgetItem;
item->setText(modifyTime);
item->setTextAlignment(Qt::AlignCenter);
m_tableWidget->setItem(rowCount, 3, item);
QString permit = fileInfo.isWritable() ? "Write" : "Read";
item = new QTableWidgetItem;
item->setText(permit);
item->setTextAlignment(Qt::AlignCenter);
m_tableWidget->setItem(rowCount, 4, item);
m_statusLabel->setText(tr("Search %1 files").arg(rowCount + 1));
}
void FileSearchDialog::onSear
没有合适的资源?快使用搜索试试~ 我知道了~
Qt实现文件搜索
共17个文件
cpp:6个
h:5个
pro:1个
3星 · 超过75%的资源 需积分: 46 61 下载量 71 浏览量
2019-08-28
16:08:20
上传
评论 4
收藏 12KB RAR 举报
温馨提示
运用Qt框架实现文件搜索,将文件搜索算法进行改良,可以浏览文件夹里面的文件进行搜索,可以双击表里面的文件进行编辑,支持.docx、.xlsx、.pptx、.accdb、.txt等文件格式的编辑,供学习和研究。
资源推荐
资源详情
资源评论
收起资源包目录
FileSearch.rar (17个子文件)
FileSearch
itemdelegate.h 323B
logo.ico 17KB
aboutdialog.h 543B
filesearch.qrc 101B
filesearchdialog.cpp 11KB
images
search.png 2KB
aboutdialog.cpp 2KB
thread.cpp 2KB
uac.manifest 422B
main.cpp 195B
optiondialog.h 286B
optiondialog.cpp 1KB
filesearchdialog.h 2KB
itemdelegate.cpp 464B
resource.rc 70B
thread.h 436B
FileSearch.pro 622B
共 17 条
- 1
资源评论
- weixin_404582182021-09-2309:32:26: 启动程序失败,路径或者权限错误?
wgj22068
- 粉丝: 10
- 资源: 45
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功