QT-自定义控件(按键、标签、编辑框)
一、前言
使用自定义控件,我们可以从外部资源直接加载UI,不用每次修改UI完之后要去编译软件。
二、以按键自定义控件为例子
1.CustomButton.h
#pragmaonce#pragmaonce#include<QtUiPlugin/QDesignerExportWidget>#include<QtWidgets/QWidget>#include<QtWidgets/QToolButton>
class QDESIGNER_WIDGET_EXPORT CustomButton : public QToolButton
{
Q_OBJECT
//Q_PROPERTY(QString strNodeName READ getNodeName WRITE setNodeName)Q_PROPERTY(int registerType READ getRegisterType WRITE setRegisterType)Q_PROPERTY(bool setCheckable READ getEnableCheckStated WRITE setEnableCheckStated)Q_PROPERTY(QString checkedImageName READ getCheckedImageName WRITE setCheckedImageName)Q_PROPERTY(QString unCheckedImageName READ getUnCheckedImageName WRITE setUnCheckedImageName)Q_PROPERTY(QString writePressedAddr READ getWritePressedAddr WRITE setWritePressedAddr)Q_PROPERTY(QString writePressedBit READ getWritePressedBit WRITE setWritePressedBit)Q_PROPERTY(QString writePressedVal READ getWritePressedVal WRITE setWritePressedVal)Q_PROPERTY(bool enableReleaseWrite READ getEnableReleaseWrite WRITE setEnableReleaseWrite)Q_PROPERTY(QString writeReleasedAddr READ getWriteReleasedAddr WRITE setWriteReleasedAddr)Q_PROPERTY(QString writeReleasedBit READ getWriteReleasedBit WRITE setWriteReleasedBit)Q_PROPERTY(QString writeReleasedVal READ getWriteReleasedVal WRITE setWriteReleasedVal)Q_PROPERTY(QString readStateAddr READ getReadAddr WRITE setReadAddr)Q_PROPERTY(QString readStateBit READ getReadBit WRITE setReadBit)
public:
explicit CustomButton(QWidget *parent = Q_NULLPTR);
virtual ~CustomButton(){}
private:
QString strNodeName;int nRegisterType;
bool bEnableCheckStated;
bool bEnableReleaseWrite;
QString strCheckedImageName;
QString strUnCheckedImageName;
QString strWritePressedAddr;
QString strWritePressedBit;
QString strWritePressedVal;
QString strWriteReleasedAddr;
QString strWriteReleasedBit;
QString strWriteReleasedVal;
QString strReadAddr;
QString strReadBit;
QString strReadVal;
QString strFilePath;
QString strImageName;
protected:voidpaintEvent(QPaintEvent *event);voidresizeEvent(QResizeEvent *event);
bool eventFilter(QObject* object, QEvent* event);
bool event(QEvent *event);
private slots:voidslotButtonPressed();voidslotButtonReleased();
public Q_SLOTS:
QString getNodeName();intgetRegisterType();
bool getEnableCheckStated();
bool getEnableReleaseWrite();
QString getCheckedImageName();
QString getUnCheckedImageName();
QString getWritePressedAddr();
QString getWritePressedBit();
QString getWritePressedVal();
QString getWriteReleasedAddr();
QString getWriteReleasedBit();
QString getWriteReleasedVal();
QString getReadAddr();
QString getReadBit();
public Q_SLOTS:voidsetNodeName(QString strName);voidsetRegisterType(int nType);voidsetEnableCheckStated(bool bEnable);voidsetEnableReleaseWrite(bool bEnable);voidsetCheckedImageName(QString strPath);voidsetUnCheckedImageName(QString strPath);voidsetWritePressedAddr(QString strAddr);voidsetWritePressedBit(QString strBit);voidsetWritePressedVal(QString strVal);voidsetWriteReleasedAddr(QString strAddr);voidsetWriteReleasedBit(QString strBit);voidsetWriteReleasedVal(QString strVal);voidsetReadAddr(QString strAddr);voidsetReadBit(QString strBit);
Q_SIGNALS:voidsigPressedButton(QString strName, QString strWritePressedAddr,QString strWritePressedBit,QString strWritePressedVal,int nRegisterType);voidsigReleasedButton(QString strName, QString strWriteReleasedAddr,QString strWriteReleasedBit, QString strWriteReleasedVal,int nRegisterType);};
2.CustomButton.cpp
代码如下:
#include"CustomButton.h"#include<QAction>#include<QCoreApplication>#include<QDir>#include<QJsonObject>#include<QJsonDocument>#include<QMouseEvent>const QString tagObjectName("ObjectName");const QString tagButtonState("ButtonState");#defineRESET_VALQString("0")
CustomButton::CustomButton(QWidget *parent):QToolButton(parent),strNodeName("wireRod"),bEnableCheckStated(true),strCheckedImageName(""),strUnCheckedImageName(""),strWritePressedAddr("0"),strWritePressedBit("0"),strWritePressedVal("1"),strWriteReleasedAddr("0"),strWriteReleasedBit("0"),strWriteReleasedVal("0"),strReadAddr("0"),strReadBit("0"),nRegisterType(1),bEnableReleaseWrite(false){
this->installEventFilter(parent);if(bEnableCheckStated)
this->setCheckable(true);else
this->setCheckable(false);
this->setIcon(QIcon(":/Resources/pic_guabian_s.png"));
this->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);int nMax =0;if(this->width()> this->height())
nMax = this->width();else
nMax = this->height();
this->setIconSize(QSize(nMax, nMax));connect(this,SIGNAL(pressed()), this,SLOT(slotButtonPressed()));connect(this,SIGNAL(released()), this,SLOT(slotButtonReleased()));
this->installEventFilter(this);
QString strConfig = QCoreApplication::applicationDirPath()+"/Config";
QDir dir(strConfig);if(!dir.exists())
dir.mkdir(strConfig);
strFilePath = QCoreApplication::applicationDirPath()+"/Config/CustomControls";
QDir dir1(strFilePath);if(!dir1.exists())
dir1.mkdir(strFilePath);
strImageName = QCoreApplication::applicationDirPath()+"/Config/Image";
QDir dir2(strImageName);if(!dir2.exists())
dir2.mkdir(strImageName);}void CustomButton::paintEvent(QPaintEvent *event){
QToolButton::paintEvent(event);// 设置图标图片if(this->isChecked()){
QString strPath = strImageName +"/"+ strCheckedImageName;
this->setIcon(QIcon(strPath));}elseif(!this->isChecked()){
QString strPath = strImageName +"/"+ strUnCheckedImageName;
this->setIcon(QIcon(strPath));}// 设置是否按下选中if(bEnableCheckStated)
this->setCheckable(true);else
this->setCheckable(false);}void CustomButton::resizeEvent(QResizeEvent *event){
QToolButton::resizeEvent(event);}
bool CustomButton::eventFilter(QObject* object, QEvent* event){return QToolButton::eventFilter(object, event);}
bool CustomButton::event(QEvent *event){if(event->type()== QEvent::MouseButtonPress){
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);if(mouseEvent->button()== Qt::LeftButton){}}return QToolButton::event(event);}
QString CustomButton::getNodeName(){return strNodeName;}int CustomButton::getRegisterType(){return nRegisterType;}
bool CustomButton::getEnableCheckStated(){return bEnableCheckStated;}
bool CustomButton::getEnableReleaseWrite(){return bEnableReleaseWrite;}
QString CustomButton::getCheckedImageName(){return strCheckedImageName;}
QString CustomButton::getUnCheckedImageName(){return strUnCheckedImageName;}
QString CustomButton::getWritePressedAddr(){return strWritePressedAddr;}
QString CustomButton::getWritePressedBit(){return strWritePressedBit;}
QString CustomButton::getWritePressedVal(){return strWritePressedVal;}
QString CustomButton::getWriteReleasedAddr(){return strWriteReleasedAddr;}
QString CustomButton::getWriteReleasedBit(){return strWriteReleasedBit;}
QString CustomButton::getWriteReleasedVal(){return strWriteReleasedVal;}
QString CustomButton::getReadAddr(){return strReadAddr;}
QString CustomButton::getReadBit(){return strReadBit;}void CustomButton::setNodeName(QString strName){
strNodeName = strName;}void CustomButton::setRegisterType(int nType){
nRegisterType = nType;}void CustomButton::setEnableCheckStated(bool bEnable){if(bEnableCheckStated)
this->setCheckable(true);else
this->setCheckable(false);
bEnableCheckStated = bEnable;}void CustomButton::setEnableReleaseWrite(bool bEnable){
bEnableReleaseWrite = bEnable;}void CustomButton::setCheckedImageName(QString strPath){
strCheckedImageName = strPath;}void CustomButton::setUnCheckedImageName(QString strPath){
strUnCheckedImageName = strPath;}void CustomButton::setWritePressedAddr(QString strAddr){
strWritePressedAddr = strAddr;}void CustomButton::setWritePressedBit(QString strBit){
strWritePressedBit = strBit;}void CustomButton::setWritePressedVal(QString strVal){
strWritePressedVal = strVal;}void CustomButton::setWriteReleasedAddr(QString strAddr){
strWriteReleasedAddr = strAddr;}void CustomButton::setWriteReleasedBit(QString strBit){
strWriteReleasedBit = strBit;}void CustomButton::setWriteReleasedVal(QString strVal){
strWriteReleasedVal = strVal;}void CustomButton::setReadAddr(QString strAddr){
strReadAddr = strAddr;}void CustomButton::setReadBit(QString strBit){
strReadBit = strBit;}void CustomButton::slotButtonReleased(){if(bEnableCheckStated)return;if(bEnableReleaseWrite)
emit sigReleasedButton(strNodeName, strWriteReleasedAddr, strWriteReleasedBit, strWriteReleasedVal,nRegisterType);}void CustomButton::slotButtonPressed(){if(bEnableCheckStated){// 选中和不选中,写入的值不同if(!this->isChecked())
emit sigPressedButton(strNodeName, strWritePressedAddr, strWritePressedBit, strWritePressedVal,nRegisterType);else
emit sigPressedButton(strNodeName, strWritePressedAddr, strWritePressedBit, RESET_VAL,nRegisterType);}else{// 只有按下的值
emit sigPressedButton(strNodeName, strWritePressedAddr, strWritePressedBit, strWritePressedVal, nRegisterType);}}
3.CustomButtonPlugin.h
#pragmaonce#include<QtUiPlugin/QDesignerCustomWidgetInterface>
class CustomButtonPlugin : public QObject, public QDesignerCustomWidgetInterface
{
Q_OBJECT
Q_INTERFACES(QDesignerCustomWidgetInterface)
public:CustomButtonPlugin(QObject *parent = Q_NULLPTR);
bool isContainer()const;
bool isInitialized()const;
QIcon icon()const;
QString domXml()const;
QString group()const;
QString includeFile()const;
QString name()const;
QString toolTip()const;
QString whatsThis()const;
QWidget *createWidget(QWidget *parent);voidinitialize(QDesignerFormEditorInterface *core);
private:
bool initialized;};
4.CustomButtonPlugin.cpp
代码如下:
#include"CustomButton.h"#include"CustomButtonPlugin.h"#include<QtCore/QtPlugin>
CustomButtonPlugin::CustomButtonPlugin(QObject *parent):QObject(parent){
initialized = false;}void CustomButtonPlugin::initialize(QDesignerFormEditorInterface */*core*/){if(initialized)return;
initialized = true;}
bool CustomButtonPlugin::isInitialized()const{return initialized;}
QWidget *CustomButtonPlugin::createWidget(QWidget *parent){return new CustomButton(parent);}
QString CustomButtonPlugin::name()const{return"CustomButton";}
QString CustomButtonPlugin::group()const{return"SWJ PLUGIN";}
QIcon CustomButtonPlugin::icon()const{returnQIcon(":/Resources/pic_guabian_s.png");}
QString CustomButtonPlugin::toolTip()const{returnQString();}
QString CustomButtonPlugin::whatsThis()const{returnQString();}
bool CustomButtonPlugin::isContainer()const{return false;}
QString CustomButtonPlugin::domXml()const{return"<widget class=\"CustomButton\" name=\"CustomButton\">\n"" <property name=\"geometry\">\n"" <rect>\n"" <x>0</x>\n"" <y>0</y>\n"" <width>100</width>\n"" <height>36</height>\n"" </rect>\n"" </property>\n""</widget>\n";}
QString CustomButtonPlugin::includeFile()const{return"CustomButton.h";}
三、下载链接
本文转载自: https://blog.csdn.net/u013083044/article/details/128584589
版权归原作者 进击的大海贼 所有, 如有侵权,请联系我们删除。
版权归原作者 进击的大海贼 所有, 如有侵权,请联系我们删除。