简体   繁体   English

在PyQt4的新窗口中制作qwidget

[英]Make qwidget in new window in PyQt4

I'm trying to make a class that extends qwidget, that pops up a new window, I must be missing something fundamental, 我正在尝试制作一个扩展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)

when a new instance of this is made in main window's class, and show() called, the content is overlaid on the main window, how can I make it display in a new window? 当在主窗口的类中创建一个新的实例并调用show()时,内容将覆盖在主窗口上,如何使其显示在新窗口中?

follow the advice that @ChristopheD gave you and try this instead 遵循@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_()

Your superclass initialiser is wrong, you probably meant: 您的超类初始化程序是错误的,您可能意味着:

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

(a reason to use super ): (使用super的原因):

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

But maybe you want inherit from QtGui.QDialog instead (that could be appropriate - hard to tell with the current context). 但是也许您想从QtGui.QDialog继承(这可能是适当的-使用当前上下文很难分辨)。

Also note that the indentation in your code example is wrong (a single space will work but 4 spaces or a single tab are considered nicer). 还要注意,代码示例中的缩进是错误的(可以使用单个空格,但认为4个空格或单个制表符更好)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM