遇见的坑

qt 5.11 与 qt 5.12 中Qquick的差异还是蛮大的,由开发环境:Pyqt5.11 + Qt5.12 部署到 Pyqt5.11 + Qt5.11时遇到以下问题:

1.当一个Item引用另一个Item,当层次比较深入时,有可能引用不到此Item对象。这时可以用alias别名尝试把此对象作为顶层类的属性。另还有一个可能是因为Item文件的命名冲突,比如我把Item文件命名为ContentLeft.qml就会出现一些莫名奇妙的问题,后来把名称改为ContentLeftF.qml就没有。由“ContentLeft"改成”ContentLeftF.qml"问题解决。

2.有些str类型的属性可以直接用于QML,但有些会提示QString不对

    #barcode
@pyqtProperty(str,notify=barcodeChanged)
def barcode(self):
return self._barcode @barcode.setter
def barcode(self, value):
self._barcode = value
self.barcodeChanged.emit(value)

把以上写法改为下面写法即没问题:

projectName = pyqtProperty(str, fget=getProjectValue, fset= setProjectValue, notify=projectNameChanged)

一些套路

1.把python类注册成qml类

qmlRegisterType(CameraOpencv,'MyCamera',1,0,'MyCustomOpenCVItem')

可以直接在qml用

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.1
import MyCamera 1.0 Item{
id:root
property alias myCustomOpenCVItem: myCustomOpenCVItem
Rectangle{
color:"black"
anchors.fill: parent
MyCustomOpenCVItem{
id:myCustomOpenCVItem
objectName:"camera_win"
anchors.fill:parent
}
MouseArea{
anchors.fill: parent
onClicked: {
content.contentMiddle.myCustomOpenCVItem.save()
}
}
}
}

2.把python对象传至qml

        self.imageModel = ModelImages()
self.rootContext().setContextProperty('ImagesModel', self.imageModel)

qml中直接调用

        ListView {
id : m_listView
width: parent.width
height: root.height
clip: true
model: ImagesModel //modelImages
delegate: m_delegate
spacing: 5
focus: true
verticalLayoutDirection: ListView.BottomToTop
add: Transition {
NumberAnimation { properties: "y";duration: 400 }
}
remove: Transition {
NumberAnimation { properties: "y";duration: 400 }
}
}

3.设置可在qml中用的属性

方法一:

self._goodsName = '' #当前商品名称
goodsNameChanged = pyqtSignal(str)
@pyqtProperty(str,notify=goodsNameChanged)
def goodsName(self):
return self._goodsName @goodsName.setter
def goodsName(self, value):
self._goodsName = value
self.goodsNameChanged.emit(value)

方法二:

projectName = pyqtProperty(str, fget=getProjectValue, fset= setProjectValue, notify=projectNameChanged)

4.qml动态设置component

                function getLoader (){

                    switch (uploadStatus)
{
case "imgUploadFail":
return uploadFail
case "imgUploadOK":
return uploadOK
case "imgUploading":
return uploading
case "imgUploadReady":
return null
default:
return null
} } Loader{
id:currLoad
anchors.fill:parent
sourceComponent:getLoader()
}

5.使用虚拟键盘

需要先安装VirtualKeyboard插件

先声明:

os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"

qml中:

import QtQuick.VirtualKeyboard 2.2
//键盘
InputPanel{
id: vkb
z:3
visible: false
anchors.bottom: parent.bottom
width: parent.width * 0.85
height: parent.height * 0.3
x:parent.width * 0.5 - width * 0.5
//这种集成方式下点击隐藏键盘的按钮是没有效果的,
//只会改变active,因此我们自己处理一下
onActiveChanged: {
if(!active) { visible = false; }
}
}

最新文章

  1. keil l251 command summary --Lib
  2. ASP.NET 4的Demo实践:URL路由改进支持
  3. phpcms前端页面上传文件
  4. Test execution order
  5. 安装与使用smarty
  6. Ubuntu小私房(3)--Uubutnu启动美化大变身
  7. 使用WCF扩展记录服务调用时间
  8. 【广告】win10 uwp 水印图床 含代码
  9. 数据对象转json与md5加密注意事项
  10. 筛选出和该元素相交的元素之BoundingBoxIntersectsFilter
  11. 敏捷开发相关编辑思想(SOA、DDD、REST、CQRS)
  12. 讲一下Asp.net core MVC2.1 里面的 ApiControllerAttribute
  13. MySQL单向加密函数
  14. 132.leecode-Palindrome Partitioning II
  15. SSD磁盘测试不达标排查
  16. LVS负载均衡-基础知识梳理
  17. UVA11077 Find the Permutations
  18. 用Python开发Zeroc Ice应用
  19. T24银行核心业务系统
  20. windows下对python的pip更新到最新版本

热门文章

  1. Linux系统扩容根目录磁盘空间的操作方法
  2. git常用命令介绍
  3. mybatis if test 判断字符串的坑
  4. linux服务器显卡崩了怎么处理
  5. (七) UVC框架分析
  6. 1.7分布式工具配置及安装(仅供学习Xshell,VMware)
  7. 微信小程序 开发文档
  8. nginx配置vue项目部署访问无问题,刷新出现404问题
  9. 同一个tomcat部署多个项目导致启动失败
  10. Keras实现LSTM