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

QHBoxLayout(QWidget): argument 1 has unexpected type 'QVBoxLayout'

我正在为 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()

您做错的是,您为 QHLayout 提供了另一个 Layout 对象,而它只接受 QWidget。

Traceback (most recent call last):
File "C:/Whosebug/QtVlayout.py", line 37, in <module>
    myapp = Application()
  File "C:/Whosebug/QtVlayout.py", line 14, in __init__
    self.createwidgets()
  File "C:/Whosebug/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)