中国容桂营销网站建设,长沙发布app,现在注册一个公司要多少钱,公司网址格式怎么写提要
自定义行编辑器#xff0c;点击后弹出颜色选择对话框#xff0c;选择喜欢的颜色#xff0c;确认后在行编辑器加载选中的颜色。
效果 选中某一个颜色后#xff0c;行编辑器中加载所选的颜色。
示例
mylineedit.h
#ifndef MYLINEEDIT_H
#define MYLINEEDIT_H#inclu…提要
自定义行编辑器点击后弹出颜色选择对话框选择喜欢的颜色确认后在行编辑器加载选中的颜色。
效果 选中某一个颜色后行编辑器中加载所选的颜色。
示例
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 QFontDialogmyLineEdit::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控件都可以使用该类来提升点击添加按钮然后点击提升按钮。原本的行编辑器就变为了自定义的行编辑器。