从PyQt3迁移到PyQt4

0 投票
1 回答
890 浏览
提问于 2025-04-17 17:03

我刚开始学习Python,想知道怎么把一个为PyQt3写的代码改成能在PyQt4上运行。比如,下面的代码在PyQt3上应该能正常工作,那我需要改哪些地方才能让它在PyQt4上也能运行呢?

谢谢。

import sys
from qt import *

class dlgLabel(QDialog):

def __init__(self,parent = None,name = None,modal = 0,fl = 0):
    QDialog.__init__(self,parent,name,modal,fl)
    self.setCaption("label dialog")
    if name == None:
        self.setName("dlgLabel")

    self.layout=QHBoxLayout(self)
    self.layout.setSpacing(6)
    self.layout.setMargin(11)

    self.label=QLabel("&Enter some text", self)
    self.edit=QLineEdit(self)
    self.label.setBuddy(self.edit)

    self.layout.addWidget(self.label)
    self.layout.addWidget(self.edit)

    self.edit.setFocus()

if __name__ == '__main__':
app = QApplication(sys.argv)
QObject.connect(app, SIGNAL('lastWindowClosed()'),
                app, SLOT('quit()'))
win = dlgLabel()
app.setMainWidget(win)
win.show()
app.exec_loop()

1 个回答

1

主要的区别会来自于Qt 3和Qt 4在接口上的不同,这些差异在http://doc.qt.io/qt-4.8/porting4-overview.html以及其他一些Qt移植指南中都有比较详细的说明。

查看一下https://github.com/develersrl/pyqt3support也可能会对你有帮助。

撰写回答