Instance Qt custom controls capture mouse messages

Source: Internet
Author: User

GUIThat is, the graphic user interface design for human-computer interaction. Many well-known international companies have been aware of the current situation of graphic user interface design in International Industries.GUIThe powerful value-added functions generated in terms of products and the huge market value, therefore, relevant departments have been set up within the company to specialize inGUIResearch and design, a number of institutions have also been established in the same industry to communicate with each otherGUIDesign Theory and experience for the purpose.

In the afternoonQtAfter visiting the Chinese forum, I found someone asking: Could you pleaseMouseOne to QlineEditWidgetI think it is feasible to trigger a signal. Based on windows experienceWidgetAlso captureMouseMessages such:

DefaultWidgetClass to derive its ownWidgetClass. In fact, the main role of this class is to allow yourself to write some processingMouseFor example, in MFCWidgetClass to add a function to process the WM_MOUSEMOVE message. This wayMouse1. MoveWidgetOn, our own program can handleMouse.

SoQtYou can't do the same. I flipped through the document and found a function of enterEvent () in Qwidget.MouseWhen you move to Qwidget, The enterEvent () function will be called. So we can alsoWidgetClass to derive its ownWidgetClass, and then implement the enterEvent () function by yourself to send a signal in this function. This is OK.

Rough code:

 
  1. class MyEdit:public QlineEdit  
  2. {  
  3.        Q_OBJECT  
  4.        public:  
  5.               MyEdit(QWidget * parent, const char * name);  
  6.               void enterEvent( QEvent * e);  
  7. };  
  8. MyEdit::MyEdit(QWidget * parent, const char * name):QLineEdit(parent,name)  
  9. {  
  10.        this->setText("11.11");  
  11. }  
  12. void MyEdit::enterEvent( QEvent * e)  
  13. {  
  14.        QString s = this->text();  
  15.        emit textChanged (s);  
  16. } 

I now connect the textChanged (s) signal to a display part and use the QLCDNumber part.

 
  1. class MyWindow:public QVBox  
  2. {  
  3.        public:  
  4.        MyWindow();  
  5.        private:  
  6.     MyEdit *        lineedit;  
  7.     QLCDNumber *       lcdnum;         
  8.  
  9. };  
  10.  
  11. MyWindow::MyWindow()  
  12. {  
  13.        QHBox* hbox1 = new QHBox(this);   
  14.        lineedit = new MyEdit(hbox1,0);  
  15.        QHBox* hbox2 = new QHBox(this);  
  16.        lcdnum = new QLCDNumber(hbox2);  
  17.        connect(lineedit,SIGNAL(textChanged ( const QString & )),lcdnum,SLOT(display ( const QString &)));  
  18. } 

Main function:

 
  1. int main(int argc, char **argv)   
  2. {  
  3.        QApplication a(argc, argv);  
  4.        MyWindow w;  
  5.        a.setMainWidget(&w);  
  6.        w.setGeometry(500,200,200,150);  
  7.        w.show();  
  8.        a.exec();  
  9. } 

OK

In fact, I think this is an experience issue, likeGUIProgram, I think some implemented class libraries are different, for exampleQtUseQtIn fact, many ideas are similar when you use MFC. As long as you are familiarGUILibrary, and others are easy to learn. Because when you are familiar with a library,GUIWhat functions do you have,WidgetA, I am familiar with it. I think there is only a problem with what library is used for implementation. At this time, as long as there isGUILibrary documentation. A lot of problems are solved.

Summary:

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.