Qt系列文章目录
文章目录
- Qt系列文章目录
- 前言
- 一、QListWidget 和 QListView 的差异
- 二、显示效果
- 1.操作工作区界面
- 1.主界面头文件
- 2. 主界面实现界面
- 2.左边图片目录展示界面
- 1.图片目录头文件
- 2.图片目录实现文件
- 2.属性窗口区
- 1.属性窗口头文件
- 2.属性窗口实现文件
- 3 源码下载
前言
QListWidget类提供了一个基于项目的列表小部件
QListWidget是一个方便的类,它提供了一个列表视图,类似于QListView提供的列表视图,但是具有一个用于添加和删除项的经典基于项的接口。QListWidget使用一个内部模型来管理列表中的每个QListWidgetItem。要获得更灵活的列表视图小部件,请使用带有标准模型的QListView类。列表小部件的构造方法与其他小部件相同。QListWidget与QListView类似,都可以显示一列Item,区别在于前者可以往其中增删Item。
QListWidget确定可以同时选择列表中的多少项,以及是否可以创建复杂的项选择。这可以使用函数设置
一、QListWidget 和 QListView 的差异
QListWidget 是一个更新且更高级的元件,能够更为方便地进行开发,例如 QListWidget 具有 QStantandardItemModel 无法访问的类型,也能更轻鬆的透过 QListWidgetItem 处理数据,然而如果使用 QListView,许多方法必须要额外定义,属于比较旧的使用方式。
QListView、QListWidget是列表形式展示的控件。
QTableView、QTableWidget是表格形式展示控件。
继承关系:带Widget的继承自View,即:QListWidget是继承QListView,QTableWidget继承自QTableView。
区别:QListView是基于Model,而QListWidget基于Item。这是它们的本质区别。QTableView、QTableWidget同理。
由于QListView和QTableView是基于model的,需要自己来建模(例如建立QStringListModel、QSqlTableModel等),保存数据,这样就大大降低了数据冗余,提高了程序的效率,以及能更方便的进行我们自己需要展示的内容,但是需要我们对数据建模有一定了解。
而QListWidget相当于是QListView的升级版,它已经自己为我们建立了一个数据存储模型(QListWidgetItem),操作方便,直接调用addItem即可添加项目(ICON,文字)。QTableView、QTableWidget同理。
二、显示效果
1.操作工作区界面
1.主界面头文件
#pragma#include "ui_QDockWidgetDemo.h"
#include "MsgBox.h"#include <QMainWindow>
#include <QDockWidget>
#include <QTextEdit>
#include <QPushButton>
#include <QKeyEvent>#include "propertyWin.h"
#include "projectWin.h"#include "FileMonitorMgr.h"QT_FORWARD_DECLARE_CLASS(QMenu)class QDockWidgetDemo : public QMainWindow
{Q_OBJECTpublic:explicit QDockWidgetDemo(QWidget* parent = nullptr);private slots:void addLog(const QString&);//标题栏槽函数void s_showToolBar();void s_showPorjectWin();void s_showPropertyWin();void s_showLogWin();void s_monitorFile();void s_close();void s_min();void s_hideProj();void s_propertyWinHide(bool);void s_logWinHide(bool);void s_timeout();void s_MonitorFolder();void s_saveFile();void s_runAc();void s_stopAc();void s_debugAc();void s_anaysisAc();void s_recordAc();void s_grabAc();void s_capacityAc();void s_paramterAc();void s_publishAc();void s_exportAc();private:void initTitleBar();void initLogView();void initWorkSpaceView();void initPropertyView();void initProjectView();void initToolBar();void dockLayout();void initMaxMinWin();void initShowImage();void showMessageBox(const QString&);private:Ui::QDockWidgetDemo ui;//标题栏QAction* m_toolBarAc = NULL;QAction* m_projectAc = NULL;QAction* m_logAc = NULL;QAction* m_propertyAc = NULL;//工具栏QAction* m_newAc = NULL;QAction* m_saveAc = NULL;QAction* m_runAc = NULL;QAction* m_stopAc = NULL;QAction* m_debugAc = NULL;QAction* m_anaysisAc = NULL;QAction* m_recordAc = NULL;QAction* m_grabAc = NULL;QAction* m_capacityAc = NULL;QAction* m_paramterAc = NULL;QAction* m_publishAc = NULL;QAction* m_exportAc = NULL;QDockWidget* m_logView = NULL;QDockWidget* m_propertyView = NULL;QDockWidget* m_projManagerView = NULL;QTextEdit* m_logBody = NULL;QWidget* m_maxminWin = NULL;QWidget* m_workspace = NULL;projectWin* m_projectWin = NULL;propertyWin* m_propertyWin = NULL;MsgBox* m_msgBox = NULL;QPushButton* m_minBtn = NULL;QPushButton* m_closeBtn = NULL;//监控文件夹FileMonitorMgr* m_fileMgr;
private:void init();
signals:void sigCommitReconRequest(const QString& strFilePath);void sigInitTree(QStringList fileLst, QString filePath);void sigFilterJpg(QStringList jgpFileLst, QString filePath);protected slots:void slotDirectoryChanged(const QString& strDirectory);void watchFloder(bool flag);void slotShowImgs(QStringList lst, QString path);
private:QStringList GetFileNames(const QFileInfoList& fileInfoList);QStringList getFolderFiles(const QString& path);QStringList fliterJPG(const QString& path);private:QString m_strMonitorDirectory;QStringList m_strListFileNames;QStringList m_allFile;QStringList m_filterJpgImg;QFileSystemWatcher* m_pDirectoryWatcher;QVector<QString> m_changeFiles;QListWidget* m_ImageList;};
2. 主界面实现界面
#include "QDockWidgetDemo.h"#include <QDesktopWidget>
#include <QDebug>
#include <QStandardPaths>
#include <QTimer>
#include <QHBoxLayout>
#include <QSpacerItem>
#include <QFileDialog>
#include <QDir>#pragma execution_character_set("utf-8")Q_DECLARE_METATYPE(QDockWidget::DockWidgetFeatures)QString MenuBarStyle =
"QMenuBar{background-color:#FFFFFF; font:14px; color:#232323;}"
"QMenuBar::item{\min-height:40px; \margin:1 10 0 10px; \padding:10 10 10 10px; /* 条目内边框 */ \background:#FFFFFF; /* 背景颜色 */ \border-radius:4px; /* 倒角 */ \}""QMenuBar::item:selected{background: #E5E5E5; }";QString MenuStyle =
"QMenu{/*整个背景*/}"
"QMenu::item{ \font-size: 14px; \color: rgb(225,225,225); /*字体颜色*/\border: 3px solid rgb(60,60,60); /*item选框*/\background-color:rgb(89,87,87); \padding:16px 16px; /*设置菜单项文字上下和左右的内边距,效果就是菜单中的条目左右上下有了间隔*/\margin:0px 2px;/*设置菜单项的外边距*/\}""QMenu::item:selected{background-color:#1E1E1E;/*选中的样式*/}""QMenu::item:pressed{/*菜单项按下效果*/ border: 1px solid rgb(60,60,61); background-color: rgb(220,80,6);}";QString tabBarStyle =
"QTabBar::tab{min-height: 28px; min-width:80px ; font:14px; }"
"QTabBar::tab:!selected{}"
"QTabBar::tab:!selected:hover{ background-color: #d9fffe; color: black;}"
"QToolBar{background-color:#e5e5e5; margin:3px;}"
"QToolBar::separator{height:1px; background-color:#000000;}"
"QToolBar QToolButton{min-width:60px; min-height:40px; background-color:transparent;}"
"QToolBar QToolButton:hover{background-color:rgba(255,255,255,0.8);}"
"QToolBar QToolButton:pressed{background-color:rgba(255,255,255,0.5);}"
;QDockWidgetDemo::QDockWidgetDemo(QWidget* parent) : QMainWindow(parent)
{qRegisterMetaType<QDockWidget::DockWidgetFeatures>();ui.setupUi(this);this->setWindowTitle(tr("图像处理"));this->setWindowIcon(QIcon(":/images/logo.png"));this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);this->setStyleSheet(tabBarStyle);//this->setStyleSheet("QMainWindow::separator {width: 2px;height: 2px;margin: 2px;padding: 2,2px;}");//On the top menu bar add toolbar,menubar and dockwidget.initTitleBar();initToolBar();initLogView();initProjectView(); //项目区初始化在前。initWorkSpaceView(); //工作区初始化在后。initPropertyView();dockLayout();initShowImage(); //显示图片this->setTabPosition(Qt::AllDockWidgetAreas, QTabWidget::North);//This property holds the tab shape used for tabbed dock widgets.this->setTabShape(QTabWidget::Triangular);this->statusBar()->showMessage(tr("Status Bar"));addLog("框架启动完成,欢迎进入主界面!");QTimer::singleShot(2, this, SLOT(s_timeout()));
}void QDockWidgetDemo::initWorkSpaceView() {if (NULL == m_workspace) {m_workspace = new QWidget;m_workspace->setWindowTitle("工作区");this->setCentralWidget(m_workspace);}qDebug() << "工作区 id:" << m_workspace->winId() << ", name:" << m_workspace->windowTitle();
}void QDockWidgetDemo::initTitleBar()
{QMenuBar* m_MenuBar = this->menuBar();m_MenuBar->setStyleSheet(MenuBarStyle);QMenu* m_viewMenu = this->menuBar()->addMenu(tr("视图"));m_viewMenu->setStyleSheet(MenuStyle);m_toolBarAc = new QAction("工具栏");connect(m_toolBarAc, &QAction::triggered, this, &QDockWidgetDemo::s_showToolBar);m_toolBarAc->setCheckable(true);m_toolBarAc->setChecked(true);m_projectAc = new QAction("项目");connect(m_projectAc, &QAction::triggered, this, &QDockWidgetDemo::s_showPorjectWin);m_projectAc->setCheckable(true);m_projectAc->setChecked(true);m_propertyAc = new QAction("属性");connect(m_propertyAc, &QAction::triggered, this, &QDockWidgetDemo::s_showPropertyWin);m_propertyAc->setCheckable(true);m_propertyAc->setChecked(true);m_logAc = new QAction("输出");connect(m_logAc, &QAction::triggered, this, &QDockWidgetDemo::s_showLogWin);m_logAc->setCheckable(true);m_logAc->setChecked(true);m_viewMenu->addAction(m_toolBarAc);m_viewMenu->addAction(m_projectAc);m_viewMenu->addAction(m_propertyAc);m_viewMenu->addAction(m_logAc);QMenu* m_setView = this->menuBar()->addMenu(tr("编辑"));QMenu* m_toolMenu = this-> menuBar()->addMenu(tr("目标识别"));QMenu* m_sar = this->menuBar()->addMenu(tr("SAR处理"));QMenu* m_picture = this->menuBar()->addMenu(tr("图像"));QMenu* m_helpMenu = this->menuBar()->addMenu(tr("帮助"));//connect(m_setView, SIGNAL(triggered(QAction*)), this, SLOT(s_monitorFile(QAction*)));connect(m_setView, SIGNAL(triggered(QAction*)), this, SLOT(s_monitorFile(QAction*)));//connect(m_setView, &QMenu::triggered, this, &QDockWidgetDemo::s_monitorFile);initMaxMinWin();}void QDockWidgetDemo::initMaxMinWin() {m_maxminWin = new QWidget(this);QHBoxLayout* horizontalLayout;QSpacerItem* horizontalSpacer;horizontalLayout = new QHBoxLayout(m_maxminWin);horizontalLayout->setSpacing(0);horizontalLayout->setContentsMargins(11, 11, 11, 11);horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));horizontalLayout->setContentsMargins(0, 0, 0, 0);horizontalSpacer = new QSpacerItem(241, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);m_minBtn = new QPushButton(m_maxminWin);connect(m_minBtn, &QPushButton::clicked, this, &QDockWidgetDemo::s_min);m_minBtn->setObjectName(QString::fromUtf8("minBtn"));m_minBtn->setMinimumSize(QSize(40, 40));m_minBtn->setMaximumSize(QSize(40, 40));m_minBtn->setStyleSheet(QString::fromUtf8("QPushButton{border:none;}\n""QPushButton::hover{background-color:#E5E5E5;}\n"""));QIcon icon;icon.addFile(QString::fromUtf8(":/images/min_main.png"), QSize(), QIcon::Normal, QIcon::Off);m_minBtn->setIcon(icon);m_minBtn->setIconSize(QSize(25, 25));m_minBtn->setAutoDefault(false);m_minBtn->setFlat(false);m_minBtn->setDefault(false);m_closeBtn = new QPushButton(m_maxminWin);connect(m_closeBtn, &QPushButton::clicked, this, &QDockWidgetDemo::s_close);m_closeBtn->setObjectName(QString::fromUtf8("closeBtn"));m_closeBtn->setMinimumSize(QSize(40, 40));m_closeBtn->setMaximumSize(QSize(40, 40));m_closeBtn->setStyleSheet(QString::fromUtf8("QPushButton{border:none;}\n""QPushButton::hover{background-color:#E81123;}\n"));QIcon icon1;icon1.addFile(QString::fromUtf8(":/images/close_release.png"), QSize(), QIcon::Normal, QIcon::Off);m_closeBtn->setIcon(icon1);m_closeBtn->setIconSize(QSize(20, 20));m_closeBtn->setFlat(false);horizontalLayout->addItem(horizontalSpacer);horizontalLayout->addWidget(m_minBtn);horizontalLayout->addWidget(m_closeBtn);horizontalLayout->setStretch(0, 1);m_maxminWin->setLayout(horizontalLayout);this->menuBar()->setCornerWidget(m_maxminWin, Qt::TopRightCorner);
}void QDockWidgetDemo::initShowImage()
{m_ImageList = new QListWidget(m_workspace);m_ImageList->resize(2150, 1080);m_ImageList->setViewMode(QListWidget::IconMode); //显示模式m_ImageList->setIconSize(QSize(400, 320));//设置图片大小m_ImageList->setSpacing(0);//间距//m_ImageList->setResizeMode(QListWidget::Adjust); //适应布局调整m_ImageList->setMovement(QListWidget::Free); //可移动,static:不可移动m_ImageList->setDragDropMode(QAbstractItemView::InternalMove); //可拖拽m_ImageList->setWrapping(true); //自动换行m_ImageList->setFlow(QListWidget::TopToBottom); //从上到下 LeftToRight从左到右布局/*QGridLayout* grid = new QGridLayout;grid->addWidget(m_ImageList);this->setLayout(grid);*/m_ImageList->setHidden(true);}void QDockWidgetDemo::QDockWidgetDemo::s_showToolBar() {if (m_toolBarAc->isChecked()) {ui.mainToolBar->show();addLog("显示 工具栏");}else {ui.mainToolBar->hide();addLog("隐藏 工具栏");}
}void QDockWidgetDemo::QDockWidgetDemo::s_showPorjectWin() {if (m_projectAc->isChecked()) {m_projManagerView->show();addLog("显示 项目区");}else {m_projManagerView->hide();addLog("隐藏 项目区");}
}void QDockWidgetDemo::QDockWidgetDemo::s_showPropertyWin() {if (m_propertyAc->isChecked()) {m_propertyView->show();addLog("显示 属性区");}else {m_propertyView->hide();addLog("隐藏 属性区");}
}void QDockWidgetDemo::QDockWidgetDemo::s_showLogWin() {if (m_logAc->isChecked()) {m_logView->show();addLog("显示 日志区");}else {m_logView->hide();addLog("隐藏 日志区");}
}void QDockWidgetDemo::QDockWidgetDemo::s_timeout() {this->showMaximized();
}void QDockWidgetDemo::QDockWidgetDemo::s_close() {this->close();
}void QDockWidgetDemo::QDockWidgetDemo::s_min() {this->showMinimized();
}void QDockWidgetDemo::addLog(const QString& log)
{m_logBody->setReadOnly(true);m_logBody->append(log);
}void QDockWidgetDemo::s_monitorFile()
{qDebug() << tr("文件夹监控中");
}void QDockWidgetDemo::initLogView() {if (NULL == m_logView) {m_logView = new QDockWidget(this);//set dock widget feature: not move, enable close.m_logView->setFeatures(QDockWidget::DockWidgetClosable);m_logView->setWindowTitle("输出");m_logBody = new QTextEdit(this);m_logView->setWidget(m_logBody);}QPalette pl = m_logBody->palette();pl.setBrush(QPalette::Base, QBrush(QColor(255, 0, 0, 0)));m_logBody->setPalette(pl);
}void QDockWidgetDemo::initPropertyView() {if (NULL == m_propertyView) {m_propertyView = new QDockWidget(this);m_propertyView->setFeatures(QDockWidget::DockWidgetClosable);m_propertyView->setWindowTitle("飞行参数");m_propertyWin = new propertyWin(this);m_propertyView->setWidget(m_propertyWin);qDebug() << "属性区 id:" << m_propertyView->winId() << ", name:" << m_propertyView->windowTitle();}
}void QDockWidgetDemo::initProjectView() {if (NULL == m_projManagerView) {m_projManagerView = new QDockWidget(this);m_projManagerView->setFeatures(QDockWidget::DockWidgetClosable);m_projManagerView->setWindowTitle("图像列表");delete title bar//QWidget* lTitleBar = m_projManagerView->titleBarWidget();//QWidget* lEmptyWidget = new QWidget();//m_projManagerView->setTitleBarWidget(lEmptyWidget);//delete lTitleBar;m_projectWin = new projectWin(this);m_projManagerView->setWidget(m_projectWin);qDebug() << "项目区 id:" << m_projManagerView->winId() << ", name:" << m_projManagerView->windowTitle();connect(this, &QDockWidgetDemo::sigInitTree, m_projectWin, &projectWin::slotTree);connect(this, &QDockWidgetDemo::sigFilterJpg, this, &QDockWidgetDemo::slotShowImgs);}
}void QDockWidgetDemo::dockLayout() {this->addDockWidget(Qt::LeftDockWidgetArea, m_projManagerView, Qt::Orientation::Vertical);this->addDockWidget(Qt::RightDockWidgetArea, m_propertyView, Qt::Orientation::Vertical);this->addDockWidget(Qt::BottomDockWidgetArea, m_logView, Qt::Orientation::Vertical);
}void QDockWidgetDemo::initToolBar() {QSize toolIconSize(50, 30);ui.mainToolBar->setIconSize(toolIconSize); //设置工具栏图标大小QIcon newFileIcon(":/images/新建文件.png");QIcon openFileIcon(":/images/打开文件.png");QIcon saveFileIcon(":/images/保存.png");QIcon runIcon(":/images/运行.png");QIcon stopIcon(":/images/停止.png");QIcon debugIcon(":/images/调试.png");QIcon anaysisIcon(":/images/分析.png");QIcon recordIcon(":/images/录制.png");QIcon grabIcon(":/images/抓取.png");QIcon capacityIcon(":/images/应用中心.png");QIcon paramterIcon(":/images/参数.png");QIcon publishIcon(":/images/发布.png");QIcon exportIcon(":/images/导出.png");m_newAc = new QAction(newFileIcon, "新建项目", this);m_saveAc = new QAction(saveFileIcon, "保存", this);m_runAc = new QAction(runIcon, "运行", this);m_stopAc = new QAction(stopIcon, "停止", this);m_debugAc = new QAction(debugIcon, "调试", this);m_anaysisAc = new QAction(anaysisIcon, "分析", this);m_recordAc = new QAction(recordIcon, "录制", this);m_grabAc = new QAction(grabIcon, "抓取", this);m_capacityAc = new QAction(capacityIcon, "能力中心", this);m_paramterAc = new QAction(paramterIcon, "参数", this);m_publishAc = new QAction(publishIcon, "发布", this);m_exportAc = new QAction(exportIcon, "导出", this);//add QAction to Widget.ui.mainToolBar->addAction(m_newAc);ui.mainToolBar->addAction(m_saveAc);ui.mainToolBar->addAction(m_saveAc);ui.mainToolBar->addSeparator();ui.mainToolBar->addAction(m_runAc);ui.mainToolBar->addAction(m_stopAc);ui.mainToolBar->addAction(m_debugAc);ui.mainToolBar->addAction(m_anaysisAc);ui.mainToolBar->addSeparator();ui.mainToolBar->addAction(m_recordAc);ui.mainToolBar->addAction(m_grabAc);ui.mainToolBar->addSeparator();ui.mainToolBar->addAction(m_capacityAc);ui.mainToolBar->addSeparator();ui.mainToolBar->addAction(m_paramterAc);ui.mainToolBar->addAction(m_publishAc);ui.mainToolBar->addAction(m_exportAc);ui.mainToolBar->addSeparator();connect(m_newAc, &QAction::triggered, this, &QDockWidgetDemo::s_MonitorFolder);connect(m_saveAc, &QAction::triggered, this, &QDockWidgetDemo::s_saveFile);connect(m_runAc, &QAction::triggered, this, &QDockWidgetDemo::s_runAc);connect(m_stopAc, &QAction::triggered, this, &QDockWidgetDemo::s_stopAc);connect(m_debugAc, &QAction::triggered, this, &QDockWidgetDemo::s_debugAc);connect(m_anaysisAc, &QAction::triggered, this, &QDockWidgetDemo::s_anaysisAc);connect(m_recordAc, &QAction::triggered, this, &QDockWidgetDemo::s_recordAc);connect(m_grabAc, &QAction::triggered, this, &QDockWidgetDemo::s_grabAc);connect(m_capacityAc, &QAction::triggered, this, &QDockWidgetDemo::s_capacityAc);connect(m_paramterAc, &QAction::triggered, this, &QDockWidgetDemo::s_paramterAc);connect(m_publishAc, &QAction::triggered, this, &QDockWidgetDemo::s_publishAc);connect(m_exportAc, &QAction::triggered, this, &QDockWidgetDemo::s_exportAc);
}void QDockWidgetDemo::QDockWidgetDemo::s_hideProj() {m_projectAc->setChecked(false);m_projManagerView->hide();
}void QDockWidgetDemo::QDockWidgetDemo::s_propertyWinHide(bool visible) {if (!visible) {m_propertyAc->setChecked(false);m_propertyView->hide();}
}void QDockWidgetDemo::QDockWidgetDemo::s_logWinHide(bool visible) {if (!visible) {m_logAc->setChecked(false);m_logView->hide();}
}void QDockWidgetDemo::s_MonitorFolder() {m_strMonitorDirectory = QFileDialog::getExistingDirectory(this, tr("请选择共享文件夹"), "./");QString monitorFolder = tr("开始监控文件夹:") + m_strMonitorDirectory;addLog(monitorFolder);m_allFile = getFolderFiles(m_strMonitorDirectory);m_filterJpgImg = fliterJPG(m_strMonitorDirectory);emit sigInitTree(m_allFile, m_strMonitorDirectory);emit sigFilterJpg(m_filterJpgImg, m_strMonitorDirectory);
}
void QDockWidgetDemo::s_saveFile() {QString log = QString("功能正在开发中,请耐心等待!");showMessageBox(log);addLog(log);
}
void QDockWidgetDemo::s_runAc() {QString log = QString("功能正在开发中,请耐心等待!");showMessageBox(log);addLog(log);
}
void QDockWidgetDemo::s_stopAc() {QString log = QString("功能正在开发中,请耐心等待!");showMessageBox(log);addLog(log);
}
void QDockWidgetDemo::s_debugAc() {QString log = QString("功能正在开发中,请耐心等待!");showMessageBox(log);addLog(log);
}
void QDockWidgetDemo::s_anaysisAc() {QString log = QString("功能正在开发中,请耐心等待!");showMessageBox(log);addLog(log);
}
void QDockWidgetDemo::s_recordAc() {QString log = QString("功能正在开发中,请耐心等待!");showMessageBox(log);addLog(log);
}
void QDockWidgetDemo::s_grabAc() {QString log = QString("功能正在开发中,请耐心等待!");showMessageBox(log);addLog(log);
}
void QDockWidgetDemo::s_capacityAc() {QString log = QString("功能正在开发中,请耐心等待!");showMessageBox(log);addLog(log);
}
void QDockWidgetDemo::s_paramterAc() {QString log = QString("功能正在开发中,请耐心等待!");showMessageBox(log);addLog(log);
}
void QDockWidgetDemo::s_publishAc() {QString log = QString("功能正在开发中,请耐心等待!");showMessageBox(log);addLog(log);
}
void QDockWidgetDemo::s_exportAc() {QString log = QString("功能正在开发中,请耐心等待!");showMessageBox(log);addLog(log);
}void QDockWidgetDemo::showMessageBox(const QString& text) {if (NULL == m_msgBox) {m_msgBox = new MsgBox();}m_msgBox->setMsgText(text);m_msgBox->show();
}void QDockWidgetDemo::init()
{m_changeFiles.clear();m_strListFileNames.clear();QString filePath = "F:\\QtExercise\\FileMonitorMgr\\Test";m_strListFileNames = QDir(filePath).entryList();m_strMonitorDirectory = filePath;m_pDirectoryWatcher = new QFileSystemWatcher(this);m_pDirectoryWatcher->addPath(filePath);connect(m_pDirectoryWatcher, SIGNAL(directoryChanged(const QString&)), this, SLOT(slotDirectoryChanged(const QString&)));//connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(watchFloder(bool)));
}//QVector<QString> &MainWindow::getFileName()
//{
// return m_fileNameVec;
//}void QDockWidgetDemo::slotDirectoryChanged(const QString& strDirectory)
{m_changeFiles.clear();QStringList strListFileNames; //To save new file namesQFileInfoList fileInfoList = QDir(m_strMonitorDirectory).entryInfoList();for (int n = 0; n < fileInfoList.size(); n++){QFileInfo fileInfo = fileInfoList[n];if (fileInfo.fileName().compare(".") == 0 || fileInfo.fileName().compare("..") == 0)continue;if (fileInfo.isDir())continue;QString strFileName = fileInfo.fileName();if (!m_strListFileNames.contains(strFileName)){strListFileNames << strFileName;//ui->textEdit->append(strFileName);m_changeFiles.push_back(strFileName);}}if (strListFileNames.isEmpty()){m_strListFileNames.clear();m_strListFileNames = GetFileNames(fileInfoList);return;}for (int n = 0; n < strListFileNames.size(); n++){//commit recon requestQString strFilePath = m_strMonitorDirectory + "/" + strListFileNames[n];emit sigCommitReconRequest(strFilePath);}m_strListFileNames.clear();m_strListFileNames = GetFileNames(fileInfoList);for (int i = 0; i < m_changeFiles.size(); i++)//显示变化的文件名//ui->textEdit->append(m_changeFiles.at(i));return;
}void QDockWidgetDemo::watchFloder(bool flag)
{QString filePath = QFileDialog::getExistingDirectory(this, QStringLiteral("请选择共享文件夹"), "./");//显示监视的文件路径//ui->lineEdit->setText(filePath);// m_strListFileNames.clear();// m_strListFileNames = QDir(filePath).entryList();// m_strMonitorDirectory = filePath;// m_pDirectoryWatcher = new QFileSystemWatcher( this );// m_pDirectoryWatcher->addPath( filePath );// connect( m_pDirectoryWatcher, SIGNAL( directoryChanged( const QString& ) ), this, SLOT( slotDirectoryChanged( const QString& ) ) );
}void QDockWidgetDemo::slotShowImgs(QStringList lst, QString path)
{QString allPath = path + "/";for (int i = 0; i < lst.size(); i++){QString onlyFile = lst.at(i);int pos = onlyFile.lastIndexOf("/");onlyFile = lst.at(i).right(lst.at(i).size() - pos - 1);QListWidgetItem* imageItem = new QListWidgetItem(/*m_ImageList*/);QString allImgFiles = allPath + lst.at(i);/*QPixmap pPhoto;pPhoto.loadFromData(QByteArray(), "jpg");QIcon ico;ico.addPixmap(pPhoto);*/imageItem->setIcon(QIcon(lst.at(i)));//imageItem->setText(onlyFile);imageItem->setSizeHint(QSize(190, 150));m_ImageList->addItem(imageItem); //m_ImageList->setItemWidget(imageItem, this);}/*QGridLayout* grid = new QGridLayout;grid->addWidget(m_ImageList);this->setLayout(grid);*/m_ImageList->setHidden(false);
}QStringList QDockWidgetDemo::GetFileNames(const QFileInfoList& fileInfoList)
{QStringList strLstNames;int nFileNum = fileInfoList.size();for (int n = 0; n < nFileNum; n++){QFileInfo fileInfo = fileInfoList[n];strLstNames << fileInfo.fileName();}return strLstNames;
}QStringList QDockWidgetDemo::getFolderFiles(const QString& path)
{QDir dir(path);QStringList tempList;QFileInfoList fileList = dir.entryInfoList(QDir::Files | QDir::AllDirs | QDir::NoSymLinks | QDir::NoDotAndDotDot);foreach(QFileInfo fileInfo, fileList){if (fileInfo.isDir()){getFolderFiles(fileInfo.absoluteFilePath());}else{tempList.append(fileInfo.absoluteFilePath());} }return tempList;
}QStringList QDockWidgetDemo::fliterJPG(const QString& path)
{QDir dir(path);QStringList tempList;QStringList filterJpgFiles;QFileInfoList fileList = dir.entryInfoList(QDir::Files | QDir::AllDirs | QDir::NoSymLinks | QDir::NoDotAndDotDot);foreach(QFileInfo fileInfo, fileList){if (fileInfo.isDir()){getFolderFiles(fileInfo.absoluteFilePath());}else{tempList.append(fileInfo.absoluteFilePath());}}foreach(QString item, tempList){if (item.contains(".jpg")){filterJpgFiles.append(item);} }return filterJpgFiles;
}
2.左边图片目录展示界面
1.图片目录头文件
#pragma once#include <QWidget>
#include <QTreeWidget>
#include <QFileSystemWatcher>
#include <QListWidget>
#include "FileMonitorMgr.h"
#include "ui_projectWin.h"class QFileSystemWatcher;class projectWin : public QWidget
{Q_OBJECTpublic:projectWin(QWidget* parent = Q_NULLPTR);~projectWin();
public:FileMonitorMgr *m_fileMgr;void showImgList(QStringList& imgList);private:void initWidget();void initTree();public Q_SLOTS:void slotClicked(QTreeWidget* item, int column);void slotMenuPup(const QPoint* point);void slotTree(QStringList lst, QString path);//void slotShowImgs(QStringList lst, QString path);private:Ui::projectWin ui;QTreeWidget* m_tree;QMenu* m_menu;//QListWidget* m_ImageList;};
2.图片目录实现文件
#include "projectWin.h"#include <QStandardPaths>
#include <QDir>
#include <QDebug>
#include <QVBoxLayout>
#include <QGridLayout>#pragma execution_character_set("utf-8")const QString styles = "QTreeView\
{\background-color: #5B677A;\font-size:17px;\color: white;\
}\
QTreeView::item:hover\
{\background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1);\border: 1px solid #bfcde4;\
}\
QTreeView::item:hover\
{\background: rgb(69, 187, 217);\
}\
QTreeView::item:selected:active\
{\background: rgb(255, 62, 150);\
}\
QTreeView::item:selected:!active\
{\background: rgb(63, 147, 168);\
}\
QTreeView::branch\
{\background:#5B677A;\
}\
QTreeView::branch:has-children:!has-siblings:closed,QTreeView::branch:closed:has-children:has-siblings\
{\border-image: none;\background:#5B677A;\image: url(image/Folder-1.png);\
}\
QTreeView::branch:open:has-children:!has-siblings,QTreeView::branch:open:has-children:has-siblings\
{\border-image: none;\background:#5B677A;\image: url(image/Open-Folder.png);\
}";projectWin::projectWin(QWidget* parent) : QWidget(parent)
{ui.setupUi(this);initWidget();
}projectWin::~projectWin() {}void projectWin::showImgList(QStringList& imgList)
{}void projectWin::initWidget()
{//m_ImageList = new QListWidget;m_ImageList->setViewMode(QListWidget::IconMode); //显示模式//m_ImageList->setIconSize(QSize(100, 100));//设置图片大小//m_ImageList->setSpacing(1);//间距//m_ImageList->setResizeMode(QListWidget::Adjust); //适应布局调整//m_ImageList->setMovement(QListWidget::Free); //可移动,static:不可移动//m_ImageList->setWrapping(true); //自动换行//m_ImageList->setFlow(QListWidget::LeftToRight); //从左到右布局}void projectWin::initTree()
{//m_fileMgr = new FileMonitorMgr("F:/QtExercise/QDockWidgetDemo/QDockWidgetDemo/Monitor");//m_tree = new QTreeWidget();打开右键菜单属性//m_tree->setContextMenuPolicy(Qt::CustomContextMenu);添加顶层节点//if (m_fileMgr->m_allFileList.size() > 0)//{// int count = m_fileMgr->m_allFileList.size();// for (int i = 0; i < count; i++)// {// QTreeWidgetItem* topItem = new QTreeWidgetItem(m_tree);// topItem->setText(i, m_fileMgr->m_allFileList.at(i));// topItem->setCheckState(i, Qt::Checked);// m_tree->addTopLevelItem(topItem);// }//}//m_tree->expandAll();//m_tree->setStyleSheet(styles);
}void projectWin::slotClicked(QTreeWidget* item, int column)
{}void projectWin::slotMenuPup(const QPoint* point)
{}void projectWin::slotTree(QStringList lst, QString path)
{//m_fileMgr = new FileMonitorMgr("F:/QtExercise/QDockWidgetDemo/QDockWidgetDemo/Monitor");m_tree = new QTreeWidget();//打开右键菜单属性m_tree->setContextMenuPolicy(Qt::CustomContextMenu);QTreeWidgetItem* topItem = new QTreeWidgetItem(m_tree);topItem->setText(0, path);topItem->setCheckState(0, Qt::Checked);m_tree->addTopLevelItem(topItem);int count = lst.size();//添加顶层节点if (lst.size() > 0){for (int i = 0; i < lst.size(); i++){int size = lst.at(i).size();int pos = lst.at(i).lastIndexOf("/");QString temp = lst.at(i).right(size - pos - 1);QTreeWidgetItem* item = new QTreeWidgetItem(topItem);item->setText(0, temp);item->setCheckState(0, Qt::Checked);}}QVBoxLayout* layout = new QVBoxLayout();m_tree->expandAll();m_tree->setStyleSheet(styles);layout->addWidget(m_tree);this->setLayout(layout);
}//void projectWin::slotShowImgs(QStringList lst, QString path)
//{
// QString allPath = path + "/";
// for (int i=0; i<lst.size(); i++)
// {
// QListWidgetItem* imageItem = new QListWidgetItem;
// QString allImgFiles = allPath + lst.at(i);
// imageItem->setIcon(QIcon(lst.at(i)));
// imageItem->setText(lst.at(i));
// imageItem->setSizeHint(QSize(120, 100));
// m_ImageList->addItem(imageItem);
// }
//
// QGridLayout* grid = new QGridLayout;
// grid->addWidget(m_ImageList);
// this->setLayout(grid);
//}
2.属性窗口区
1.属性窗口头文件
#pragma once#include <QWidget>#include "ui_propertyWin.h"class propertyWin : public QWidget
{Q_OBJECTpublic:propertyWin(QWidget* parent = Q_NULLPTR);~propertyWin();private:void initWidget();private:Ui::propertyWin ui;
};
2.属性窗口实现文件
#include "propertyWin.h"#include <QStandardPaths>
#include <QDir>
#include <QDebug>#pragma execution_character_set("utf-8")propertyWin::propertyWin(QWidget* parent) : QWidget(parent)
{ui.setupUi(this);initWidget();
}propertyWin::~propertyWin() {}void propertyWin::initWidget() {}
3 源码下载
源码下载地址