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

Qt custom button and the switch of pictures in different states

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

I haven't used Qt for a long time. I recently made a custom button when I was doing a form. At first, I wanted to achieve it by modifying its MASK and ICON. It is true that the effect is not always satisfactory, such as simply defining a XPushButton. The record of its implementation is also released. In order to facilitate their own use in the future and give a small hint to friends with corresponding problems. To implement arbitrarily shaped forms and retain the features of QPushButton, inherit QPushButton to create a subclass. Class QtXPushButton: public QPushButton {Q_OBJECTpublic: QtXPushButton (QString strImagePath, QWidget * parent = NULL); ~ QtXPushButton ();}

In order to easily describe the normal button, mouse slide, select state, do not click state to define a state enumeration.

/ / Button status

Enum XBUTTONSTATE

{

NORMAL = 0X01 / normal status

HOVER = 0X02 reminder / mouse over statu

SELECTED = 0X04sparing / selected status

DISABLE = 0X08// No Click statu

}

In order to easily set the status icon of each program, add a status icon to set the interface, and set an identification to indicate which states are set.

/ / set the normal icon

Void SetNormalPixmap (QString strImagePath)

/ / set the mouse slide picture

Void SetHoverPixmap (QString strImagePath)

/ / set the selected status picture

Void SetSelectedPixmap (QString strImagePath)

/ / set to disable clicking on icon

Void SetDisablePixmap (QString strImagePath)

/ / set the button's current status

Void SetBtnState (XBUTTONSTATE state)

/ / set the image size

Void SetSize (QSize sz)

At this point, an interface with a button with normal settings, mouse sliding, selecting, and no clicking is defined. The final definition of this subclass is as follows.

# pragma once

# include

# include

# include

# include

/ / Button status

Enum XBUTTONSTATE

{

NORMAL = 0X01 / normal status

HOVER = 0X02 reminder / mouse over statu

SELECTED = 0X04sparing / selected status

DISABLE = 0X08// No Click statu

}

Class QtXPushButton: public QPushButton

{

Q_OBJECT

Public:

QtXPushButton (QString strImagePath, QWidget * parent = NULL)

~ QtXPushButton ()

/ / set the normal icon

Void SetNormalPixmap (QString strImagePath)

/ / set the mouse slide picture

Void SetHoverPixmap (QString strImagePath)

/ / set the selected status picture

Void SetSelectedPixmap (QString strImagePath)

/ / set to disable clicking on icon

Void SetDisablePixmap (QString strImagePath)

/ / set the button's current status

Void SetBtnState (XBUTTONSTATE state)

/ / set the image size

Void SetSize (QSize sz)

Protected:

Virtual void paintEvent (QPaintEvent * event)

Virtual void enterEvent (QEvent * event)

Virtual void leaveEvent (QEvent * event)

Private:

QtXPushButton (const QtXPushButton& btn)

QtXPushButton& operator= (const QtXPushButton& btn)

Private:

QPixmap masked NormalPixbank / normal icon

QPixmap massively HoverPixbomacter / mouse slide icon

QPixmap masked selected PixPlacement / selected status icon

QPixmap massively DisablePixampact / do not click on icon

Int makeshift / contains 1 startup normal icon, 2 enable slide icon, 4 enable selected status icon, 8 enable no click icon, default is 1.

XBUTTONSTATE massively curStatepole / current status

XBUTTONSTATE masked lastStatebank / previous status

}

Then realize its corresponding function. The implementation of an irregular form is so large that it requires two actions.

(1) determine the border through REGON or MASK, and add the following code to the constructor:

QtXPushButton::QtXPushButton (QString strImagePath, QWidget * parent)

: QPushButton (parent)

