143 lines
3.8 KiB
C++
143 lines
3.8 KiB
C++
#include "card_list_widget.h"
|
|
#include <QPainter>
|
|
#include <QApplication>
|
|
#include <QVBoxLayout>
|
|
|
|
CardItemDelegate::CardItemDelegate(QObject* parent)
|
|
: QStyledItemDelegate(parent)
|
|
{
|
|
}
|
|
|
|
void CardItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
|
|
const QModelIndex& index) const
|
|
{
|
|
painter->save();
|
|
|
|
// 获取数据
|
|
CardData data = index.data(Qt::UserRole).value<CardData>();
|
|
|
|
// 绘制卡片背景
|
|
QRect rect = option.rect;
|
|
rect.adjust(5, 5, -5, -5); // 留出边距
|
|
|
|
// 如果被选中,绘制不同的背景色
|
|
if (option.state & QStyle::State_Selected) {
|
|
painter->fillRect(rect, QColor(230, 230, 255));
|
|
} else {
|
|
painter->fillRect(rect, Qt::white);
|
|
}
|
|
|
|
// 绘制卡片边框
|
|
painter->setPen(QPen(Qt::lightGray));
|
|
painter->drawRect(rect);
|
|
|
|
// 设置字体
|
|
QFont nameFont = painter->font();
|
|
nameFont.setBold(true);
|
|
nameFont.setPointSize(10);
|
|
|
|
QFont normalFont = painter->font();
|
|
normalFont.setPointSize(9);
|
|
|
|
// 计算文本区域
|
|
int padding = 10;
|
|
QRect nameRect = rect.adjusted(padding, padding, -padding, 0);
|
|
nameRect.setHeight(25);
|
|
|
|
QRect dateRect = nameRect;
|
|
dateRect.translate(0, nameRect.height());
|
|
|
|
QRect descRect = dateRect;
|
|
descRect.translate(0, dateRect.height());
|
|
descRect.setHeight(40);
|
|
|
|
// 绘制文本
|
|
painter->setFont(nameFont);
|
|
painter->setPen(Qt::black);
|
|
painter->drawText(nameRect, Qt::AlignLeft | Qt::AlignVCenter, data.name);
|
|
|
|
painter->setFont(normalFont);
|
|
painter->setPen(Qt::darkGray);
|
|
painter->drawText(dateRect, Qt::AlignLeft | Qt::AlignVCenter, data.date);
|
|
|
|
// 绘制描述文本,支持自动换行
|
|
painter->drawText(descRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap,
|
|
data.description);
|
|
|
|
painter->restore();
|
|
}
|
|
|
|
QSize CardItemDelegate::sizeHint(const QStyleOptionViewItem& option,
|
|
const QModelIndex& index) const
|
|
{
|
|
Q_UNUSED(option);
|
|
Q_UNUSED(index);
|
|
return QSize(300, 100); // 卡片固定大小
|
|
}
|
|
|
|
MainWindow::MainWindow(QWidget* parent)
|
|
: QWidget(parent)
|
|
{
|
|
setWindowTitle(tr("卡片列表示例"));
|
|
resize(400, 600);
|
|
|
|
// 创建布局
|
|
auto layout = new QVBoxLayout(this);
|
|
|
|
// 创建列表控件
|
|
listWidget = new QListWidget(this);
|
|
listWidget->setSpacing(5); // 设置卡片间距
|
|
listWidget->setViewMode(QListView::ListMode);
|
|
listWidget->setItemDelegate(new CardItemDelegate(listWidget));
|
|
|
|
// 设置样式
|
|
listWidget->setStyleSheet(
|
|
"QListWidget {"
|
|
" background-color: #f0f0f0;"
|
|
" border: none;"
|
|
"}"
|
|
"QListWidget::item {"
|
|
" background-color: transparent;"
|
|
"}"
|
|
"QListWidget::item:selected {"
|
|
" background-color: transparent;"
|
|
"}"
|
|
);
|
|
|
|
layout->addWidget(listWidget);
|
|
|
|
// 添加示例数据
|
|
for (int i = 1; i <= 10; ++i) {
|
|
CardData data{
|
|
QString(tr("项目 %1")).arg(i),
|
|
QDate::currentDate().toString("yyyy-MM-dd"),
|
|
QString(tr("这是项目 %1 的详细描述信息,可以包含多行文本内容。")).arg(i)
|
|
};
|
|
addCardItem(data);
|
|
}
|
|
}
|
|
|
|
void MainWindow::addCardItem(const CardData& data)
|
|
{
|
|
QListWidgetItem* item = new QListWidgetItem(listWidget);
|
|
item->setData(Qt::UserRole, QVariant::fromValue(data));
|
|
listWidget->addItem(item);
|
|
}
|
|
|
|
// 注册自定义数据类型
|
|
Q_DECLARE_METATYPE(CardData)
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
QApplication app(argc, argv);
|
|
|
|
// 设置编码和字体
|
|
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
|
|
QFont font("Microsoft YaHei");
|
|
app.setFont(font);
|
|
|
|
MainWindow window;
|
|
window.show();
|
|
|
|
return app.exec();
|
|
}
|