The realization is shown below:
This text is written in two main ways:
The first is the customization of buttons and the second is the use of context menus, not only for buttons, but also for some other controls that can be similarly created and used.
Regarding the right-click menu, there are some ways to use QMenu:
The use of style sheets:
("QMenu{background:purple;}" "QMenu{border:1px solid lightgray;}" "QMenu{border-color:green;}" "QMenu::item{padding:0px 40px 0px 20px;}" "QMenu::item{height:30px;}" "QMenu::item{color:blue;}" "QMenu::item{background:white;}" "QMenu::item{margin:1px 0px 0px 0px;}" "QMenu::item:selected:enabled{background:lightgray;}" "QMenu::item:selected:enabled{color:white;}" "QMenu::item:selected:!enabled{background:transparent;}" "QMenu::separator{height:50px;}" "QMenu::separator{width:1px;}" "QMenu::separator{background:white;}" "QMenu::separator{margin:1px 1px 1px 1px;}" "QMenu#menu{background:white;}" "QMenu#menu{border:1px solid lightgray;}" "QMenu#menu::item{padding:0px 40px 0px 30px;}" "QMenu#menu::item{height:25px;}" "QMenu#menu::item:selected:enabled{background:lightgray;}" "QMenu#menu::item:selected:enabled{color:white;}" "QMenu#menu::item:selected:!enabled{background:transparent;}" "QMenu#menu::separator{height:1px;}" "QMenu#menu::separator{background:lightgray;}" "QMenu#menu::separator{margin:2px 0px 2px 0px;}" "QMenu#menu::indicator {padding:10px;}" )
Right-click menu creation and signal slots for menus:
def createContextMenu(self): ''''' Creating a right-click menu ''' # ContextMenuPolicy must be set to # Otherwise the customContextMenuRequested signal is not available () () # Create QMenu = QMenu(self) = (QIcon("images/"),u'| Action A') = (QIcon("images/"),u'| Action B') = (QIcon("images/"),u'| Action C') #Add secondary menu = (QIcon("images/"),u"| Secondary Menu") = (QIcon("images/"),u'| Action A') = (QIcon("images/"),u'| Action B') = (QIcon("images/"),u'| Action C') # Associate actions with handler functions # Here, for simplicity, associate all actions with the same handler function. # Of course it's also possible to associate them with separate functions to achieve different functionality () () () () () ()
The display position of the menu:
.exec_(()) #Display at mouse position
The customization of the buttons includes redefining some events and redesigning and drawing the ui interface of the buttons, so I won't list them all.
The following is a demo including the customization of the button, the creation and use of the context menu, including two files, the image can be found at random, not too large or too small on the line:
# -*- coding: utf-8 -*- from import Qt, QRect from import QPushButton, QPainter, QPainterPath, QPen, QColor, QPixmap, QIcon, QBrush, QCursor,QMenu class MenuButton(QPushButton): def __init__(self,parent = None): super(MenuButton,self).__init__(parent) ("QMenu{background:purple;}" "QMenu{border:1px solid lightgray;}" "QMenu{border-color:green;}" "QMenu::item{padding:0px 40px 0px 20px;}" "QMenu::item{height:30px;}" "QMenu::item{color:blue;}" "QMenu::item{background:white;}" "QMenu::item{margin:1px 0px 0px 0px;}" "QMenu::item:selected:enabled{background:lightgray;}" "QMenu::item:selected:enabled{color:white;}" "QMenu::item:selected:!enabled{background:transparent;}" "QMenu::separator{height:50px;}" "QMenu::separator{width:1px;}" "QMenu::separator{background:white;}" "QMenu::separator{margin:1px 1px 1px 1px;}" "QMenu#menu{background:white;}" "QMenu#menu{border:1px solid lightgray;}" "QMenu#menu::item{padding:0px 40px 0px 30px;}" "QMenu#menu::item{height:25px;}" "QMenu#menu::item:selected:enabled{background:lightgray;}" "QMenu#menu::item:selected:enabled{color:white;}" "QMenu#menu::item:selected:!enabled{background:transparent;}" "QMenu#menu::separator{height:1px;}" "QMenu#menu::separator{background:lightgray;}" "QMenu#menu::separator{margin:2px 0px 2px 0px;}" "QMenu#menu::indicator {padding:10px;}" ) = False = False = QIcon() = QColor() = 1.0 = 0 # (True) # ("#Check {background-color: rgb(255, 255, 255);}"); () = 0 def createContextMenu(self): ''''' Creating a right-click menu ''' # ContextMenuPolicy must be set to # Otherwise the customContextMenuRequested signal is not available () () # Create QMenu = QMenu(self) = (QIcon("images/"),u'| Action A') = (QIcon("images/"),u'| Action B') = (QIcon("images/"),u'| Action C') # Add secondary menu = (QIcon("images/"),u"| Secondary Menu") = (QIcon("images/"),u'| Action A') = (QIcon("images/"),u'| Action B') = (QIcon("images/"),u'| Action C') # Associate actions with handler functions # Here, for simplicity, associate all actions with the same handler function. # Of course it's also possible to associate them with separate functions to achieve different functionality () () () () () () def showContextMenu(self, pos): ''''' Functions called on right-click ''' +=1 # Before the menu is displayed, move it to the mouse click position .exec_(()) #Display at mouse position #() print def actionHandler(self): ''''' Functions called by specific actions in the menu ''' if %3==1: (u"first") elif %3==2: (u"second") elif %3==0: (u"third") def setEnterCursorType(self, Type): = Type def setColor(self,color): = color def setOpacitys(self,opacity): = opacity # (0.5) def enterEvent(self,event): = True () (self,event) def leaveEvent(self,event): = False () (QCursor()) (self,event) def mousePressEvent(self, event): = True () (self,event) def mouseReleaseEvent(self, event): = False () (self,event) def paintEvent(self,event): painter = QPainter(self) btnRect = () iconRect = () color = QColor() if : color = if : color = (120) (QPen(QColor(),2)) outline = QPainterPath() (0, 0, (), (), 0, 0) (1) (outline) (QBrush(color)) () painter_path = QPainterPath() painter_path.addRoundedRect(1, 1, () - 2, () - 2, 0, 0) if : (painter_path) (1, 1, () - 2, () - 2, 0, 0) (1) iconPos,textPos = (btnRect, iconRect) # Redraw text if not ().isNull(): (()) (QPen(QColor(),2)) ((), (), (), (), , ()) # Redraw icons if not ().isNull(): (iconPos, QPixmap(().pixmap(()))) # Calculate icon and text size position def calIconTextPos(self,btnSize,iconSize): if ().isNull(): iconWidth = ()*3/5 iconHeight = ()*3/5 else: iconWidth = () iconHeight = () - 50 iconX = (()-iconWidth)/2 iconY = (()-iconHeight)/2 iconPos = QRect() (iconX) (iconY) (iconWidth) (iconHeight) textPos = QRect() if not ().isNull(): (iconX) (()- 50) (iconWidth) (50) return (iconPos,textPos) 1 # -*- coding: utf-8 -*- from mybutton import MenuButton import sys from import QTextCodec, QSize, SIGNAL from import QDialog, QIcon, QHBoxLayout, QApplication (("utf8")) class TestDialog(QDialog): def __init__(self,parent=None): super(TestDialog,self).__init__(parent) (200,200) = MenuButton() (QSize(100,100)) (QIcon("")) (QSize(100,100)) #(("Chosa")) (, SIGNAL("clicked()"),) myLayout = QHBoxLayout() () (myLayout) def cancel(self): () app=QApplication() dialog=TestDialog() () app.exec_()
summarize
The above is a small introduction to the python of PyQt button right-click menu function of the realization of the code, I hope to help you, if you have any questions welcome to leave me a message, I will reply to you in a timely manner!