{

M_NormalPix.load (strImagePath)

Resize (m_NormalPix.size ())

SetMask (QBitmap (m_NormalPix.mask ()

M_iMask = XBUTTONSTATE::NORMAL

M_curState = XBUTTONSTATE::NORMAL

M_lastState = XBUTTONSTATE::NORMAL

}

(2) when the form is drawn, draw the picture on it, reload its drawing function, and add the following code.

Void QtXPushButton::paintEvent (QPaintEvent * event)

{

QPainter painter (this)

Painter.drawPixmap (0,0, m_NormalPix)

}

The above two steps can implement a custom form with arbitrary rules, which can be used to switch pictures in different states.

Set up pictures in different states. Here, take mouse sliding as an example. Since the mouse should be set to the mouse sliding state after entering the form region, reload the form's mouse entry and mouse departure from the area to monitor its status.

/ / set the mouse slide picture

Void QtXPushButton::SetHoverPixmap (QString strImagePath)

{

M_HoverPix.load (strImagePath)

M_iMask | = XBUTTONSTATE::HOVER

}

/ / reload mouse entry and removal events

Void QtXPushButton::enterEvent (QEvent * event)

{

SetBtnState (XBUTTONSTATE::HOVER)

QPushButton::enterEvent (event)

Update ()

}

Void QtXPushButton::leaveEvent (QEvent * event)

{

M_curState = m_lastState

QPushButton::leaveEvent (event)

Update ()

}

/ / redraw the form with the specified picture

Void QtXPushButton::paintEvent (QPaintEvent * event)

{

QPixmap drawPix

If (m_curState = = XBUTTONSTATE::NORMAL)

{

DrawPix = m_NormalPix

}

Else if (m_curState = = XBUTTONSTATE::HOVER)

{

Int iValue = m_iMask&XBUTTONSTATE::HOVER

DrawPix = (0 = = iValue)? M_NormalPix: m_HoverPix

}

Else if (m_curState = = XBUTTONSTATE::SELECTED)

{

Int iValue = m_iMask&XBUTTONSTATE::SELECTED

DrawPix = (0 = = iValue)? M_NormalPix: m_SelectedPix

}

Else if (m_curState = = XBUTTONSTATE::DISABLE)

{

Int iValue = m_iMask&XBUTTONSTATE::DISABLE

DrawPix = (0 = = iValue)? M_NormalPix: m_DisablePix

}

QPainter painter (this)

Painter.drawPixmap (0,0, drawPix)

}

After the above process, the mouse enters the button area, and the button will display a HOVER picture. Move the mouse out of the button area and the button displays the state it was in before the mouse entered.

The complete source code of the program is as follows:

# include "QtXPushButton.h"

# include

# include

QtXPushButton::QtXPushButton (QString strImagePath, QWidget * parent)

: QPushButton (parent)

{

M_NormalPix.load (strImagePath)

Resize (m_NormalPix.size ())

SetMask (QBitmap (m_NormalPix.mask ()

M_iMask = XBUTTONSTATE::NORMAL

M_curState = XBUTTONSTATE::NORMAL

M_lastState = XBUTTONSTATE::NORMAL

}

QtXPushButton::~QtXPushButton ()

{

}

/ / set the normal icon

Void QtXPushButton::SetNormalPixmap (QString strImagePath)

{

M_NormalPix.load (strImagePath)

M_iMask | = XBUTTONSTATE::NORMAL

}

/ / set the mouse slide picture

Void QtXPushButton::SetHoverPixmap (QString strImagePath)

{

M_HoverPix.load (strImagePath)

M_iMask | = XBUTTONSTATE::HOVER

}

/ / set the selected status picture

Void QtXPushButton::SetSelectedPixmap (QString strImagePath)

{

M_SelectedPix.load (strImagePath)

M_iMask | = XBUTTONSTATE::SELECTED

}

/ / set to disable clicking on icon

Void QtXPushButton::SetDisablePixmap (QString strImagePath)

{

M_DisablePix.load (strImagePath)

M_iMask | = XBUTTONSTATE::DISABLE

}

/ / set the button's current status

Void QtXPushButton::SetBtnState (XBUTTONSTATE state)

{

M_lastState = m_curState

M_curState = state

}

/ / set the image size

Void QtXPushButton::SetSize (QSize sz)

{

M_NormalPix = m_NormalPix.scaled (sz)

Int iValue = m_iMask&XBUTTONSTATE::HOVER

If (iValue! = 0)

{

M_HoverPix = m_HoverPix.scaled (sz)

}

IValue = m_iMask&XBUTTONSTATE::SELECTED

If (iValue! = 0)

{

M_SelectedPix = m_SelectedPix.scaled (sz)

}

IValue = m_iMask&XBUTTONSTATE::DISABLE

If (iValue! = 0)

{

M_DisablePix = m_DisablePix.scaled (sz)

}

}

Void QtXPushButton::paintEvent (QPaintEvent * event)

{

QPixmap drawPix

If (m_curState = = XBUTTONSTATE::NORMAL)

{

DrawPix = m_NormalPix

}

Else if (m_curState = = XBUTTONSTATE::HOVER)

{

Int iValue = m_iMask&XBUTTONSTATE::HOVER

DrawPix = (0 = = iValue)? M_NormalPix: m_HoverPix

}

Else if (m_curState = = XBUTTONSTATE::SELECTED)

{

Int iValue = m_iMask&XBUTTONSTATE::SELECTED

DrawPix = (0 = = iValue)? M_NormalPix: m_SelectedPix

}

Else if (m_curState = = XBUTTONSTATE::DISABLE)

{

Int iValue = m_iMask&XBUTTONSTATE::DISABLE

DrawPix = (0 = = iValue)? M_NormalPix: m_DisablePix

}

QPainter painter (this)

Painter.drawPixmap (0,0, drawPix)

}

Void QtXPushButton::enterEvent (QEvent * event)

{

SetBtnState (XBUTTONSTATE::HOVER)

QPushButton::enterEvent (event)

Update ()

}

Void QtXPushButton::leaveEvent (QEvent * event)

{

M_curState = m_lastState

QPushButton::leaveEvent (event)

Update ()

}

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