Haru
Haru

Reputation: 2083

How to change sizeHint after showing widgets in actual?

I made a widgetAction.

I set sizeHint (100, 30) as a Default setting.

I set the widgetAction to the menu of QPushButton()

the QPushButton is aligned by QVBoxLayout()

So, the size of QPushButton is often changed by stretching... resizing... minimized... and so on.

But the labels of widgetAction are not changed.

Is there a way to change the size of sizeHint() after showing it?

I want to resize the label of widgetAction to the same size of QPushButton

the menu of widgetAction is not the same size of the menu button

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()

Upvotes: 2

Views: 1373

Answers (1)

Dimitry Ernot
Dimitry Ernot

Reputation: 6594

Your action is embedded in a QWidgetAction. So, you need to resize the parent widget of your self.label_A, also.

If you add the line self.parent().resize(self.initial_size) at the end of UnderlineStyleLabel.mousePressEvent, the label will be well resized.

Upvotes: 2

Related Questions