Qt颜色选择编辑小案例

一句话,上代码:

1.头文件:

#ifndef COLORSELECTWINDOW_H
#define COLORSELECTWINDOW_H

#include <QWidget>
#include <QWidget>
#include<QComboBox>
#include<QFrame>
#include <QLabel>
#include <QTextEdit>
#include <QPushButton>
#include <QToolButton>
#include <QLineEdit>
#include <QColorDialog>

namespace Ui {
class ColorSelectWindow;
}


class ColorSelectWindow : public QWidget
{
    Q_OBJECT

public:
    explicit ColorSelectWindow(QWidget *parent = nullptr);


private slots:

    void btnClickedFun();



public:
    QColor getColor()
    {
        return m_color;
    }

    void setColor(const QColor& color)
    {
        if( color.isValid() )
        {
            m_color = color;
            m_label->setStyleSheet( QString("background-color:%1").arg( color.name() ) );
            m_lineEdit->setText( color.name() );
        }
    }

private:
    void uier();
    void initialize();

    QColor m_color;
    QLabel* m_label;
    QLineEdit* m_lineEdit;
    QToolButton* m_button;

public:
    ~ColorSelectWindow();

private:
    Ui::ColorSelectWindow *ui;
};

#endif // COLORSELECTWINDOW_H

2、源文件:

#include "ColorSelectWindow.h"
#include "ui_ColorSelectWindow.h"

#include <QPalette>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QStringList>
#include <QColor>
#include <QPixmap>
#include <QSpinBox>
#include <QTextEdit>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#pragma execution_character_set("utf-8")

ColorSelectWindow::ColorSelectWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ColorSelectWindow)
{
    ui->setupUi(this);

    this->uier();
    this->initialize();

}

void ColorSelectWindow::uier()
{
    m_label = new QLabel();
    m_lineEdit = new QLineEdit();

    m_button = new QToolButton();

    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->setMargin(20);
    layout->addWidget( m_label );
    layout->setSpacing(10);
    layout->addWidget(m_lineEdit,1);
    layout->setSpacing(10);
    layout->addWidget(m_button);

    this->setLayout( layout );

    this->setAutoFillBackground(true);

    QPalette palete;
    palete.setColor(QPalette::Background,Qt::white);
    this->setPalette(palete);
}


void ColorSelectWindow::initialize()
{
    m_color = Qt::green;
    m_label->setFixedWidth(120);
    m_label->setFixedHeight(50);
    m_label->setStyleSheet( QString("background-color:%1").arg( m_color.name() ) );

    m_lineEdit->setText( m_color.name() );
    m_lineEdit->setFocusPolicy( Qt::NoFocus);

    m_button->setText("...");

    connect( m_button,SIGNAL( clicked() ),this,SLOT( btnClickedFun() ) );

}

void ColorSelectWindow::btnClickedFun()
{
    QColor color = QColorDialog::getColor( m_color );
    if(color.isValid())
     {
       m_color=color;

       m_label->setStyleSheet(QString("background-color:%1").arg(m_color.name()));
       m_lineEdit->setText(m_color.name());
    }
}


ColorSelectWindow::~ColorSelectWindow()
{
    delete ui;
}

效果如下图所示:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值