qt自定义行编辑器,用来加载颜色

提要

自定义行编辑器,点击后弹出颜色选择对话框,选择喜欢的颜色,确认后在行编辑器加载选中的颜色。

效果

在这里插入图片描述
在这里插入图片描述
选中某一个颜色后,行编辑器中加载所选的颜色。

示例

mylineedit.h

#ifndef MYLINEEDIT_H
#define MYLINEEDIT_H

#include <QWidget>
#include <QLineEdit>

/******类功能描述:自定义行编辑器,可加载颜色*****/
class myLineEdit : public QLineEdit
{
Q_OBJECT
public:
    myLineEdit(QWidget *parent = nullptr);
    ~myLineEdit();

    void setBackgroundColor(QColor &color);//设置颜色
    QColor getBackgroundColor();//获取设置的颜色

protected:
    void paintEvent(QPaintEvent *event);//绘制编辑框的背景色
    void mousePressEvent(QMouseEvent *event);//鼠标点击出现颜色对话框
private:
    QColor m_color;//保存行编辑器的背景色
};

#endif // MYLINEEDIT_H

mylineedit.cpp

#include "mylineedit.h"
#include <QMouseEvent>
#include <QColorDialog>
#include <QFontDialog>

myLineEdit::myLineEdit(QWidget *parent)
:QLineEdit(parent)
{
    m_color = QColor(255,170,127,255);
    setReadOnly(true);//设置不可编辑
}

myLineEdit::~myLineEdit()
{

}

void myLineEdit::setBackgroundColor(QColor &color)
{
    m_color = color;
    update();
}

QColor myLineEdit::getBackgroundColor()
{
    return m_color;
}

void myLineEdit::paintEvent(QPaintEvent *event)
{
    QPalette pal;
    pal.setBrush(QPalette::Base,QColor(m_color));
    setPalette(pal);

    QLineEdit::paintEvent(event);
}

void myLineEdit::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton){
        QColorDialog colorDlg(this);
        colorDlg.setFixedSize(600,500);
        colorDlg.setWindowTitle("颜色选择对话框");
        colorDlg.setCurrentColor(QColor(170,0,0,255));
        if(colorDlg.exec() == QColorDialog::Accepted){
            QColor color = colorDlg.currentColor();
            setBackgroundColor(color);
        }
    }
    QLineEdit::mousePressEvent(event);
}

以上便可以实现行编辑器用来加载颜色,使用时将该类的头文件和源文件拷进到项目中,包含该类的头文件,在ui文件中拖入QLineEdit控件,在控件上右键,选择提升为,弹出提升的窗口部件对话框,填写提升的类名称时,将类名myLineEdit直接拷贝到提升的类名称的后面的编辑框中,会自动加载下面的头文件后面的内容,勾选全局包含时,整个程序中的QLineEdit控件都可以使用该类来提升,点击添加按钮,然后点击提升按钮。原本的行编辑器就变为了自定义的行编辑器。
在这里插入图片描述

使用QT实现颜色选择器可以参考以下代码实现: ```cpp #include <QColorDialog> void MyColor::on_pushButton_clicked() { QColor color = QColorDialog::getColor(Qt::white, this); qDebug() << "颜色选择" << color << color.name(); if(!color.isValid()){ // 点击关闭或取消,颜色无效 // return; } else { ui->winColor->setStyleSheet("background-color: "+ color.name()+ ";"); QRgb rgb = color.rgb(); qDebug() << "rgb== " << qRed(rgb) << qGreen(rgb) << qBlue(rgb); qDebug() << "m_winColor== " << color.name(); } } ``` 这段代码使用了QColorDialog类来创建颜色选择对话框,并获取用户选择的颜色。通过设置样式表,将选择的颜色应用到ui->winColor上,实现颜色的显示。同时,还可以获取颜色的RGB值和颜色名称。 #### 引用[.reference_title] - *1* [QT 颜色选择器 QColorDialog 使用](https://blog.csdn.net/qq_40015157/article/details/125087942)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Qt颜色选择器(HSV)](https://blog.csdn.net/weixin_42219627/article/details/123509635)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肩上风骋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值