我做了一个widgetAction
。
我将sizeHint (100, 30)
设置为默认设置。
我将widgetAction
设置为QPushButton()
的菜单
QPushButton由QVBoxLayout()
对齐
因此,QPushButton
的大小通常通过拉伸,调整大小,最小化等等来更改。
但是widgetAction的标签未更改。
在显示sizeHint()
后是否可以更改其大小?
我想将widgetAction
的标签大小调整为QPushButton
的相同大小
from PySide import QtGui
from PySide import QtCore
import sys
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.menubar = QtGui.QMenuBar()
self.a_menu = QtGui.QMenu("a-menu")
self.label_A = UnderlineStyleLabel(self.a_menu,QtGui.QTextCharFormat.WaveUnderline)
self.widgetAction = QtGui.QWidgetAction(self.a_menu)
self.widgetAction.createWidget(self.label_A)
self.widgetAction.setDefaultWidget(self.label_A)
self.a_menu.addAction(self.widgetAction)
self.menubar.addMenu(self.a_menu)
self.setMenuBar(self.menubar)
self.show()
class UnderlineStyleLabel(QtGui.QWidget):
def __init__(self,parent=None,underlineStyle=QtGui.QTextCharFormat.NoUnderline):
super(UnderlineStyleLabel, self).__init__(parent=None)
self.setParent(parent)
self.pen_linepath = QtGui.QPainterPath(QtCore.QPointF(0, 0))
self.pen_linepath.lineTo(100, 0)
self.underlineStyle = underlineStyle
self.initial_size = QtCore.QSize(100, 30)
def sizeHint(self):
return self.initial_size
def mousePressEvent(self, event):
self.initial_size = QtCore.QSize(200,30)
print("mousePressEvent pressed! please change the size!")
def paintEvent(self,event):
pen = QtGui.QPen()
if self.underlineStyle == QtGui.QTextCharFormat.WaveUnderline:
pen = QtGui.QPen()
pen.setStyle(QtCore.Qt.PenStyle.SolidLine)
pen.setWidth(10)
pen.setColor(QtCore.Qt.black)
painter = QtGui.QPainter()
if not painter.isActive():
painter.begin(self)
painter.setPen(pen)
painter.setRenderHint(QtGui.QPainter.RenderHint.Antialiasing)
painter.drawArc(10, 10, 10, 10, 480, 1920)
painter.drawArc(20, 7, 10, 10, -480, -1920)
painter.drawArc(30, 10, 10, 10, 480, 1920)
painter.drawArc(40, 7, 10, 10, -480, -1920)
painter.drawArc(50, 10, 10, 10, 480, 1920)
painter.drawArc(60, 7, 10, 10, -480, -1920)
painter.end()
return QtGui.QWidget.paintEvent(self,event)
def main():
try:
QtGui.QApplication([])
except Exception as e:
print(18,e)
w = Window()
sys.exit(QtGui.QApplication.exec_())
if __name__ == "__main__":
main()
答案 0 :(得分:1)
您的操作已嵌入QWidgetAction
中。因此,您还需要调整self.label_A
的父窗口小部件的大小。
如果在self.parent().resize(self.initial_size)
的末尾添加行UnderlineStyleLabel.mousePressEvent
,标签的大小将被适当调整。