简体   繁体   中英

Creating multiple primary windows

I am trying to write an application which opens multiple top level (primary) windows.

Since a widget without parent is a primary window ( http://qt-project.org/doc/qt-4.8/application-windows.html ), I made a sample program which spawns a new window every time you press a button.

I can obtain the desired result in C++:

Window::Window(QWidget *parent):
    QWidget(parent) {
    QPushButton *btn = new QPushButton("Another one!", this);
    connect(btn, SIGNAL(clicked()), this, SLOT(addOne()));
}

void Window::addOne() {
    QWidget *nw = new QWidget();
    nw->show();
}

And a new empty window is created every time I press the button, and the program correctly terminates when the last window is closed.

I tried the same in python3, using PyQt4, but no windows will show up:

import sys
from PyQt4 import QtCore, QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(300, 300, 250, 150)
        b = QtGui.QPushButton('Another one!', self)
        b.clicked.connect(self.new_window)
        self.show()

    def new_window(self):
        print('Opening new window...')
        w = QtGui.QWidget()
        w.show()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())

The message is printed correctly, so it does not seem to be a calling problem... No matter if I use python3 or 2, the result is the same.

What am I missing?

这是因为垃圾收集

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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