#include "projectIconSelectionDlg.h" #include #include #include ProjectIconSelectionDlg::ProjectIconSelectionDlg(const QMap mapSvg,QWidget* parent) : QDialog(parent),svgMap(mapSvg) { this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint| windowFlags()); setWindowTitle("选择SVG图标"); resize(400, 300); QVBoxLayout* layout = new QVBoxLayout(this); listWidget = new QListWidget(this); listWidget->setIconSize(QSize(32, 32)); listWidget->setViewMode(QListWidget::IconMode); listWidget->setResizeMode(QListWidget::Adjust); listWidget->setStyleSheet( "QListWidget::item:selected {" " background: #4e72b8;" " color: palette(HighlightedText);" "}" ); for (auto& svgData : svgMap) { QSvgRenderer renderer(svgData); QPixmap pixmap(32, 32); pixmap.fill(Qt::white); QPainter painter(&pixmap); renderer.render(&painter); QListWidgetItem* item = new QListWidgetItem(QIcon(pixmap), ""); item->setData(Qt::UserRole, svgData); listWidget->addItem(item); } QDialogButtonBox* buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); layout->addWidget(listWidget); layout->addWidget(buttonBox); connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); } QByteArray ProjectIconSelectionDlg::selectedSVG() const { if (listWidget->currentItem()) { return listWidget->currentItem()->data(Qt::UserRole).toByteArray(); } return QByteArray(); }