久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <i id='0SaXr'><tr id='0SaXr'><dt id='0SaXr'><q id='0SaXr'><span id='0SaXr'><b id='0SaXr'><form id='0SaXr'><ins id='0SaXr'></ins><ul id='0SaXr'></ul><sub id='0SaXr'></sub></form><legend id='0SaXr'></legend><bdo id='0SaXr'><pre id='0SaXr'><center id='0SaXr'></center></pre></bdo></b><th id='0SaXr'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='0SaXr'><tfoot id='0SaXr'></tfoot><dl id='0SaXr'><fieldset id='0SaXr'></fieldset></dl></div>
    <legend id='0SaXr'><style id='0SaXr'><dir id='0SaXr'><q id='0SaXr'></q></dir></style></legend>

        <bdo id='0SaXr'></bdo><ul id='0SaXr'></ul>
    1. <small id='0SaXr'></small><noframes id='0SaXr'>

      1. <tfoot id='0SaXr'></tfoot>

        如何從我的 Python 文件中更新 Qml 對象的屬性?

        How Can I Update a Qml Object#39;s Property from my Python file?(如何從我的 Python 文件中更新 Qml 對象的屬性?)
        • <i id='IOIwp'><tr id='IOIwp'><dt id='IOIwp'><q id='IOIwp'><span id='IOIwp'><b id='IOIwp'><form id='IOIwp'><ins id='IOIwp'></ins><ul id='IOIwp'></ul><sub id='IOIwp'></sub></form><legend id='IOIwp'></legend><bdo id='IOIwp'><pre id='IOIwp'><center id='IOIwp'></center></pre></bdo></b><th id='IOIwp'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='IOIwp'><tfoot id='IOIwp'></tfoot><dl id='IOIwp'><fieldset id='IOIwp'></fieldset></dl></div>
        • <tfoot id='IOIwp'></tfoot>
          <legend id='IOIwp'><style id='IOIwp'><dir id='IOIwp'><q id='IOIwp'></q></dir></style></legend>
          • <bdo id='IOIwp'></bdo><ul id='IOIwp'></ul>

            <small id='IOIwp'></small><noframes id='IOIwp'>

                    <tbody id='IOIwp'></tbody>
                  本文介紹了如何從我的 Python 文件中更新 Qml 對象的屬性?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我想在 Qml 中顯示一個矩形,并且我想從我的 python 代碼中更改矩形的屬性(寬度、長度).其實python代碼中有一個socket連接,通過它從另一臺電腦接收width和length的值.簡單地說:另一個用戶應該能夠實時調整這個矩形.我知道如何在我的 python 文件中建立套接字連接并使用 PyQt5,我可以從 python 顯示 qml 文件.

                  I want to show a rectangle in Qml and I want to change the rectangle's properties(width, length) from my python code. In fact, there is a socket connection in the python code, through which the values of width and length are received from another computer. To put it simple: another user should be able to adjust this rectangle in real-time. I know how to make a socket connection in my python file and using PyQt5, I can show the qml file from python.

                  但是,我無法通過我的 python 代碼訪問矩形的參數.我該怎么做?

                  However, I am in trouble to access the rectangle's parameters through my python code. How can I do that?

                  這是我的 qml 文件的簡化示例:

                  This is a simplified sample of my qml file:

                  import QtQuick 2.11
                  import QtQuick.Window 2.2
                  import QtQuick.Controls 2.2
                  
                  ApplicationWindow {    
                      visible: true
                      width: Screen.width/2
                      height: Screen.height/2
                      Rectangle {
                          id: rectangle
                          x: 187
                          y: 92
                          width: 200
                          height: 200
                          color: "blue"
                      }
                  }
                  

                  這是我在 .py 文件中寫的內容:

                  And here is what I have written in my .py file:

                  from PyQt5.QtQml import QQmlApplicationEngine, QQmlProperty
                  from PyQt5.QtQuick import QQuickWindow, QQuickView
                  from PyQt5.QtCore import QObject, QUrl
                  from PyQt5.QtWidgets import QApplication
                  import sys
                  def run():
                      myApp = QApplication(sys.argv)
                      myEngine = QQmlApplicationEngine()
                  
                      myEngine.load('mainViewofHoomanApp.qml')
                  
                  
                      if not myEngine.rootObjects():
                          return -1
                      return myApp.exec_()
                  
                  if __name__ == "__main__":
                      sys.exit(run())
                  

                  推薦答案

                  在python/C++中有幾種方法可以修改QML元素的屬性,各有優缺點.

                  There are several methods to modify a property of a QML element from python/C++, and each has its advantages and disadvantages.

                  • 通過另一個對象通過findChildren獲取QML對象.
                  • 分別使用 setProperty()property() 或使用 QQmlProperty 修改或訪問屬性.
                  • Obtain the QML object through findChildren through another object.
                  • Modify or access the property with setProperty() or property(), respectively or with QQmlProperty.

                  ma??in.qml(qml 用于下一個 2 .py)

                  main.qml (the qml is for the next 2 .py)

                  import QtQuick 2.11
                  import QtQuick.Window 2.2
                  import QtQuick.Controls 2.2
                  
                  ApplicationWindow {    
                      visible: true
                      width: Screen.width/2
                      height: Screen.height/2
                      Rectangle {
                          id: rectangle
                          x: 187
                          y: 92
                          width: 200
                          height: 200
                          color: "blue"
                          objectName: "foo_object"
                      }
                  }
                  

                  1.1 setProperty(), property().

                  import os
                  import sys
                  from PyQt5 import QtCore, QtGui, QtQml
                  from functools import partial
                  
                  def testing(r):
                      import random
                      w = r.property("width")
                      h = r.property("height")
                      print("width: {}, height: {}".format(w, h))
                      r.setProperty("width", random.randint(100, 400))
                      r.setProperty("height", random.randint(100, 400))
                  
                  def run():
                      myApp = QtGui.QGuiApplication(sys.argv)
                      myEngine = QtQml.QQmlApplicationEngine()
                      directory = os.path.dirname(os.path.abspath(__file__))
                      myEngine.load(QtCore.QUrl.fromLocalFile(os.path.join(directory, 'main.qml')))
                      if not myEngine.rootObjects():
                          return -1
                      r = myEngine.rootObjects()[0].findChild(QtCore.QObject, "foo_object")
                      timer = QtCore.QTimer(interval=500)
                      timer.timeout.connect(partial(testing, r))
                      timer.start()
                      return myApp.exec_()
                  
                  if __name__ == "__main__":
                      sys.exit(run())
                  

                  1.2 QQml 屬性.

                  import os
                  import sys
                  from PyQt5 import QtCore, QtGui, QtQml
                  from functools import partial
                  
                  def testing(r):
                      import random
                      w_prop = QtQml.QQmlProperty(r, "width")
                      h_prop = QtQml.QQmlProperty(r, "height")
                      print("width: {}, height: {}".format(w_prop.read(), w_prop.read()))
                      w_prop.write(random.randint(100, 400))
                      h_prop.write(random.randint(100, 400))
                  
                  def run():
                      myApp = QtGui.QGuiApplication(sys.argv)
                      myEngine = QtQml.QQmlApplicationEngine()
                      directory = os.path.dirname(os.path.abspath(__file__))
                      myEngine.load(QtCore.QUrl.fromLocalFile(os.path.join(directory, 'main.qml')))
                  
                      if not myEngine.rootObjects():
                          return -1
                      r = myEngine.rootObjects()[0].findChild(QtCore.QObject, "foo_object")
                      timer = QtCore.QTimer(interval=500)
                      timer.timeout.connect(partial(testing, r))
                      timer.start()
                      return myApp.exec_()
                  
                  if __name__ == "__main__":
                      sys.exit(run())
                  

                  這種方法的一個缺點是如果對象與根對象的關系很復雜(有時其他 QML 中的對象很難用 findChild 訪問)訪問對象的部分變得復雜,有時甚至不可能,所以這種方法將失敗.另一個問題是,當使用 objectName 作為主要搜索數據時,Python 層對 QML 層的依賴性很高,因為如果在 QML 中修改了 objectName,則必須修改 python 中的邏輯.另一個缺點是,如果不管理 QML 對象的生命周期,它可能會在 Python 不知情的情況下被消除,因此它會訪問不正確的引用,從而導致應用程序意外終止.

                  A disadvantage of this method is that if the relation of the object with the rootobject is complex(Sometimes objects that are in other QMLs are hard to access with findChild) the part of accessing the object becomes complicated and sometimes impossible so this method will fail. Another problem is that when using the objectName as the main search data there is a high dependency of the Python layer to the QML layer since if the objectName is modified in QML the logic in python would have to be modified. Another disadvantage is that by not managing the life cycle of the QML object it could be eliminated without Python knowing so it would access an incorrect reference causing the application to terminate unexpectedly.

                  • 創建一個具有相同類型屬性的 QObject.
                  • 使用 setContextProperty 導出到 QML.
                  • 在 QObject 的屬性和 item 的屬性之間進行綁定.

                  ma??in.qml

                  import QtQuick 2.11
                  import QtQuick.Window 2.2
                  import QtQuick.Controls 2.2
                  
                  ApplicationWindow {    
                      visible: true
                      width: Screen.width/2
                      height: Screen.height/2
                      Rectangle {
                          id: rectangle
                          x: 187
                          y: 92
                          width: r_manager.width
                          height: r_manager.height
                          color: "blue"
                      }
                  }
                  

                  ma??in.py

                  import os
                  import sys
                  from PyQt5 import QtCore, QtGui, QtQml
                  from functools import partial
                  
                  class RectangleManager(QtCore.QObject):
                      widthChanged = QtCore.pyqtSignal(float)
                      heightChanged = QtCore.pyqtSignal(float)
                  
                      def __init__(self, parent=None):
                          super(RectangleManager, self).__init__(parent)
                          self._width = 100
                          self._height = 100
                  
                      @QtCore.pyqtProperty(float, notify=widthChanged)
                      def width(self):
                          return self._width
                  
                      @width.setter
                      def width(self, w):
                          if self._width != w:
                              self._width = w
                              self.widthChanged.emit(w)
                  
                      @QtCore.pyqtProperty(float, notify=heightChanged)
                      def height(self):
                          return self._height
                  
                      @height.setter
                      def height(self, h):
                          if self._height != h:
                              self._height = h
                              self.heightChanged.emit(h)
                  
                  def testing(r):
                      import random
                      print("width: {}, height: {}".format(r.width, r.height))
                      r.width = random.randint(100, 400)
                      r.height = random.randint(100, 400)
                  
                  def run():
                      myApp = QtGui.QGuiApplication(sys.argv)
                      myEngine = QtQml.QQmlApplicationEngine()
                      manager = RectangleManager()
                      myEngine.rootContext().setContextProperty("r_manager", manager)
                      directory = os.path.dirname(os.path.abspath(__file__))
                      myEngine.load(QtCore.QUrl.fromLocalFile(os.path.join(directory, 'main.qml')))
                  
                      if not myEngine.rootObjects():
                          return -1
                      timer = QtCore.QTimer(interval=500)
                      timer.timeout.connect(partial(testing, manager))
                      timer.start()
                      return myApp.exec_()
                  
                  if __name__ == "__main__":
                      sys.exit(run())
                  

                  缺點是您必須編寫更多代碼.優點是對象可以被所有 QML 訪問,因為它使用 setContextProperty,另一個優點是如果 QML 對象被刪除,它不會產生問題,因為只消除了綁定.最后,通過不使用 objectName,依賴項不存在.

                  The disadvantage is that you have to write some more code. The advantage is that the object is accessible by all the QML since it uses setContextProperty, another advantage is that if the QML object is deleted it does not generate problems since only the binding is eliminated. And finally, by not using the objectName, the dependency does not exist.

                  所以我更喜歡使用第二種方法,更多信息請閱讀 從 C++ 與 QML 交互.

                  So I prefer to use the second method, for more information read Interacting with QML from C++.

                  這篇關于如何從我的 Python 文件中更新 Qml 對象的屬性?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How to bind a function to an Action from Qt menubar?(如何將函數綁定到 Qt 菜單欄中的操作?)
                  PyQt progress jumps to 100% after it starts(PyQt 啟動后進度躍升至 100%)
                  How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何將 yaxis 刻度標簽設置在固定位置,以便當我向左或向右滾動時,yaxis 刻度標簽應該可見
                  `QImage` constructor has unknown keyword `data`(`QImage` 構造函數有未知關鍵字 `data`)
                  Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                  How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時顯示進度條?)

                      <tbody id='e7wfc'></tbody>

                        <bdo id='e7wfc'></bdo><ul id='e7wfc'></ul>
                        <i id='e7wfc'><tr id='e7wfc'><dt id='e7wfc'><q id='e7wfc'><span id='e7wfc'><b id='e7wfc'><form id='e7wfc'><ins id='e7wfc'></ins><ul id='e7wfc'></ul><sub id='e7wfc'></sub></form><legend id='e7wfc'></legend><bdo id='e7wfc'><pre id='e7wfc'><center id='e7wfc'></center></pre></bdo></b><th id='e7wfc'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='e7wfc'><tfoot id='e7wfc'></tfoot><dl id='e7wfc'><fieldset id='e7wfc'></fieldset></dl></div>
                          • <legend id='e7wfc'><style id='e7wfc'><dir id='e7wfc'><q id='e7wfc'></q></dir></style></legend>

                          • <tfoot id='e7wfc'></tfoot>

                            <small id='e7wfc'></small><noframes id='e7wfc'>

                          • 主站蜘蛛池模板: 欧美综合在线观看 | 国产精品久久777777 | 秋霞国产| 日韩欧美网 | 蜜桃视频在线观看免费视频网站www | 伊人成人免费视频 | 免费在线播放黄色 | 欧美日日 | 国产三级 | 中文字幕国产精品 | 精品一级电影 | 欧美精品在线播放 | 欧美日韩高清一区二区三区 | 91婷婷韩国欧美一区二区 | 美女黄视频网站 | 中文字幕视频一区二区 | 啪啪综合网 | 国产精品成人国产乱一区 | 九色综合网 | 国产一区二区三区精品久久久 | 国产毛片久久久久久久久春天 | 亚洲性视频 | 天天操天天射天天 | 色综合美女 | a天堂在线| 欧美视频在线免费 | 亚洲国产精品第一区二区 | 欧美a级成人淫片免费看 | 欧美高清视频 | 亚洲香蕉| 欧美一级欧美三级在线观看 | 岛国毛片在线观看 | 国产欧美精品一区二区色综合朱莉 | 观看av | 一区在线免费视频 | 人人干人人草 | 久久久久久一区 | 欧美精品第一页 | 在线观看亚洲 | 久久久久国产精品www | 亚洲欧美在线观看 |