zoukankan      html  css  js  c++  java
  • Qt学习-模仿Qt实现一个colorbutton

    1、mycolorbutton.h

    #include<QToolButton>
    #include<QtGlobal>
    #include<QColor>
    class MyColorButtonPrivate;
    class MyColorButton : public QToolButton
    {
        Q_OBJECT
    public:
        MyColorButton(QWidget* parent);
        ~MyColorButton();
        QColor color() const;
    public slots:
        void setColor(const QColor& color);
    signals:
        void colorChanged(const QColor& color);
    private:
        QScopedPointer<MyColorButtonPrivate> d_ptr;
        Q_DECLARE_PRIVATE(MyColorButton)
        Q_DISABLE_COPY(MyColorButton)
        Q_PRIVATE_SLOT(d_func(), void slotEditColor())
    };
    

      2、mycolorbutton_p.h

    #include "mycolorbutton.h"
    class MyColorButtonPrivate
    {
    
    public:
        QColor m_color;
        void slotEditColor();
        MyColorButton* q_ptr;
        Q_DECLARE_PUBLIC(MyColorButton)
    
    };
    

      3、mycolorbutton.cpp

    #include <QColorDialog>
    #include "mycolorbutton_p.h"
    
    
    void MyColorButtonPrivate::slotEditColor()
    {
        const QColor newColor = QColorDialog::getColor(m_color, q_ptr, QString(), QColorDialog::ShowAlphaChannel);
        if(!newColor.isValid() || newColor == q_ptr->color())
            return;
        q_ptr->setColor(newColor);
        emit q_ptr->colorChanged(m_color);
    }
    /////////////////////////////////////////////////////////////////
    MyColorButton::MyColorButton(QWidget* parent)
        :QToolButton(parent), d_ptr(new MyColorButtonPrivate)
    {
        d_ptr->q_ptr = this;
    	connect(this, SIGNAL(clicked()), this, SLOT(slotEditColor()));
    }
    
    MyColorButton::~MyColorButton()
    {
    
    
    }
    
    QColor MyColorButton::color() const
    {
        return d_ptr->m_color;
    }
    
    void MyColorButton::setColor(const QColor &color)
    {
        d_ptr->m_color = color;
    }
    
    #include "moc_mycolorbutton.cpp"
    

      

  • 相关阅读:
    subtitleedit
    NHibernate.Mapping1.1.csgen 模板
    js jqueryhotkeys
    c#.net将对象序列化,反序列化json
    ruby document
    sqlserver2008新数据类型
    [转]杂记
    UVA 532 Dungeon Master
    UVA 10557 XYZZY
    UVA 10129 Play on Words
  • 原文地址:https://www.cnblogs.com/m-zhang-yang/p/11991173.html
Copyright © 2011-2022 走看看