106 lines
2.4 KiB
C++
106 lines
2.4 KiB
C++
#include "ComboBoxItem.h"
|
|||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 构造函数
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="parent">父类</param>
|
||
|
|
ComboBoxItem::ComboBoxItem(QWidget* parent) : QWidget(parent)
|
||
|
|
, m_mousePress(false)
|
||
|
|
, painter(nullptr)
|
||
|
|
{
|
||
|
|
m_label = new QLabel(this);
|
||
|
|
m_button = new QPushButton(this);
|
||
|
|
|
||
|
|
// 修复:正确创建和绘制叉号图标
|
||
|
|
QPixmap pixmap(16, 16);
|
||
|
|
pixmap.fill(Qt::transparent);
|
||
|
|
|
||
|
|
QPainter painter(&pixmap);
|
||
|
|
painter.setRenderHint(QPainter::Antialiasing, true);
|
||
|
|
QPen pen(Qt::black);
|
||
|
|
pen.setWidth(2);
|
||
|
|
painter.setPen(pen);
|
||
|
|
|
||
|
|
// 绘制叉号
|
||
|
|
painter.drawLine(4, 4, 12, 12);
|
||
|
|
painter.drawLine(12, 4, 4, 12);
|
||
|
|
painter.end();
|
||
|
|
|
||
|
|
// 设置按钮图片
|
||
|
|
m_button->setIcon(QIcon(pixmap));
|
||
|
|
m_button->setIconSize(QSize(12, 12));
|
||
|
|
m_button->setFixedSize(20, 20);
|
||
|
|
// 设置风格
|
||
|
|
m_button->setStyleSheet("QPushButton { border: none; background: transparent; }"
|
||
|
|
"QPushButton:hover { background: rgba(200, 200, 200, 100); border-radius: 3px; }");
|
||
|
|
|
||
|
|
// 点击事件
|
||
|
|
connect(m_button, &QPushButton::clicked, [this] {
|
||
|
|
// 删除选项
|
||
|
|
emit signalRemoveClicked(m_label->text());
|
||
|
|
// 删除属性
|
||
|
|
emit signalRemoveProperty(property("count").toString());
|
||
|
|
});
|
||
|
|
|
||
|
|
// 水平布局
|
||
|
|
QHBoxLayout* layout = new QHBoxLayout(this);
|
||
|
|
layout->setContentsMargins(5, 2, 5, 2);
|
||
|
|
layout->setSpacing(5);
|
||
|
|
|
||
|
|
// 标签
|
||
|
|
layout->addWidget(m_label);
|
||
|
|
// 弹簧
|
||
|
|
layout->addStretch();
|
||
|
|
// 按钮
|
||
|
|
layout->addWidget(m_button);
|
||
|
|
|
||
|
|
setLayout(layout);
|
||
|
|
setFixedHeight(25); // 修复:固定高度
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 设置项目文本
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="text">文本</param>
|
||
|
|
void ComboBoxItem::setLabelText(QString text)
|
||
|
|
{
|
||
|
|
m_label->setText(text);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 获取当前项目文本
|
||
|
|
/// </summary>
|
||
|
|
/// <returns>文本</returns>
|
||
|
|
QString ComboBoxItem::getLabelText()
|
||
|
|
{
|
||
|
|
return m_label->text();
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 鼠标按下事件
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="event">事件</param>
|
||
|
|
void ComboBoxItem::mousePressEvent(QMouseEvent* event)
|
||
|
|
{
|
||
|
|
if (event->button() == Qt::LeftButton)
|
||
|
|
{
|
||
|
|
m_mousePress = true;
|
||
|
|
}
|
||
|
|
QWidget::mousePressEvent(event); // 修复:调用父类事件
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 鼠标释放事件
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="event">事件</param>
|
||
|
|
void ComboBoxItem::mouseReleaseEvent(QMouseEvent* event)
|
||
|
|
{
|
||
|
|
if (m_mousePress && rect().contains(event->pos()))
|
||
|
|
{
|
||
|
|
// 发送信号
|
||
|
|
qDebug() << "按下:" << m_label->text();
|
||
|
|
emit signalShowClicked(m_label->text());
|
||
|
|
}
|
||
|
|
m_mousePress = false;
|
||
|
|
QWidget::mouseReleaseEvent(event); // 修复:调用父类事件
|
||
|
|
}
|