qt自定义行编辑器,用来加载颜色
生活随笔
收集整理的這篇文章主要介紹了
qt自定义行编辑器,用来加载颜色
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
提要
自定義行編輯器,點擊后彈出顏色選擇對話框,選擇喜歡的顏色,確認(rèn)后在行編輯器加載選中的顏色。
效果
選中某一個顏色后,行編輯器中加載所選的顏色。
示例
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);//設(shè)置顏色QColor getBackgroundColor();//獲取設(shè)置的顏色protected:void paintEvent(QPaintEvent *event);//繪制編輯框的背景色void mousePressEvent(QMouseEvent *event);//鼠標(biāo)點擊出現(xiàn)顏色對話框 private:QColor m_color;//保存行編輯器的背景色 };#endif // MYLINEEDIT_Hmylineedit.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);//設(shè)置不可編輯 }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); }以上便可以實現(xiàn)行編輯器用來加載顏色,使用時將該類的頭文件和源文件拷進(jìn)到項目中,包含該類的頭文件,在ui文件中拖入QLineEdit控件,在控件上右鍵,選擇提升為,彈出提升的窗口部件對話框,填寫提升的類名稱時,將類名myLineEdit直接拷貝到提升的類名稱的后面的編輯框中,會自動加載下面的頭文件后面的內(nèi)容,勾選全局包含時,整個程序中的QLineEdit控件都可以使用該類來提升,點擊添加按鈕,然后點擊提升按鈕。原本的行編輯器就變?yōu)榱俗远x的行編輯器。
總結(jié)
以上是生活随笔為你收集整理的qt自定义行编辑器,用来加载颜色的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言20152016真题及答案,201
- 下一篇: qt创建图形项,添加自定义窗口