Custom widgets can be created by subclasses of an already existing QT widget, or by Qwidget directly to the sub-class. The following is a direct subclass of an existing QT widget:
The following is the code for implementing a self-required widget by Qlineedit the sub-class:
/********************** sub-class header file *****************************/
#ifndefLINEEDIT_H
#defineLINEEDIT_H
#include <QLineEdit>
#include <QMouseEvent>
Classlineedit:publicqlineedit
{
Q_object
Public
Explicitlineedit (qobject*parent=0);
Protected
Voidmousedoubleclickevent (qmouseevent*);
};
#endif//lineedit_h
/********************** the source file of the subclass *****************************/
#include "Lineedit.h"
#include <QMessageBox>
Lineedit::lineedit (qobject*parent)
{
}
Re-implementing the Mousedoubleclickevent (Qmouseevent*event) of the Qlineedit class
event handler, so that a message box pops up when you double-click the Lineedit
Voidlineedit::mousedoubleclickevent (qmouseevent*event)
{
Qmessagebox::information (THIS,TR ("hint"), TR ("You are right!") "));
Event->ignore ();
}
The above is my own implementation of a Lineedit class, I double-click the Lineedit control, will pop up a message box out.
First build a project, put the above two files under the project directory, and then to implement their own code:
/********************** the header file of the main window *****************************/
#ifndefMYWIDGET_H
#defineMYWIDGET_H
#include <QWidget>
#include "Lineedit.h"
Classmywidget:publicqwidget
{
Q_object
Public
Explicitmywidget (qwidget*parent=0);
Private
Lineedit*lineedit;
};
#endif//mywidget_h
/********************** the source file of the main window *****************************/
#include "Mywidget.h"
#include <QHBoxLayout>
Mywidget::mywidget (qwidget*parent):
Qwidget (parent)
{
Lineedit=newlineedit;
Qhboxlayout*hlayout=newqhboxlayout;
Hlayout->addwidget (Lineedit);
SetLayout (hlayout);
}
/********************** Displays the source file of the main window *****************************/
#include <QApplication>
#include <QTextCodec>
#include "Mywidget.h"
Intmain (intargc,char*argv[])
{
Qapplicationapp (ARGC,ARGV);
QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname ("GBK"));
Mywidget*mywidget=newmywidget;
Mywidget->show ();
Returnapp.exec ();
}
|
|
|
QT custom Control Message implementation