How to realize the effect of Universal Button Map by Qt
This article mainly introduces "how to achieve universal button map effect in Qt". In daily operation, I believe many people have doubts about how to achieve general button map effect in Qt. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to achieve universal button map effect in Qt". Next, please follow the editor to study!
I. Preface
Main functions:
Can set defense zone style circle, police, bubble, bubble 2, message 2
Can set up defense zone status for defense, withdrawal, alarm, bypass, failure
Alarm switch can be set
The defense zone number that can be set for display
Can be set whether the mouse can be dragged
2. Code idea void ButtonDefence::paintEvent (QPaintEvent *) {double width = this- > width (); double height = this- > height (); QPainter painter (this); painter.setRenderHint (QPainter::Antialiasing); / / background map QImage img (imgName); if (! img.isNull ()) {img = img.scaled (width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); painter.drawImage (0,0, img) } / / calculate the font QFont font; font.setPixelSize (height * 0.37); font.setBold (true); / / automatically calculate the text drawing area and draw the defense area number QRectF rect = this- > rect (); if (buttonStyle = = ButtonStyle_Police) {double y = (30 * height / 60); rect = QRectF (0, y, width, height-y) } else if (buttonStyle = = ButtonStyle_Bubble) {double y = (8 * height / 60); rect = QRectF (0,0, width, height-y);} else if (buttonStyle = = ButtonStyle_Bubble2) {double y = (13 * height / 60); rect = QRectF (0,0, width, height-y); font.setPixelSize (width * 0.33) } else if (buttonStyle = = ButtonStyle_Msg) {double y = (17 * height / 60); rect = QRectF (0,0, width, height-y);} else if (buttonStyle = = ButtonStyle_Msg2) {double y = (17 * height / 60); rect = QRectF (0,0, width, height-y);} / / draw text logo painter.setFont (font); painter.setPen (Qt::white) Painter.drawText (rect, Qt::AlignCenter, text);} bool ButtonDefence::eventFilter (QObject * watched, QEvent * event) {if (canMove) {static QPoint lastPoint; static bool isPressed = false; if (event- > type () = = QEvent::MouseButtonPress) {QMouseEvent * e = static_cast (event) If (this- > rect (). Contains (e-> pos ()) & & (e-> button () = = Qt::LeftButton)) {lastPoint = e-> pos (); isPressed = true;}} else if (event- > type () = = QEvent::MouseMove & isPressed) {QMouseEvent * e = static_cast (event) Int dx = e-> pos (). X ()-lastPoint.x (); int dy = e-> pos (). Y ()-lastPoint.y (); this- > move (this- > x () + dx, this- > y () + dy); return true;} else if (event- > type () = = QEvent::MouseButtonRelease & isPressed) {isPressed = false }} if (event- > type () = = QEvent::MouseButtonPress) {emit clicked ();} else if (event- > type () = = QEvent::MouseButtonDblClick) {emit doubleClicked ();} return QWidget::eventFilter (watched, event);} III.
At this point, the study on "how to achieve universal button map effect in Qt" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!