python - QAbstractButton Image streches over full width of the column -


i subclassing qabstractbutton class create clickable icon accesses user files. did following tutorial here: https://coolchevy.org.ua/2016/06/20/basic-example-how-to-code-a-image-button-in-pyqt/

the problem running qabsractbutton size. adding widget qgridlayout. on grid, right above button greater width. image takes size of column , stretches shown here

is there way adjust size of qabstractbutton before added screen? have tried .setmaximumwidth, doesn't have method. thanks!

update w/ code

def init_ui(self):     self.settings_button=qpushbutton("email settings")     self.templates_button=templatebutton(qpixmap('templates.png'))     self.layout.addwidget(self.templates_button,0,4)     self.layout.addwidget(self.settings_button,1,4)`  class templatebutton(qabstractbutton):     def __init__(self,pixmap):         super(templatebutton, self).__init__()         self.pixmap = pixmap      def paintevent(self, event):         pix = self.pixmap         if self.isdown():             print("click")         painter = qpainter(self)         painter.drawpixmap(event.rect(), pix)      def enterevent(self, event):         self.update()     def leaveevent(self, event):         self.update()     def sizehint(self):         return qsize(50,50) 

in case must establish size policy widget, using method setsizepolicy(), policies can established follows:

qsizepolicy::fixed: qwidget::sizehint() acceptable alternative, widget can never grow or shrink (e.g. vertical direction of push button).

qsizepolicy::minimum: sizehint() minimal, , sufficient. widget can expanded, there no advantage being larger (e.g. horizontal direction of push button). cannot smaller size provided sizehint().

qsizepolicy::maximum: sizehint() maximum. widget can shrunk amount without detriment if other widgets need space (e.g. separator line). cannot larger size provided sizehint().

qsizepolicy::preferred: sizehint() best, widget can shrunk , still useful. widget can expanded, there no advantage being larger sizehint() (the default qwidget policy).

qsizepolicy::expanding: sizehint() sensible size, widget can shrunk , still useful. widget can make use of space, should space possible (e.g. horizontal direction of horizontal slider).

qsizepolicy::minimumexpanding: sizehint() minimal, , sufficient. widget can make use of space, should space possible (e.g. horizontal direction of horizontal slider).

qsizepolicy::ignored sizehint() ignored. widget space possible.

he policies widgets have default qsizepolicy::preferred, in case should use qsizepolicy::fixed. in addition must establish alignment centered, above following:

self.settings_button=qpushbutton("email settings") self.templates_button=templatebutton(qpixmap('qt_extended_48x48.png')) self.templates_button.setsizepolicy(qsizepolicy.fixed, qsizepolicy.fixed) self.layout.addwidget(self.templates_button,0,4, qt.aligncenter) self.layout.addwidget(self.settings_button,1,4) 

screenshots:

enter image description here

enter image description here


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -