qt - Resize QDialog when RadioButton is Checked PyQt -


not many experience @ pyqt..

i have designed ui qt designer 2 radiobuttons.

depending on radiobutton selected widgets visible , other not. i'm not trying resize automatically layout of dialog depending on how many widgets visible in ui.

the question similar this one . here extract of code might helpful understand problem (i know piece of code, entire dialog pretty long):

class mydialog(qdialog, form_class):     ..........      # connect radiobutton function reloads ui     self.radiosingle.toggled.connect(self.refreshwidgets)       .....      # dictionary widgets , values     self.widgettype = {             self.cmbmodelname: ['all'],             self.cmbgridlayer: ['all'],             self.cmbriverlayer: ['all'],             self.linenewlayeredit: ['all'],             self.linelayernumber: ['single']         }  # function refresh ui correct widgets depending on radiobutton selected     def refreshwidgets(self, idx):         '''         refresh ui widgets depending on radiobutton chosen         '''           tp = self.radiosingle.ischecked()          k, v in self.widgettype.items():             if tp:                 if 'all' or 'single' in v:                     active = true                 k.setenabled(active)                 k.setvisible(active)             else:                 active = 'all' in v                 k.setenabled(active)                 k.setvisible(active)          # attempt resize dialog         self.setsizeconstraint() 

surely code improved (not skilled code writing)...

you gotta set_up ui resize when it's needed. there lot of more beautiful way this, here simple , fast example:

you have define layout behave way want. if make use of minimum , maximum size it'll resize need. have own personalized buttons, widgets, .... have cleaner code.

import sys  pyqt5.qtwidgets import qapplication pyqt5.qtwidgets import qdialog pyqt5.qtwidgets import qhboxlayout pyqt5.qtwidgets import qradiobutton pyqt5.qtwidgets import qvboxlayout pyqt5.qtwidgets import qwidget   class customdialog(qdialog):      w_container = none     v_layout_container = none     v_scroll_area = none     v_layout_preview = none       def __init__(self):         """init ui."""          super(customdialog, self).__init__()         self.init_ui()      def init_ui(self):         """init ui object requirements."""          self.r1 = qradiobutton("smaller")         self.r2 = qradiobutton("bigger")          style = "background: green;"         size = [200, 50]          self.small_size = [200, 250]         self.big_size = [200, 500]          self.resize(200,200)          self.w1 = qwidget()         self.w2 = qwidget()         self.w3 = qwidget()         self.w4 = qwidget()         self.w5 = qwidget()         self.w6 = qwidget()         self.w1.setstylesheet(style)         self.w2.setstylesheet(style)         self.w3.setstylesheet(style)         self.w4.setstylesheet(style)         self.w5.setstylesheet(style)         self.w6.setstylesheet(style)         self.w1.setfixedsize(size[0], size[1])         self.w2.setfixedsize(size[0], size[1])         self.w3.setfixedsize(size[0], size[1])         self.w4.setfixedsize(size[0], size[1])         self.w5.setfixedsize(size[0], size[1])         self.w6.setfixedsize(size[0], size[1])          self.full_container = qvboxlayout()         self.full_container.setcontentsmargins(0,0,0,0)          self.radio_widget = qwidget()         self.radio_widget.setfixedsize(200, 50)         self.radio_container = qhboxlayout()         self.radio_container.setcontentsmargins(0,0,0,0)          self.widget_widget = qwidget()         self.widget_container = qvboxlayout()          self.radio_widget.setlayout(self.radio_container)         self.widget_widget.setlayout(self.widget_container)          self.full_container.addwidget(self.radio_widget)         self.full_container.addwidget(self.widget_widget)          self.radio_container.addwidget(self.r1)         self.radio_container.addwidget(self.r2)          self.widget_container.addwidget(self.w1)         self.widget_container.addwidget(self.w2)         self.widget_container.addwidget(self.w3)         self.widget_container.addwidget(self.w4)         self.widget_container.addwidget(self.w5)         self.widget_container.addwidget(self.w6)          self.setlayout(self.full_container)          self.r1.setchecked(true)      def paintevent(self, event):         if self.r1.ischecked():             self.w4.hide()             self.w5.hide()             self.w6.hide()             self.setfixedsize(self.small_size[0],self.small_size[1])         elif self.r2.ischecked():             self.w4.show()             self.w5.show()             self.w6.show()             self.setfixedsize(self.big_size[0], self.big_size[1])         self.full_container.update()         super(customdialog, self).paintevent(event)  def run():     app = qapplication(sys.argv)     gui = customdialog()     gui.show()     sys.exit(app.exec_())   run() 

observe i'm using pyqt5, if need gotta change imports version using.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -