Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the effect of Universal Button Map by Qt

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report