How to make the child(Qlistwidget) fill the blank part of the parent(QStackedWidget)?

#MainWindow code
from main_ui import Ui_MainWindow
from widgets.subwidgets import SubWidget
class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        global widgets
        widgets = self.ui
        # APPLY TEXTS
        self.setWindowTitle(title)
        widgets.titleRightInfo.setText(description) 
        self.ui.extraContent = SubWidget(self.ui.extraContent) #####******this
        # self.ui.extraContent is QStackedWidget. 
#subwidget
class SubWidget(QStackedWidget):
    def __init__(self,parent=None):
        super(SubWidget, self).__init__(parent)
        self.parent = parent
        self.init_ui()
        self.setCurrentIndex(0)

    def init_ui(self):
        # create mainwindow
        #self.page1 = QFrame()
        #self.setFixedSize(230,650)
        self.setMinimumWidth(230)
        self.setMaximumWidth(230)
        self.setMinimumHeight(650)
        self.setMaximumHeight(3000)
        self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Expanding)
        self.page1 = QWidget()
        basic_settings = QVBoxLayout()
        row_one = QHBoxLayout()
        self.open_btn   = QPushButton()
        self.open_btn.setIcon(QIcon("images/src_icons/open.png"))
        self.export_btn   = QPushButton()
        self.export_btn.setIcon(QIcon("images/src_icons/save.png"))
        row_one.addWidget(self.open_btn)
        row_one.addWidget(self.export_btn)

        row_two = QHBoxLayout()
        self.prev_btn   = QPushButton()
        self.prev_btn.setIcon(QIcon("images/src_icons/previous.png"))
        

        self.next_btn   = QPushButton()
        self.next_btn.setIcon(QIcon("images/src_icons/next.png"))
        row_two.addWidget(self.prev_btn)
        row_two.addWidget(self.next_btn)

        row_three = QHBoxLayout()
        self.zoom_out = QToolButton()
        self.zoom_out.setIcon(QIcon("images/src_icons/zoom-out.png"))
        self.zoom_in = QToolButton()
        self.zoom_in.setIcon(QIcon("images/src_icons/zoom-in.png"))

        self.zoom_widget = ZoomWidget(value=100)
        self.zoom_widget.setWhatsThis(
            u"Zoom in or out of the image. Also accessible with"
            " %s and %s from the canvas." % (format_shortcut("Ctrl+[-+]"),
                                            format_shortcut("Ctrl+Wheel")))
        self.zoom_widget.setEnabled(False)

        row_three.addWidget(self.zoom_out)
        row_three.addWidget(self.zoom_in)
        row_three.addWidget(self.zoom_widget)


        row_four = QHBoxLayout()
        label    = QLabel()
        label.setStyleSheet("QLabel{color: #333; font-size: 16px;background-color: #f0f0f0; border: 1px solid #ccc; border-radius: 5px;text-align: center;}""QLabel:hover {color: #409eff; text-decoration: underline;}")
        self.finish_btn   = QPushButton()
        self.finish_btn.setIcon(QIcon("images/src_icons/finish.png"))
        row_four.addWidget(label)
        row_four.addWidget(self.finish_btn)

        row_five = QHBoxLayout()
        self.undo_btn   = QPushButton()
        self.undo_btn.setIcon(QIcon("images/src_icons/undo.png"))
        
        self.reset_btn  = QPushButton()
        self.reset_btn.setIcon(QIcon("images/src_icons/reset.png"))
        row_five.addWidget(self.undo_btn)
        row_five.addWidget(self.reset_btn)

        row_six = QVBoxLayout()
        self.image_listWidget = CustomListWidget() #####******this
        self.listWidget_label = QLabel()
        self.listWidget_label.setText("图像列表")
        row_six.addWidget(self.listWidget_label)
        row_six.addWidget(self.image_listWidget)  

        basic_settings.addLayout(row_one)
        basic_settings.addLayout(row_two)
        basic_settings.addLayout(row_three)
        basic_settings.addLayout(row_four)
        basic_settings.addLayout(row_five)
        basic_settings.addLayout(row_six)
        basic_function = QVBoxLayout()
        basic_function.addLayout(basic_settings)

        self.page1.setLayout(basic_function)
        self.addWidget(self.page1)

self.ui.extraContent is a QStackedWidget that I drew with qtdesigner and then overwrote its child elements with SubWidget. I added a lot of child elements to the subwidget.
How do I make the “self.image_listWidget” autofill as “self.ui.extraContent” scales? How do I make listwidget scale with its parent?
How do I make listwidget scale with its parent? I hope you can give me a reply. Thank you very much

  • If self.ui.extraContent already is a QStackedWidget, you should certainly not “overwrite” it, nor create a further QStackedWidget. Instead, make SubWidget directly inherit from QWidget, remove self.page1 and just use self.setLayout(basic_settings) (basic_function is useless), finally do self.ui.extraContent.addPage() with the instance of SubWidget.

    – 

  • (In pyside6, qstackedwidget can try .addWidget() method) Thank you very much for your reply, which solved my problem (by the way, you also helped solve my problem last time).

    – 

  • Yes, sorry, it is addWidget(), not addPage() (even in Qt5). Qt has thousands of functions, it’s easy to get confused sometimes 😉 Note that, since you created the QStackedWidget in Designer, it probably has already one “page” at least, which you may not need nor want. Check the “Object inspector” and see if the stacked widget has child elements: if it does, they are empty and you don’t need them (because you only add them programmatically), then you should remove them; just right click on the stacked widget item and remove all the pages you don’t need (see the “Page x of x” submenu).

    – 

Leave a Comment