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

时间:2015-02-12 01:04:53

标签: python user-interface layout pyqt

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

2 个答案:

答案 0 :(得分:3)

你做错了的是,你提供的QHLayout还有另一个Layout对象,而它只接受一个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()

这应该可以解决问题。

答案 1 :(得分:0)

QLayout构造的原型类型是

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

所以,你需要像这样构建:

self.hLayout = QtGui.QHBoxLayout(self)