QT Custom Control Message implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.