为什么我的QtWidgets没有出现?

时间:2016-01-15 16:59:17

标签: python qt pyqt

我的GLWidget(继承自QtOpenGL.QGLWidget)没有出现在屏幕上,而它应该在其他小部件的右侧可见。但是,当我从水平布局切换时:

 layout_final = Qtgui.QHBoxLayout() 

到垂直的:

 layout_final = Qtgui.QVBoxLayout() 

我的GL小部件确实出现在其他小部件下面,但我希望它位于其他小部件的右侧。

class MainWindow(QtGui.QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()


        # create layout
        layout_before_final = QtGui.QVBoxLayout()
        layout_final = QtGui.QHBoxLayout()
        layout = QtGui.QGridLayout()
        layout1 = QtGui.QGridLayout()
        self.groupBox = QtGui.QGroupBox('Set HDF5 file')
        self.groupBox1 = QtGui.QGroupBox('Current HDF5 file')

        # instance widgets
        # first box
        self.setH5Button = QtGui.QPushButton('set H5')
        self.currentH5LineEdit = QtGui.QLineEdit('')
        layout.addWidget(self.setH5Button, 0,0)
        layout.addWidget(self.currentH5LineEdit, 0, 1)
        self.groupBox.setLayout(layout)

        # second box
        self.channelsLabel = QtGui.QLabel('Channel')
        self.channelsComboBox = QtGui.QComboBox()
        self.levelsLabel = QtGui.QLabel('Level')
        self.levelsComboBox = QtGui.QComboBox()      


        layout1.addWidget(self.channelsLabel, 0, 0, 1, 1)
        layout1.addWidget(self.channelsComboBox, 0, 1, 1, 1)
        layout1.addWidget(self.levelsLabel, 1, 0, 1, 1)
        layout1.addWidget(self.levelsComboBox, 1, 1, 1, 1)
        self.groupBox1.setLayout(layout1)        

        # create QWidget to gather the two previous boxes

        self.widget = QtGui.QWidget()
        layout_before_final.addWidget(self.groupBox)
        layout_before_final.addWidget(self.groupBox1)   

        self.widget.setLayout(layout_before_final)

        # GL widget
        self.widgetGL = MyWidget()

        # create a final widget to add the GL widget
        self.finalWidget = QtGui.QWidget()
        layout_final.addWidget(self.widgetGL)
        layout_final.addWidget(self.widget)   

        self.finalWidget.setLayout(layout_final)                


        self.setCentralWidget(self.finalWidget)

        self.setWindowTitle('PyPractis')
        self.resize(640, 480)      





def main():
    import sys

    app = QtGui.QApplication(sys.argv)


    w = MainWindow()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

我不确定为什么正在发生,但它似乎是由GL小部件优先水平折叠(将其空间放弃到其他元素)引起的。但是,您可以通过在添加元素时指定拉伸优先级来轻松覆盖此行为:

layout_final.addWidget(self.widgetGL, stretch=1)

这导致以下布局:

enter image description here