#include "linetype.h"
/* combox的下拉框显示的项 */
LineStyleItem::LineStyleItem(LabelStyleType type, Qt::PenStyle style, QWidget *parent)
:QLabel(parent)
,m_style(style)
,m_type(type)
,m_pen(QColor(161, 160, 160))
{
}
LineStyleItem::~LineStyleItem()
{
}
//设置样式
void LineStyleItem::setStyle(Qt::PenStyle style)
{
m_style = style;
}
void LineStyleItem::setLinePen(QColor color)
{
m_pen = color;
}
//重写paint
void LineStyleItem::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QSize size = this->size();
QPainter painter(this);
QPen pen;
pen.setColor(m_pen);
pen.setWidth(2);
pen.setStyle(m_style);
/*switch (m_style)
{
case SolidLine:
pen.setStyle(Qt::SolidLine);
break;
case DashLine:
pen.setStyle(Qt::DashLine);
break;
case DotLine:
pen.setStyle(Qt::DotLine);
break;
case DashDotLine:
pen.setStyle(Qt::DashDotLine);
break;
case DashDotDotLine:
pen.setStyle(Qt::DashDotDotLine);
break;
default:
break;
}*/
painter.setPen(pen);
QPoint p1(5, size.height() / 2);
QPoint p2(size.width() - 5, size.height() / 2);
QLine line(p1, p2);
painter.drawLine(line);
}
//重写 点击发送信号(宽度)
void LineStyleItem::mousePressEvent(QMouseEvent * event)
{
//当点击下拉框的item
if (m_type == EMITEM)
{
emit click(m_style);
}
QLabel::mousePressEvent(event);
}
/* 自定义的LineEdit类,用于显示combox */
ShowLineStyleInEdit::ShowLineStyleInEdit(Qt::PenStyle style, QWidget *parent)
:QLineEdit(parent),m_style(style)
{
m_label = new LineStyleItem(EMLINEEDIT, style, this);
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(m_label);
//layout->setMargin(0);
layout->setContentsMargins(5, 0, 0, 3);//左 上 右 下 边距
setLayout(layout);
setReadOnly(true);//设置只读
}
ShowLineStyleInEdit::~ShowLineStyleInEdit()
{
}
void ShowLineStyleInEdit::setStyle(Qt::PenStyle style)
{
m_label->setStyle(style);
}
void ShowLineStyleInEdit::setLineItemColor(QColor color)
{
m_label->setLinePen(color);
}
/* 自定义ComboBox */
LineStyleBox::LineStyleBox(QWidget *parent)
:QComboBox(parent)
{
//设置初值
//m_index = 0;
m_listWidget = new QListWidget();
setContextMenuPolicy(Qt::NoContextMenu);//禁用菜单
m_listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//禁用垂直滚动条
// m_listWidget->setStyleSheet("QListWidget::Item:hover{background-color:rgb(85, 170, 255);}");
//设置显示模式
setModel(m_listWidget->model());
//设置组合框弹出窗口的视图 listWidget
setView(m_listWidget);
//设置当前comboBox的显示的lineEdit
m_showLineInEdit = new ShowLineStyleInEdit(Qt::SolidLine, this);
setLineEdit(m_showLineInEdit);
//默认当前选择为0
setCurrentIndex(0);
}
LineStyleBox::~LineStyleBox()
{
}
//添加组合框下拉的项
void LineStyleBox::appendItem(Qt::PenStyle style)
{
//创建自定义的widget label
LineStyleItem *lineItem = new LineStyleItem(EMITEM, style, this);
connect(lineItem, &LineStyleItem::click, this, &LineStyleBox::slotOnclickLineItem);
//创建显示的listWidgetItem
QListWidgetItem *item = new QListWidgetItem(m_listWidget);
//加入listItem到list
m_listWidget->addItem(item);
//加入自定义的label到listItem中显示
m_listWidget->setItemWidget(item, lineItem);
}
//点击下拉框的其中一项时
void LineStyleBox::slotOnclickLineItem(Qt::PenStyle style)
{
m_style = style;
//设置当前显示的项
m_showLineInEdit->setStyle(style);
//设置当前栏为选中项
setCurrentSelStyle(style);
//隐藏下拉栏
hidePopup();
}
void LineStyleBox::setCurrentSelStyle(Qt::PenStyle style)
{
m_style = style;
emit sigPenStyle(m_style);
/*switch (m_style)
{
case QtSolidLine:
emit sigPenStyle(Qt::SolidLine);
break;
case DashLine:
emit sigPenStyle(Qt::DashLine);
break;
case DotLine:
emit sigPenStyle(Qt::DotLine);
break;
case DashDotLine:
emit sigPenStyle(Qt::DashDotLine);
break;
case DashDotDotLine:
emit sigPenStyle(Qt::DashDotDotLine);
break;
default:
break;
}*/
}
void LineStyleBox::setCurrentShowStyle(Qt::PenStyle style)
{
m_showLineInEdit->setStyle(style);
}
void LineStyleBox::setLineEditColor(QColor color)
{
m_showLineInEdit->setLineItemColor(color);
}
mengzhi啊
- 粉丝: 759
- 资源: 33
最新资源
- 【java毕业设计】springboot智能健康饮食系统(springboot+vue+mysql+说明文档).zip
- 【java毕业设计】springboot基于java的网上订餐系统(springboot+vue+mysql+说明文档).zip
- 2633996324RUSH_TANK_RACE_SA2.1_48CH.json
- 【java毕业设计】springbootJava区社区停车信息管理系统(springboot+mysql+说明文档).zip
- mobilenetv2-12.onnx
- 2024最新股票系统源码 附详细教程
- 2048游戏(附源码)
- 4%2Fpf%2Ffiles%2FKKDuiZhanDownloader-v1.0.1.409.exe
- 基于 java 实现Android手机的摄像头,通过闪光灯识别手指的血管,完成心率的检测,绘制出心率图
- ACM-ICPC有效训练方法及算法学习心得分享
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