QCombobox下拉選擇線寬
阿新 • • 發佈:2020-12-12
技術標籤:Qt
效果圖
程式碼
h檔案
#ifndef CLINEWIDTH_H
#define CLINEWIDTH_H
#include <QObject>
#include <QLabel>
#include <QComboBox>
#include <QListWidget>
#include <QLineEdit>
enum EmLabelType{emItem = 0, emLineEdit};//label型別,0:下拉框的item,1:combobox的編輯框
class CLineWidthItem : public QLabel
{
Q_OBJECT
public:
CLineWidthItem( EmLabelType type = emItem,int width = 0,QWidget *parent = nullptr);
~CLineWidthItem() override;
void setWidth(const int & width);
protected:
void paintEvent(QPaintEvent *) override;
void mousePressEvent(QMouseEvent *event) override;
private:
int m_width;//線條寬度
EmLabelType m_type;//label型別
signals:
void click(const int & width);
void sigMouseMove();
};
class QShowLineInEdit : public QLineEdit
{
Q_OBJECT
public:
QShowLineInEdit(const int & width = 0,QWidget *parent = nullptr);
~QShowLineInEdit()override;
void setWidth(const int & width);
private:
CLineWidthItem * m_pLabel;
int m_width;//線條寬度
};
class CLineWidthBox :public QComboBox
{
Q_OBJECT
public:
CLineWidthBox(QWidget *parent = Q_NULLPTR);
~CLineWidthBox()override;
int GetCurrentIndex();
void SetCurrentIndex(int idx);
private slots:
void onClickPenWidget(const int& index);
public:
void appendItem(const int& index);
private:
QListWidget* m_pListWidget;
int m_index;
QShowLineInEdit *m_pShowLineInEdit;
};
#endif // CPENCSSEDITOR_H
cpp檔案
#include "clinewidth.h"
#include <QPainter>
#include <QHBoxLayout>
//-- class_1 下拉框中的item
CLineWidthItem::CLineWidthItem(EmLabelType type,int width,QWidget *parent)
:QLabel(parent)
, m_width(width)
,m_type(type)
{
}
CLineWidthItem::~CLineWidthItem()
{
}
void CLineWidthItem::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QSize size = this->size();
QPainter painter(this);
QPen pen;
pen.setColor(Qt::black);
pen.setWidth(m_width+1);
painter.setPen(pen);
QPoint p1(0, size.height() / 2);
QPoint p2(size.width(), size.height() / 2);
QLine line(p1, p2);
painter.drawLine(line);
}
void CLineWidthItem::mousePressEvent(QMouseEvent *event)
{
if(m_type == emItem)
{
click(m_width);
}
QLabel::mousePressEvent(event);
}
void CLineWidthItem::setWidth(const int &width)
{
m_width = width;
}
//-- class_2 combobox中的顯示的編輯框
QShowLineInEdit::QShowLineInEdit(const int & width,QWidget *parent)
:QLineEdit(parent)
,m_width(width)
{
m_pLabel = new CLineWidthItem(emLineEdit,width,this);
QHBoxLayout* layout = new QHBoxLayout();
layout->addWidget(m_pLabel);
layout->setMargin(0);
setLayout(layout);
setReadOnly(true);
}
QShowLineInEdit::~QShowLineInEdit()
{
}
void QShowLineInEdit::setWidth(const int &width)
{
m_pLabel->setWidth(width);
}
//-- class_3 能選擇線寬的combobox
CLineWidthBox::CLineWidthBox(QWidget *parent)
:QComboBox(parent)
{
m_index = 0;
m_pListWidget = new QListWidget();
//setContextMenuPolicy(Qt::NoContextMenu);//禁用選單
//m_pListWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//禁用垂直滾動條
m_pListWidget->setStyleSheet("QListWidget::Item:hover{background-color:rgb(0,125,255);}");
setModel(m_pListWidget->model());
setView(m_pListWidget);
m_pShowLineInEdit = new QShowLineInEdit(0,this);
setLineEdit(m_pShowLineInEdit);
SetCurrentIndex(0);
}
CLineWidthBox::~CLineWidthBox()
{
}
void CLineWidthBox::appendItem(const int& index)
{
CLineWidthItem* pWid = new CLineWidthItem(emItem,index,this);
connect(pWid, SIGNAL(click(const int&)), this, SLOT(onClickPenWidget(const int& )));
QListWidgetItem* pItem = new QListWidgetItem(m_pListWidget);
m_pListWidget->addItem(pItem);
m_pListWidget->setItemWidget(pItem, pWid);
}
void CLineWidthBox::onClickPenWidget(const int& index)
{
m_index = index;
m_pShowLineInEdit->setWidth(index);
setCurrentIndex(index-1);
hidePopup();
}
int CLineWidthBox::GetCurrentIndex()
{
return m_index;
}
void CLineWidthBox::SetCurrentIndex(int index)
{
m_index = index;
}
應用
ui->comboBox_3->appendItem(0);//注意:comboBox_3必須提升為CLineWidthBox
ui->comboBox_3->appendItem(1);
ui->comboBox_3->appendItem(2);
ui->comboBox_3->appendItem(3);
ui->comboBox_3->appendItem(4);