QT使用QListWidget显示多张图片

news2024/9/23 11:13:24

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_OBJECT

public:
	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 names
	QFileInfoList 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 request
		QString 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_OBJECT

public:
	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_OBJECT

public:
	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 源码下载

源码下载地址

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/400802.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【安卓软件】KMPlayer-一款完美的媒体播放器 可以播放所有格式的字幕和视频

KM PlayerKM Player是一款未编码的视频播放器&#xff0c;让您无需编码即可方便地播放各种格式的视频&#xff0c;并为您的新体验添加了字幕支持、视频播放速度和手势等功能。KMPlayer 拥有美观和直观的设计&#xff0c;让您可以更方便地管理和播放视频&#xff01;功能高品质视…

电流分段校准原理与步骤

分段电流校准是一种校准电流传感器的方法。 传感器不可避免地存在误差&#xff0c;这些误差可能来自于不同方面&#xff0c;例如温度漂移、零点漂移和灵敏度漂移等。 在精确测量电流的应用中&#xff0c;这些误差可能会导致测量结果不准确。 因此&#xff0c;对电流传感器进行校…

100种思维模型之认知资源思思维模型-030

我们常说&#xff0c;一个人永远也赚不到自己认知以外的钱&#xff0c;这话的确很有道理&#xff0c;被无数人所推崇。 由此&#xff0c;不难看出&#xff0c;认知在我们的生活起着多么关键的作用。 你的认知层次越高&#xff0c;范围越广&#xff0c;就意味着你这个人所处的阶…

vue - vue使用腾讯api进行定位获取,绘制地图、标点、搜索、路线规划

章节目录1&#xff0c;前言2&#xff0c;准备3&#xff0c;开始引入api4&#xff0c;vue组件中使用 - 获取定位5&#xff0c;绘制地图和标点6&#xff0c;关键字搜索功能7&#xff0c;驾车线路规划8&#xff0c;演示组件中的全部代码9&#xff0c;参考链接1&#xff0c;前言 首…

Vue项目本地开发集成引入https

问题描述 本地项目开发中用到的接口是https &#xff0c;本地http会请求不到数据 案例使用采用的vue-cli开发&#xff0c;所以需要针对这两种方式启动https 问题处理 1.首先是需要配置一个证书,使用mkcert 进行配置证书 2.在vue-cli 中进行修改package.json、vue.config.js 中进…

Leetcode.828 统计子串中的唯一字符

题目链接 Leetcode.828 统计子串中的唯一字符 Rating &#xff1a; 2034 题目描述 我们定义了一个函数 countUniqueChars(s)来统计字符串 s中的唯一字符&#xff0c;并返回唯一字符的个数。 例如&#xff1a;s "LEETCODE"&#xff0c;则其中 "L", "…

【Mysql】查询数据库,行转列,mapper.xml中查询条件的写法

目录 一、用mysql脚本建表二、现有以下三个实体对应三张表&#xff0c;其关联关系如下三、行转列的sql语句四、对应的mapper.xml写法五、输入某一关键字&#xff0c;查找车牌号或车名包含该关键字的车辆用or六、总结&#xff1a;用GROUP_CONCAT实现行转列一、用mysql脚本建表 …

compose系列教程-6.实现图文列表,添加点击事件

每个行添加点击事件&#xff0c;可以使用Clickable组件。在Clickable组件的onClick参数中&#xff0c;您可以指定要在用户单击行时执行的操作。下面是一个示例代码&#xff1a; Composable fun ImageTextList(imageTextList: List<ImageTextItem>, onItemClick: (ImageTe…

机器学习学习记录1:基本术语和假设空间

基本术语机器学习正是这样一门学科&#xff0c;它致力于研究如何通过计算的手段&#xff0c;利用经 验来玫善系统自身的性能在计算机系统中&#xff0c;"经验"通常以"数据"形式存 在&#xff0c;因此&#xff0c;机器学习所研究的主要内容&#xff0c;是关…

数据仓库的设计思想

数据仓库设计 知识点01&#xff1a;设计大纲与学习目标 #内容大纲1、数据仓库基础知识&#xff08;回顾&#xff09;什么是数仓为什么有数仓数仓的特点是什么OLTP和OLAP系统区别&#xff08;数据库和数仓的区别&#xff09;2、数仓系统的架构与核心流程核心1&#xff1a;ETL核…

