Qt for Python 5.12初體驗


Qt for Python 5.12初體驗

2018年12月18日,Qt在其博客上宣布Qt for Python 5.12正式發布,按照其幫助文檔的說明,嘗試運行了第一個小例子。
首先需要安裝Python 3.5+ or 2.7和for Qt 5.12,安裝完成之后邊可以輸入代碼編譯運行了。
復制自Qt示例的源代碼如下:

1 import sys
2 from PySide2.QtWidgets import QApplication, QLabel
3 
4 app = QApplication(sys.argv)
5 label = QLabel("Hello World!")
6 label.show()
7 app.exec_()

編譯運行,提示錯誤如下:
qt.qpa.plugin: Could not find the Qt platform plugin "windows" in ""
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
找不到插件的路徑,這里是解決方案。修改之后的代碼如下:

 1 import os
 2 import sys
 3 import PySide2
 4 from PySide2.QtWidgets import QApplication, QLabel
 5 
 6 dirname = os.path.dirname(PySide2.__file__)
 7 plugin_path = os.path.join(dirname, 'plugins', 'platforms')
 8 os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
 9 
10 app = QApplication(sys.argv)
11 label = QLabel("Hello World")
12 label.show()
13 sys.exit(app.exec_())

成功運行,並彈出對話框。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM