1、创建Qt项目
2、设计Qt界面
3、编写代码
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
#include
#include
#include
#include
#include
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
void onRadioButtonClicked();
void onItemClicked(QListWidgetItem* item);
void onItemDoubleClicked(QListWidgetItem* item);
private slots:
void onBtnAddClicked();
void onBtnDeleteClicked();
//Files list
// int getFiles(const QString& dirName);
private:
Ui::Widget *ui;
QButtonGroup* mBtnGroup;
QList fileAllList;
QList fileNameAllList;
bool strOmit = false;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include
QStringList iconStringList = {
":/images/apple.png",
":/images/banana.png",
":/images/orange.png",
":/images/peach.png",
":/images/strawberry.png",
};
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//设置显示模式
mBtnGroup = new QButtonGroup(this);
mBtnGroup->addButton(ui->rbListMode,0);
mBtnGroup->addButton(ui->rbIconMode,1);
connect(ui->rbListMode,&QRadioButton::clicked,this,&Widget::onRadioButtonClicked);
connect(ui->rbIconMode,&QRadioButton::clicked,this,&Widget::onRadioButtonClicked);
ui->rbListMode->setChecked(true);
ui->videoListWidget->setViewMode(QListView::ListMode);
//单击和双击信号槽
connect(ui->videoListWidget,&QListWidget::itemClicked,this,&Widget::onItemClicked);
connect(ui->videoListWidget,&QListWidget::itemDoubleClicked,this,&Widget::onItemDoubleClicked);
//add/delete
connect(ui->btnAdd,&QPushButton::clicked,this,&Widget::onBtnAddClicked);
connect(ui->btnDelete,&QPushButton::clicked,this,&Widget::onBtnDeleteClicked);
//不显示行向滚动条,子项文本过长自动显示...
ui->videoListWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->videoListWidget->setStyleSheet("QScrollBar{width:10px;background:#0A1626;}");
}
Widget::~Widget()
{
delete ui;
}
void Widget::onRadioButtonClicked()
{
int checkedId = mBtnGroup->checkedId();
if(checkedId == 0){
//列表模式
ui->videoListWidget->setViewMode(QListView::ListMode);
}else if(checkedId == 1){
//图标模式
ui->videoListWidget->setViewMode(QListView::IconMode); //设置为自适应
ui->videoListWidget->setResizeMode(QListView::Adjust); //设置为自适应
ui->videoListWidget->setMovement(QListView::Static); //设置为不可拖动
ui->videoListWidget->setUniformItemSizes(true);
//ui->videoListWidget->setGridSize(QSize(40, 40));
ui->videoListWidget->setWordWrap(true);
//超出文本不显示省略号
// ui->videoListWidget->setTextElideMode(Qt::ElideNone);
}
}
//单击信号槽的时候获取文本
void Widget::onItemClicked(QListWidgetItem *item)
{
//ui->selectVideoFile->setText(item->text());
QString mStr = item->text();
ui->selectVideoFile->setText(mStr);
QFontMetrics fontMetrics(ui->selectVideoFile->font());
int fontSize = fontMetrics.width(mStr);
if (fontSize > ui->selectVideoFile->width()) {
//省略左边的内容(Qt::ElideLeft) 省略中间的内容(Qt::ElideMiddle) 省略中间的内容(Qt::ElideRight)
ui->selectVideoFile->setText(fontMetrics.elidedText(mStr, Qt::ElideMiddle, ui->selectVideoFile->width()));
ui->selectVideoFile->setToolTip(mStr);
} else {
ui->selectVideoFile->setText(mStr);
ui->selectVideoFile->setToolTip("");
}
}
//双击信号槽的时候变成可编辑的状态
void Widget::onItemDoubleClicked(QListWidgetItem *item)
{
item->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled);
}
//添加VideoItem
void Widget::onBtnAddClicked()
{
int iconIndex = QRandomGenerator::global()->generate()%5;//获取0-4的随机数
QIcon icon(iconStringList[iconIndex]);
QString strCurrentPath = QDir::homePath();//获取系统当前目录
QString strDlgTitle = "请选择视频文件";//打开文件对话框标题
QString strFilter = "MP4 Files(*.mp4);;WMV(*.wmv);;AVI(*.avi);;All Files(*.*)";//打开视频文件过滤器
QList fileList = QFileDialog::getOpenFileNames(this, strDlgTitle, strCurrentPath, strFilter);
for (int i= 0;i fileNameList = fileList[i].split(".mp4")[0].split("/");
qDebug()<videoListWidget->addItem(item);
/************************ Qt QListWidget项文本过长显示处理 ************************/
//根据长度判断是否设置ToolTip
QFontMetrics fontMetrics(item->font());
if (ui->videoListWidget->width() < fontMetrics.width(text)){
item->setToolTip(text);
text =fontMetrics.elidedText(text, Qt::ElideMiddle, ui->selectVideoFile->width());
}
item->setText(text);
item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
ui->videoListWidget->setIconSize(QSize(30, 30));
ui->videoListWidget->addItem(item);
// int currentRow = fileAllList.length() - ui->videoListWidget->currentRow();
// qDebug()<videoListWidget->currentRow();
// qDebug()<videoListWidget->insertItem(currentRow,item);
}
}
qDebug()<<"fileList:"<videoListWidget->currentRow();
qDebug()<<"删除第几行:"<videoListWidget->takeItem(row);
delete item;
ui->selectVideoFile->setText("");
fileAllList.removeAt(row);
qDebug()<<"fileAllList:"<
添加文件: 查看ListMode模式和IconMode模式
选中文件,查看文件,删除文件
喜欢的话,点个赞赞+收藏❤,您的鼓励是我不断创作的动力!!!!!!!!!!!!!!