mybatis(二)

mybatis练习---2种方式 能够使用映射配置文件实现CRUD操作 能够使用注解实现CRUD操作 配置文件CRUD就是把sql语句写到配置文件中&#xff0c;注解CRUD就是吧sql语句写到注解上。 一、配置文件实现CRUD 如上图所示产品原型&#xff0c;里面包含了品牌数据的 查询 、 按条件查…

使用ControlNet 控制 Stable Diffusion

本文将要介绍整合HuggingFace的diffusers 包和ControlNet调节生成文本到图像&#xff0c;可以更好地控制文本到图像的生成 ControlNet是一种通过添加额外条件来控制扩散模型的神经网络结构。它提供了一种增强稳定扩散的方法&#xff0c;在文本到图像生成过程中使用条件输入&…

【工具使用】STM32CubeMX-基础使用篇

一、概述 无论是新手还是大佬&#xff0c;基于STM32单片机的开发&#xff0c;使用STM32CubeMX都是可以极大提升开发效率的&#xff0c;并且其界面化的开发&#xff0c;也大大降低了新手对STM32单片机的开发门槛。     本文主要面向初次接触STM32CubeMX的同学&#xff0c;大…

垃圾回收:垃圾数据如何自动回收

有些数据被使用之后&#xff0c;可能就不再需要了&#xff0c;我们把这种数据称为垃圾数据。如果这些垃圾数据一直保存在内存中&#xff0c;那么内存会越用越多&#xff0c;所以我们需要对这些垃圾数据进行回收&#xff0c;以释放有限的内存空间 不同语言的垃圾回收策略 通常…

「中华田园敏捷开发」,是老板无能还是程序员无力?

敏捷开发一直都是无数程序员的追求&#xff0c;也被被视为“开发者的福音”&#xff0c;但显然敏捷开发在中国落地的专业度还不够&#xff0c;以至于出现了“中华田园敏捷”的说法&#xff0c;什么叫“中华田园敏捷开发”&#xff1f; 简单点说&#xff1a;中华田园敏捷开发的…

异常(C++)

文章目录1. 概念1.1 C语言处理错误机制1.2 C异常机制throw表达式try...catch语句例子2. 抛出异常2.1 栈展开栈展开的例子2.2 栈展开过程中对象被自动销毁2.3 析构函数与异常内存泄漏2.4 异常对象3. 捕获异常3.1 捕获子类异常3.2 异常的重新抛出4. 异常安全4.2 例子不抛出异常保…

VIT(vision transformer)onnx模型解析

背景&#xff1a;transformer在CV领域的应用论文下载链接&#xff1a;https://arxiv.org/abs/2010.11929Pytorch实现代码&#xff1a; pytorch_classification/vision_transformer(太阳花的小绿豆博主实现的代码)有一些大神在研究关于CNNtransformer或者纯用transformer实现。原…

北邮22信通:你是不是在looking for……那串代码?(2)第三章单链表

相信有了第二章顺序表的基础&#xff0c;小伙伴们学习第三章链表应该会轻松一点吧 目录 类模板下的单链表 1.1书上干净完整代码&#xff08;无增改、适合自己动手实验&#xff09; 1.2对书上代码的完善和对一些问题的验证和解释代码 1.补全一个函数&#xff1a; 2.this指…

荧光染料IR 825叠氮IR825 N3,IR-825 azide,IR-825叠氮 科研试剂

产品描述&#xff1a;IR-825 N3含有叠氮基团&#xff0c;IR-825是一种近红外染料&#xff08;NIR&#xff09;&#xff0c;IR-825在封装成纳米颗粒后&#xff0c;可能用于cancer光热和光动力 。叠氮化物基团可以参与铜催化的与炔部分的点击化学反应。西安凯新生物科技有限公司近…

基于多任务融合的圣女果采摘识别算法研究

基于多任务融合的圣女果采摘识别算法研究 1、简介 本文主要解决圣女果生产销售环节中&#xff0c;现有的流程是采摘成熟的圣女果&#xff0c;再对采摘下的果实进行单独的品质分级&#xff0c;不仅费时费力&#xff0c;而且多增加一个环节&#xff0c;也增加了对果实的二次伤害…