Search code examples
pythonpyqt4qwidget

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,

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?


Solution

  • 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):

    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).

    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).