Qt custom tag button and qt custom tag

Source: Internet
Author: User

Qt custom tag button and qt custom tag

When you get started with Qt, you will be surprised by its extremely convenient cross-platform approach and want to try Qt. Gradually, you will find that some of Qt's built-in controls cannot meet your own needs. At this time, we need to define our own controls. In general, there are many tag style settings, but the default tag does not respond to events with the mouse.

Today, we bring you the tag button class. You can see from the name that the tag is changed to a button, so that the tag has the button and mouse response function.

In your Qt project

Add a new file: C ++ Class, input Class name: ClickedLabel, and base Class: QLabel. Qt automatically generates the ClickedLabel. h and ClickedLabel. cpp files.

In ClickedLabel. h

1 # ifndef CLICKEDLABEL_H 2 # define CLICKEDLABEL_H 3 4 # include <QLabel> 5 // night labels: 17/06/04 6 class ClickedLabel: public QLabel 7 {8 Q_OBJECT 9 public: 10 ClickedLabel (QWidget * parent = 0); 11 int MyLabelPressed; 12 void mousePressEvent (QMouseEvent * e); // Add the mouse RESPONSE event 13 void mouseReleaseEvent (QMouseEvent * e ); // Add the mouse release event 14 signals: 15 void clicked (); // click signal 16 17}; 18 19 # endif // CLICKEDLABEL_H

Then, set the default style in the constructor in ClickedLabel. cpp (Note: You can leave it empty) and add an initial value 0 to MyLabelPressed;

1 # include "clickedlabel. h" 2 3 ClickedLabel: ClickedLabel (QWidget * parent): QLabel (parent) 4 {5 setText ("Author: Night success! "); // Add the default label text 6 setAlignment (Qt: AlignCenter); // set the default alignment mode: center AlignCenter alignment) 7 // set the default label style 8 setStyleSheet ("ClickedLabel {background-color: rgb (143,122,102); border-radius: 10px; font: bold; color: white ;}"); 9 MyLabelPressed = 0; 10} 11 12 void ClickedLabel: mousePressEvent (QMouseEvent * e) 13 {14 MyLabelPressed = 1; 15} 16 17 void ClickedLabel: mouseReleaseEvent (QMouseEvent * e) 18 {19 if (MyLabelPressed) 20 {21 emit clicked (); 22 MyLabelPressed = 0; 23} 24}

Then, in your Qt project

Add a header function: # include "clickedlabel. h"

Add a private or public function: ClickedLabel * Btn1;

Then implement the function in the CPP file of the project file.

Related Article

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.