http://wgt2050.blog.163.com/blog/static/18162431920116123151111/
Qt中有好多窗体都不能满足应用需要,需要自己定义窗体部件。例如,Qlabel不具有点击功能,为此,我想自己定义一个具有点击功能的label类,并且将其添加到Qt desinger中。
定义clickablelabel的方法:见http://blog.youkuaiyun.com/rockeinstein2/article/details/5296242。
1,新建clickablelabel文件夹,建立头文件clickablelabel.h,新建一个类clickablelabel从QLabel中继承过来。
#ifndef CLICKABLELABEL_H
#define CLICKABLELABEL_H
#include<QtGui>
class ClickableLabel : public QLabel
{
Q_OBJECT
public:
ClickableLabel(QWidget *parent=0);
int MyLabelPressed;
void mousePressEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);
signals:
void clicked();
};
#endif // CLICKABLELABEL_H
2,然后建立相应的cpp文件clickablelabel.cpp
#include "clickablelabel.h"ClickableLabel::ClickableLabel(QWidget *parent) :QLabel(parent){this->setText("ClickableLabel");}void ClickableLabel::mousePressEvent ( QMouseEvent * e ){MyLabelPressed = 1;}void ClickableLabel::mouseReleaseEvent(QMouseEvent *e){if ( MyLabelPressed){emit clicked();//发出点击信号MyLabelPressed = 0;}}这样就实现了可以点击的标签clickablelabel类,下面将它集成到qt designer中:在clickablelabel文件夹的同级目录下建立文件夹clickablelabelplugin。3,建立clickablelabelplugin.h头文件:#ifndef CLICKABLELABELPLUGIN_H#define CLICKABLELABELPLUGIN_H#include <QDesignerCustomWidgetInterface>class clickablelabelplugin : public QObject,public QDesignerCustomWidgetInterface{Q_OBJECTQ_INTERFACES(QDesignerCustomWidgetInterface)public:clickablelabelplugin(QObject *parent = 0);QString name() const;QString includeFile() const;QString group() const;QIcon icon() const;QString toolTip() const;QString whatsThis() const;bool isContainer() const;QWidget *createWidget(QWidget *parent);signals:public slots:};#endif // CLICKABLELABELPLUGIN_H4,然后建立cpp文件:#include <QtPlugin>#include "clickablelabelplugin.h"#include "../clickablelabel/clickablelabel.h"clickablelabelplugin::clickablelabelplugin(QObject *parent) :QObject(parent){}QString clickablelabelplugin::name() const{return "ClickableLabel";}QString clickablelabelplugin::text() const{return "ClickableLabel";}QString clickablelabelplugin::selectedText() const{return "ClickableLabel";}QString clickablelabelplugin::includeFile() const{return "clickablelabel.h";}QString clickablelabelplugin::group() const{return tr("Image Manipulation Widgets");}QIcon clickablelabelplugin::icon() const{return QIcon(":/images/clickablelabel.png");}QString clickablelabelplugin::toolTip() const{return tr("A clickablelabel widget");}QString clickablelabelplugin::whatsThis() const{return tr("This widget is A clickablelabel widget");}bool clickablelabelplugin::isContainer() const{return false;}QWidget *clickablelabelplugin::createWidget(QWidget *parent){return new ClickableLabel(parent);}Q_EXPORT_PLUGIN2(clickablelabelplugin, clickablelabelplugin)5,然后建立工程:clickablelabelplugin.pro,将clickablelabel.h和clickablelabel.cpp包含进来。
TEMPLATE = lib #//自动生成的是appTARGET =DEPENDPATH += .INCLUDEPATH += .CONFIG += designer plugin release# InputHEADERS += clickablelabelplugin.h \../clickablelabel/clickablelabel.hSOURCES += clickablelabelplugin.cpp \../clickablelabel/clickablelabel.cppDESTDIR = $$[QT_INSTALL_PLUGINS]/designerRESOURCES += \images/clickablelabel.qrc
6,编译,进入minGW中输入nmake,运行通过,打开qt designer,就能看到新的可点击标签啦~
586

被折叠的 条评论
为什么被折叠?



