QHBoxLayout(QWidget):参数1具有意外类型“QVBoxLayout”

2024-05-15 00:09:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为一个GUI创建一个布局,这个GUI应该有一个QVBoxLayout用于主布局,QHBoxLayout用于子布局,但是由于某些原因,它给了我这个错误。

代码如下:

class Application(QtGui.QMainWindow):

        err1 = QtCore.pyqtSignal(int)
        reset = QtCore.pyqtSignal()

        def __init__(self, parent=None):
          super(Application, self).__init__()
          self.setGeometry(300, 300, 600, 600)
          self.setWindowTitle('IPv6 traffic generator')
          PlotWidget(self)
          self.createwidgets()

        def createwidgets(self):


          self.mainWidget = QtGui.QWidget(self) 
          self.setCentralWidget(self.mainWidget)

          self.mainLayout = QtGui.QVBoxLayout(self.mainWidget)
          self.hLayout = QtGui.QHBoxLayout(self.mainLayout)

             ---- creating widgets ----

          self.hLayout.addWidget(self.label2)
          self.hLayout.addWidget(self.menubutton1)
          self.hLayout.addWidget(self.label3)
          self.hLayout.addWidget(self.button2)
          self.hLayout.addWidget(self.button3)
          self.mainLayout.setLayout(self.hLayout)
          self.mainLayout.show()

Tags: selfapplicationinitdefgui布局qtguiqtcore
2条回答

你做错了的是,你用另一个布局对象提供QHLayout,而它只接受一个QWidget。

Traceback (most recent call last):
File "C:/stackoverflow/QtVlayout.py", line 37, in <module>
    myapp = Application()
  File "C:/stackoverflow/QtVlayout.py", line 14, in __init__
    self.createwidgets()
  File "C:/stackoverflow/QtVlayout.py", line 23, in createwidgets
    self.hLayout = QtGui.QHBoxLayout(self.mainLayout)
TypeError: arguments did not match any overloaded call:
    QHBoxLayout(): too many arguments
    QHBoxLayout(QWidget): argument 1 has unexpected type 'QVBoxLayout'

为了达到你的目的:

self.mainLayout = QtGui.QVBoxLayout(self.mainWidget)
self.hLayout = QtGui.QHBoxLayout()
self.mainLayout.addLayout(self.hLayout)

并移除

self.mainLayout.show()

这应该能解决问题。

构造QLayout的原型类型是

def __init__(self, QWidget=None): # real signature unknown; restored from __doc__ with multiple overloads
    pass

所以,你需要这样构造:

self.hLayout = QtGui.QHBoxLayout(self)

相关问题 更多 >

    热门问题