QComboBox中添加右鍵菜單項設置QPen樣式

之前項目中有用到鼠標繪製矩形框的Demo,這兩天新添加了點小功能,重寫QComboBox控件使用事件過濾器篩選出第二項的右鍵事件,在右鍵事件中調出QMenu菜單設置QPen的顏色、線型以及線寬,給用戶多些選擇:

以下是MyCustomComboBox的頭文件以及源文件:

#ifndef MYCUSTOMCOMBOBOX_H
#define MYCUSTOMCOMBOBOX_H

#include <QComboBox>
#include <QDebug>
#include <QMouseEvent>
#include <QMenu>
#include <QAction>
#include <QPen>
#include <QPainter>
#include <QColorDialog>
#include <QInputDialog>

class MyCustomComboBox : public QComboBox
{
    Q_OBJECT
public:
    MyCustomComboBox();
    ~MyCustomComboBox();

    QColor curColor;
    int curStyle;
    int curWidth;

private:
    QAction *autoCropAction;
    QMenu *menu1, *colorMenu, *styleMenu, *widthMenu;

    QPen linePen;
    void CreateCustomMenu();
    QIcon FillColor(QColor color);
    QIcon FillStyle(int style);
    QIcon FIllWidth();

private slots:
    void SetLineColor();

    void SetSolidLine();
    void SetDashLine();
    void SetDotLine();
    void SetDashDotLine();
    void SetDashDotDotLine();
    void SetCustomDashLine();

    void SetLineWidth();

protected:
    bool eventFilter(QObject *object, QEvent *event);

signals:
    void LineChanged(QColor color, int style, int width);

};

#endif // MYCUSTOMCOMBOBOX_H
#include "mycustomcombobox.h"

MyCustomComboBox::MyCustomComboBox()
{
    installEventFilter(this);

    linePen = QPen(Qt::red, 2, Qt::DashDotLine, Qt::FlatCap);
    curColor = QColor(Qt::red);
    curStyle = 0;
    curWidth = 2;

    addItem("Item1");
    addItem("Item2");
    addItem("Item3");

    menu1 = new QMenu();
    menu1->addSeparator();
    menu1->setStyleSheet("border:0.5px solid gray;background-color:white;color:black;");
    CreateCustomMenu();
}

MyCustomComboBox::~MyCustomComboBox()
{
    delete menu1;
}

void MyCustomComboBox::CreateCustomMenu()
{
    QIcon icon1 = FillColor(QColor(255,0,0));
    colorMenu = menu1->addMenu(icon1, QString("顏色"));
    colorMenu->addAction("請選擇顏色!", this, SLOT(SetLineColor()));

    QIcon icon2 = FillStyle(0);
    styleMenu = menu1->addMenu(icon2, QString("線型"));
    styleMenu->addAction(FillStyle(0),"實線", this, SLOT(SetSolidLine()));
    styleMenu->addAction(FillStyle(1),"虛線", this, SLOT(SetDashLine()));
    styleMenu->addAction(FillStyle(2),"點線", this, SLOT(SetDotLine()));
    styleMenu->addAction(FillStyle(3),"點劃線", this, SLOT(SetDashDotLine()));
    styleMenu->addAction(FillStyle(4),"雙點劃線", this, SLOT(SetDashDotDotLine()));
    //styleMenu->addAction(FillStyle(5),"CustomDashLine", this, SLOT(SetCustomDashLine()));

    widthMenu = menu1->addMenu(QString("線寬"));
    widthMenu->addAction("請輸入指定寬度(1-10)!", this, SLOT(SetLineWidth()));
}

QIcon MyCustomComboBox::FillColor(QColor color)
{
    linePen.setColor(color);

    QPixmap pixmap(QSize(25, 25));
    QPainter painter(&pixmap);
    painter.fillRect(QRect(0, 0, pixmap.width(), pixmap.height()), curColor);
    painter.setPen(linePen);
    painter.fillRect(QRect(0, 0, pixmap.width(), pixmap.height()), linePen.brush());

    return QIcon(pixmap);
}

QIcon MyCustomComboBox::FillStyle(int style)
{
    switch(style)
    {
    case 0:
        linePen.setStyle(Qt::SolidLine);
        break;
    case 1:
        linePen.setStyle(Qt::DashLine);
        break;
    case 2:
        linePen.setStyle(Qt::DotLine);
        break;
    case 3:
        linePen.setStyle(Qt::DashDotLine);
        break;
    case 4:
        linePen.setStyle(Qt::DashDotDotLine);
        break;
    case 5:
        linePen.setStyle(Qt::CustomDashLine);
        break;
    default:
        break;
    }

    QPixmap pixmap(QSize(25, 25));
    QPainter painter(&pixmap);
    painter.fillRect(QRect(0, 0, pixmap.width(), pixmap.height()), QColor(255, 255, 255));
    painter.setPen(linePen);
    painter.drawRect(QRect(0, 0, 25, 25));

    return QIcon(pixmap);
}

void MyCustomComboBox::SetLineColor()
{
    QColorDialog *colorDlg = new QColorDialog(this);
    colorDlg->exec();
    curColor = colorDlg->currentColor();
    linePen.setColor(curColor);
    colorMenu->setIcon(FillColor(curColor));
    styleMenu->setIcon(FillStyle(curStyle));
    int index = 0;
    foreach (QAction *action, styleMenu->actions()) {
        action->setIcon(FillStyle(index++));
    }
}

void MyCustomComboBox::SetSolidLine()
{
    curStyle = 0;
    styleMenu->setIcon(FillStyle(curStyle));
}
void MyCustomComboBox::SetDashLine()
{
    curStyle = 1;
    styleMenu->setIcon(FillStyle(curStyle));
}
void MyCustomComboBox::SetDotLine()
{
    curStyle = 2;
    styleMenu->setIcon(FillStyle(curStyle));
}
void MyCustomComboBox::SetDashDotLine()
{
    curStyle = 3;
    styleMenu->setIcon(FillStyle(curStyle));
}
void MyCustomComboBox::SetDashDotDotLine()
{
    curStyle = 4;
    styleMenu->setIcon(FillStyle(curStyle));
}
void MyCustomComboBox::SetCustomDashLine()
{
    curStyle = 5;
    styleMenu->setIcon(FillStyle(curStyle));
}

void MyCustomComboBox::SetLineWidth()
{
    bool ok;
    QString text = QInputDialog::getText(this, tr("請輸入指定線寬"),
                                         tr("線寬:"), QLineEdit::Normal,
                                         QString(""), &ok, Qt::Dialog, Qt::ImhPreferNumbers);
    if (ok && !text.isEmpty())
    {
        int width = text.toInt(&ok, 10);
        if(width>0 && width <= 10)
            curWidth = width;
    }
}

bool MyCustomComboBox::eventFilter(QObject *object, QEvent *event)
{
    if(object == this)
    {
        if(event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent = (QMouseEvent*)event;
            if(mouseEvent->button() == Qt::RightButton  && 1 == currentIndex())
            {
                menu1->exec(QCursor::pos());

                emit LineChanged(curColor, curStyle, curWidth);
                return true;
            }
            else
                return false;
        }
        else
            return false;
    }
    else
        return MyCustomComboBox::eventFilter(object, event);
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章