Python3 + QML2,python作为qml组件

时间:2014-11-12 17:31:07

标签: python qt qml pyqt5

我正在尝试创建一个在qml-file中使用的python组件。当我尝试qmlscene使用它时会抛出错误:

$ qmlscene mainwindow.qml
ImportError: PyCapsule_Import could not import module "sip"
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 20, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ImportError: /usr/lib/python3/dist-packages/apt_pkg.cpython-34m-x86_64-linux-gnu.so: undefined symbol: PyExc_OSError

Original exception was:
ImportError: PyCapsule_Import could not import module "sip"

MainWindow.qml:

import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.1
import QtQuick.Window 2.1

import "PyComponents" 0.1 as QLC


ApplicationWindow {
    id: mainWindow
}

Python组件 - openserver.py

from PyQt5.QtCore import pyqtProperty, QObject, pyqtSlot


class OpenServer(QObject):

def __init__(self, parent=None):
    QObject.__init__(self)

    self._server = ''

# Define the getter of the 'name' property.  The C++ type of the
# property is QString which Python will convert to and from a string.
@pyqtProperty('QString')
def server(self):
    return self._server

# Define the setter of the 'name' property.
@server.setter
def server(self, server):
    self._server = server

@pyqtSlot(str)
def open_server(self, server_string):
    print('OpenServer: %s' % server_string)

qmldir 文件:

module .home.user.proj.ui.PyComponents
plugin pyqt5qmlplugin /usr/lib/x86_64-linux-gnu/qt5/plugins/PyQt5/
OpenServer 0.1 openserver.py

这也是qmldir文件的一个问题:当我尝试将模块指定为qml文件的相对目录时,其中包含如下:

module PyComponents

它也抛出(它在上面附加错误)错误:

file:///home/user/proj/ui/mainwindow.qml:7 plugin cannot be loaded for module ".home.user.proj.ui.PyComponents": Module namespace 'PyComponents' does not match import URI '.home.user.proj.ui.PyComponents'

系统:ubuntu 14.10 x64,软件包:python3.4python3-pyqt5*qtcreator。 包python3-sip已安装,可以从python3导入(由我自己测试)。

0 个答案:

没有答案