How to insert QWidgets in the middle of a Layout?

后端 未结 1 1325
广开言路
广开言路 2021-01-14 21:50

I\'m using the Qt framework to build my graphical user interface. I use a QGridLayout to position my QWidgets neatly.
The GUI looks like this:

My

1条回答
  •  梦毁少年i
    2021-01-14 22:53

    Thank you @ekhumoro, @Stuart Fisher, @vahancho and @mbjoe for your help. I eventually found a way to solve the issue. I no longer use the QGridLayout(). Instead, I built a wrapper around the QVBoxLayout to behave as if it was a GridLayout, with an extra function to insert new rows:

    class CustomGridLayout(QVBoxLayout):
        def __init__(self):
            super(CustomGridLayout, self).__init__()
            self.setAlignment(Qt.AlignTop)  # !!!
            self.setSpacing(20)
    
    
        def addWidget(self, widget, row, col):
            # 1. How many horizontal layouts (rows) are present?
            horLaysNr = self.count()
    
            # 2. Add rows if necessary
            if row < horLaysNr:
                pass
            else:
                while row >= horLaysNr:
                    lyt = QHBoxLayout()
                    lyt.setAlignment(Qt.AlignLeft)
                    self.addLayout(lyt)
                    horLaysNr = self.count()
                ###
            ###
    
            # 3. Insert the widget at specified column
            self.itemAt(row).insertWidget(col, widget)
    
        ''''''
    
        def insertRow(self, row):
            lyt = QHBoxLayout()
            lyt.setAlignment(Qt.AlignLeft)
            self.insertLayout(row, lyt)
    
        ''''''
    
        def deleteRow(self, row):
            for j in reversed(range(self.itemAt(row).count())):
                self.itemAt(row).itemAt(j).widget().setParent(None)
            ###
            self.itemAt(row).setParent(None)
    
        def clear(self):
            for i in reversed(range(self.count())):
                for j in reversed(range(self.itemAt(i).count())):
                    self.itemAt(i).itemAt(j).widget().setParent(None)
                ###
            ###
            for i in reversed(range(self.count())):
                self.itemAt(i).setParent(None)
            ###
    
        ''''''
    

    0 讨论(0)
提交回复
热议问题