【问题标题】:Creating multiple primary windows创建多个主窗口
【发布时间】:2013-09-19 21:36:52
【问题描述】:

我正在尝试编写一个打开多个顶级(主)窗口的应用程序。

由于没有父窗口的小部件是主窗口 (http://qt-project.org/doc/qt-4.8/application-windows.html),我制作了一个示例程序,每次按下按钮时都会生成一个新窗口。

我可以在 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();
}

每次按下按钮都会创建一个新的空窗口,当最后一个窗口关闭时程序正确终止。

我在 python3 中尝试了同样的方法,使用 PyQt4,但没有窗口会出现:

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_())

消息打印正确,所以好像不是调用问题……不管我用python3还是2,结果都是一样的。

我错过了什么?

【问题讨论】:

  • 可能是因为garbage collection
  • 天哪!我没想到!当然是......请添加一个答案,以便我接受它:)

标签: c++ python linux qt pyqt4


【解决方案1】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多