【问题标题】:Make qwidget in new window in PyQt4在 PyQt4 的新窗口中制作 qwidget
【发布时间】:2023-04-03 02:28:01
【问题描述】:

我正在尝试创建一个扩展 qwidget 的类,它会弹出一个新窗口,我一定遗漏了一些基本的东西,

class NewQuery(QtGui.QWidget):
 def __init__(self, parent):
  QtGui.QMainWindow.__init__(self,parent)
  self.setWindowTitle('Add New Query')
  grid = QtGui.QGridLayout()
  label = QtGui.QLabel('blah')
  grid.addWidget(label,0,0)
  self.setLayout(grid)
  self.resize(300,200)

当在主窗口的类中创建了一个新实例并调用了 show() 时,内容覆盖在主窗口上,我怎样才能让它在新窗口中显示?

【问题讨论】:

    标签: python pyqt4 qwidget


    【解决方案1】:

    听从@ChristopheD 给你的建议,试试这个

    from PyQt4 import QtGui
    
    class NewQuery(QtGui.QWidget):
        def __init__(self, parent=None):
            super(NewQuery, self).__init__(parent)
            self.setWindowTitle('Add New Query')
            grid = QtGui.QGridLayout()
            label = QtGui.QLabel('blah')
            grid.addWidget(label,0,0)
            self.setLayout(grid)
            self.resize(300,200)
    
    app = QtGui.QApplication([])
    mainform = NewQuery()
    mainform.show()
    newchildform = NewQuery()
    newchildform.show()
    app.exec_()
    

    【讨论】:

      【解决方案2】:

      您的超类初始化程序错误,您的意思可能是:

      class NewQuery(QtGui.QWidget):
          def __init__(self, parent):
              QtGui.QWidget.__init__(self, parent)
      

      (使用super的原因):

      class NewQuery(QtGui.QWidget):
          def __init__(self, parent):
              super(NewQuery, self).__init__(parent)
      

      但也许您希望从 QtGui.QDialog 继承(这可能是合适的 - 很难用当前上下文来判断)。

      另请注意,您的代码示例中的缩进是错误的(单个空格可以,但 4 个空格或单个制表符被认为更好)。

      【讨论】:

      • 是的,我需要 QDialog,谢谢。单个空格一定是复制代码的问题,我在代码中有标签:)
      猜你喜欢
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-12
      相关资源
      最近更新 更多