赞助

Qt 组合框QComboBox定制选择线宽

转载:https://blog.csdn.net/qq_33029733/article/details/109084388

在项目开发中需要定制Qt Combobox,每一项是显示一个尺寸的线宽供用户选择,这个功能类似我我之前实现的定制选择颜色对话框

QLineWidthCombobox.h

#pragma once

#include<QComboBox>
#include<QLabel>
#include<QLineEdit>
#include<QListWidget>

class QPenWidth : public QLabel
{
    Q_OBJECT

public:
    QPenWidth(QWidget  *parent = Q_NULLPTR);
    ~QPenWidth();

    void SetDrawParameters(int drawType, int isize);
protected:
    void paintEvent(QPaintEvent *) override;
private:
    int m_drawType;//绘制类型(画点/画线)
    int m_size;//大小
};


class  QPenWidget : public QLineEdit
{
    Q_OBJECT
public:
    QPenWidget(QWidget *parent = Q_NULLPTR);
    ~QPenWidget();

    void updatePen(const int& index, const int& type);
protected:
    void mousePressEvent(QMouseEvent *event);

signals:
    void click(const int& index);
private:
    QLabel* m_pLabel;
    QPenWidth* m_pCssLabel;
    int m_index;
    int m_type;
};

class  QLineWidthCombobox :public QComboBox
{
    Q_OBJECT
public:
    QLineWidthCombobox(QWidget *parent = Q_NULLPTR);
    ~QLineWidthCombobox();

    void SetType(int drawType);

    int GetCurrentIndex();
    void SetCurrentIndex(int index);

    void SetList(QList<int>&list);
    QList<int> GetList();

private slots:
    void onClickPenWidget(const int& index);

signals:
    void SelectedItemChanged(int);

public:
    void appendItem(const int& index);
private:
    QPenWidget* m_pPenEdit;
    QListWidget* m_pListWidget;
    QList<QColor> m_colorsList;
    int m_drawType;
    int m_index;
    QList<int> m_list;
};

 

QLineWidthComboboxEx.cpp

#include "QLineWidthCombobox.h"
#include<QHBoxLayout>
#include<QPainter>
#include<QPaintEvent>
#include<QPoint>
#include<QLine>
QPenWidth::QPenWidth(QWidget  *parent)
    : QLabel(parent)
    , m_drawType(0)
    , m_size(1)
{
}

QPenWidth::~QPenWidth()
{
}

void QPenWidth::SetDrawParameters(int drawType, int isize)
{
    m_drawType = drawType;
    m_size = isize;
}

void QPenWidth::paintEvent(QPaintEvent *)
{
    QSize size = this->size();
    QPainter painter(this);
    QPen pen;
    pen.setColor(Qt::black);
    pen.setWidth(m_size);
    painter.setPen(pen);
    if (m_drawType == 0)
        painter.drawPoint(QPoint(size.width() / 2, size.height() / 2));
    else
    {

        QPoint p1(0, size.height() / 2);
        QPoint p2(size.width(), size.height() / 2);
        QLine line(p1, p2);
        painter.drawLine(line);
    }

}

QPenWidget::QPenWidget(QWidget *parent /*= Q_NULLPTR*/)
    :QLineEdit(parent)
    , m_index(0)
    , m_type(0)
{
    m_pLabel = new QLabel();
    m_pCssLabel = new QPenWidth();

    m_pLabel->setFixedSize(12, 12);

    QHBoxLayout* layout = new QHBoxLayout();
    layout->addWidget(m_pLabel);
    layout->addWidget(m_pCssLabel);
    layout->setContentsMargins(5, 0, 0, 2);
    setLayout(layout);
    setReadOnly(true);

    setStyleSheet("QLineEdit{border: none;}QLineEdit:hover{background-color:rgb(0,125,255);}");

}

QPenWidget::~QPenWidget()
{

}

void QPenWidget::updatePen(const int& index, const int& type)
{
    m_index = index;
    m_type = type;

    QString strText = QString("%1 )").arg(QString::number(m_index));
    m_pLabel->setText(strText);
    m_pCssLabel->SetDrawParameters(type, index);

}

void QPenWidget::mousePressEvent(QMouseEvent *event)
{
    emit click(m_index);
}

QLineWidthCombobox::QLineWidthCombobox(QWidget *parent /*= Q_NULLPTR*/)
    :QComboBox(parent)
    , m_drawType(1)
{
    m_pPenEdit = new QPenWidget();
    m_pListWidget = new QListWidget();
    m_pPenEdit->setStyleSheet("");
    setContextMenuPolicy(Qt::NoContextMenu);//禁用菜单
    m_pListWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//禁用垂直滚动条
    m_pListWidget->setStyleSheet("QListWidget::Item:hover{background-color:rgb(0,125,255);}");
    setLineEdit(m_pPenEdit);
    setModel(m_pListWidget->model());
    setView(m_pListWidget);
}

QLineWidthCombobox::~QLineWidthCombobox()
{

}

void QLineWidthCombobox::SetType(int drawType)
{
    m_drawType = drawType;
}

int QLineWidthCombobox::GetCurrentIndex()
{
    return m_index;
}

void QLineWidthCombobox::SetCurrentIndex(int index)
{
    m_index = index;
    m_pPenEdit->updatePen(index, m_drawType);
}

void QLineWidthCombobox::SetList(QList<int>&list)
{
    m_list = list;
    m_pListWidget->clear();
    int icount = m_list.count();
    for (int i = 0; i < icount; i++)
        appendItem(m_list[i]);
}

QList<int> QLineWidthCombobox::GetList()
{
    return m_list;
}

void QLineWidthCombobox::onClickPenWidget(const int& index)
{
    m_index = index;
    m_pPenEdit->updatePen(index, m_drawType);
    hidePopup();
    emit SelectedItemChanged(m_index);
}

void QLineWidthCombobox::appendItem(const int& index)
{
    QPenWidget* pWid = new QPenWidget(this);
    pWid->updatePen(index, m_drawType);
    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);
}

MainWindow.cpp

.......................


ui.comboBox->appendItem(1);
ui.comboBox->appendItem(2);
ui.comboBox->appendItem(3);
ui.comboBox->appendItem(4);
ui.comboBox->appendItem(5);
ui.comboBox->appendItem(6);
ui.comboBox->appendItem(7);
ui.comboBox->appendItem(8);
ui.comboBox->appendItem(9);

........................

 

posted @ 2021-02-25 22:22  车臣  阅读(904)  评论(0编辑  收藏  举报