如何将 PyQt 中的动态菜单条目添加到 QML Drawer

我目前在将动态菜单条目从 PyQt 添加到 QML Drawer 内的 QAbstractListModel 时遇到了一些麻烦。


在以下链接中可以找到我正在使用的相应 QML 文件:main.qml和NavigationDrawer.qml


drawer.py:


class QDrawer(QQuickView):


    close_button_clicked = QtCore.pyqtSignal()

    error_triggered = QtCore.pyqtSignal()

    on_transparent_frame_clicked = QtCore.pyqtSignal()



    def __init__(self, parent=None, layout=None, drawerMenuEntriesList=[], callback=None):

        super().__init__()

        self.parent =  parent


        qmlRegisterType(QPythonBinding, "MyApplication", 1, 0, "QPythonBinding")


        # Formatting of the QDrawer

        self._format(QtGui.QColor(QtCore.Qt.transparent))

        self._source_qml_file("main.qml")


        self.setResizeMode(self.SizeRootObjectToView)


        self.engine().rootContext().setContextProperty("QDrawer", self)


        # Prevent from continuing if the QML file was not successfully sourced

        if not self.engine().rootContext():

            logger.error("No object could be loaded from sourced QML file and create a context out of it")

            self.error_detected()


        # Connections

        self.statusChanged.connect(self.on_statusChanged)


        self.transparent_frame = QWidget()

        eventFilter_transparent_frame = _Filter(self.transparent_frame)

        self.transparent_frame.installEventFilter(eventFilter_transparent_frame)

        eventFilter_transparent_frame.released.connect(self.transparent_frame_clicked)


        self._add_drawer_to_container_on_layout(layout)


        # the callback function is propagated up to the ui_controller 

        self.populate(drawerMenuEntriesList, callback)


        logger.debug(f"Drawer created")


    ...


    def populate(self, drawerMenuEntriesList, callback):

        menu = self._get_qml_component_by_objectname(QtCore.QAbstractListModel, "drawer_list")


        qpybinding = QPythonBinding()


生成的抽屉如下所示:

http://img2.mukewang.com/62dfa37400011da204870270.jpg

这个想法是用populate()函数中的“ drawerMenuEntriesList ”确定的任何自定义字符串条目替换抽屉菜单中的条目“Fragment X” 。



慕神8447489
浏览 173回答 1
1回答

qq_遁去的一_1

如果分析 QML 代码,只需要从 python 中创建一个模型(例如 QStandardItemModel),用 setContextProperty 将其导出到 QML 并在 ListView 中设置。import sysfrom PyQt5 import QtCore, QtGui, QtQuickclass QDrawer(QtQuick.QQuickView):&nbsp; &nbsp; def __init__(self, parent=None):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__(parent)&nbsp; &nbsp; &nbsp; &nbsp; self.setResizeMode(self.SizeRootObjectToView)&nbsp; &nbsp; &nbsp; &nbsp; self.entry_model = QtGui.QStandardItemModel()&nbsp; &nbsp; &nbsp; &nbsp; self.rootContext().setContextProperty("entry_model", self.entry_model)&nbsp; &nbsp; &nbsp; &nbsp; self.setSource(QtCore.QUrl.fromLocalFile("main.qml"))&nbsp; &nbsp; def addEntry(self, entry):&nbsp; &nbsp; &nbsp; &nbsp; it = QtGui.QStandardItem(entry)&nbsp; &nbsp; &nbsp; &nbsp; self.entry_model.appendRow(it)def main():&nbsp; &nbsp; app = QtGui.QGuiApplication(sys.argv)&nbsp; &nbsp; w = QDrawer()&nbsp; &nbsp; for i in range(10):&nbsp; &nbsp; &nbsp; &nbsp; w.addEntry("entry-{}".format(i))&nbsp; &nbsp; w.show()&nbsp; &nbsp; sys.exit(app.exec_())if __name__ == "__main__":&nbsp; &nbsp; main()main.qmlimport QtQuick 2.12import QtQuick.Controls 2.12import QtQuick.Layouts 1.12import QtQuick.Window 2.12Rectangle {&nbsp; &nbsp; id: window&nbsp; &nbsp; width: 360&nbsp; &nbsp; height: 520&nbsp; &nbsp; color: "#00000000"&nbsp; &nbsp; readonly property int dpi: Screen.pixelDensity * 25.4&nbsp; &nbsp; function dp(x){ return (dpi < 120) ? x : x*(dpi/160); }&nbsp; &nbsp; // Application Bar&nbsp; &nbsp; Rectangle {&nbsp; &nbsp; &nbsp; &nbsp; id: menuRect&nbsp; &nbsp; &nbsp; &nbsp; anchors.top: parent.top&nbsp; &nbsp; &nbsp; &nbsp; anchors.left: parent.left&nbsp; &nbsp; &nbsp; &nbsp; anchors.right: parent.right&nbsp; &nbsp; &nbsp; &nbsp; height: dp(48)&nbsp; &nbsp; &nbsp; &nbsp; color: "#4cd964"&nbsp; &nbsp; &nbsp; &nbsp; // Icon-Hamburger&nbsp; &nbsp; &nbsp; &nbsp; Rectangle {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.top: parent.top&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.bottom: parent.bottom&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.left: parent.left&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: dp(48)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: "#4cd964"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rectangle {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.top: parent.top&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.topMargin: dp(16)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.left: parent.left&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.leftMargin: dp(14)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: dp(20)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height: dp(2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rectangle {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.top: parent.top&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.topMargin: dp(23)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.left: parent.left&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.leftMargin: dp(14)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: dp(20)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height: dp(2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rectangle {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.top: parent.top&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.topMargin: dp(30)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.left: parent.left&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.leftMargin: dp(14)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: dp(20)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height: dp(2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MouseArea {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.fill: parent&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onClicked: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nav.toggle()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; Label{&nbsp; &nbsp; &nbsp; &nbsp; id: label&nbsp; &nbsp; &nbsp; &nbsp; anchors.top: menuRect.bottom&nbsp; &nbsp; &nbsp; &nbsp; anchors.left: parent.left&nbsp; &nbsp; &nbsp; &nbsp; anchors.right: parent.right&nbsp; &nbsp; &nbsp; &nbsp; anchors.bottom: parent.bottom&nbsp; &nbsp; &nbsp; &nbsp; horizontalAlignment: Text.AlignHCenter&nbsp; &nbsp; &nbsp; &nbsp; verticalAlignment: Text.AlignVCenter&nbsp; &nbsp; }&nbsp; &nbsp; NavigationDrawer {&nbsp; &nbsp; &nbsp; &nbsp; id: nav&nbsp; &nbsp; &nbsp; &nbsp; width: window.width * .75&nbsp; &nbsp; &nbsp; &nbsp; Rectangle {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.fill: parent&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: "white"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ListView {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.fill: parent&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delegate: Item {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height: dp(48)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.left: parent.left&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.right: parent.right&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rectangle {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.fill: parent&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.margins: dp(5)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: "whitesmoke"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: model.display&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.fill: parent&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font.pixelSize: dp(20)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; renderType: Text.NativeRendering&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; horizontalAlignment: Text.AlignHCenter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; verticalAlignment: Text.AlignVCenter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MouseArea {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.fill: parent&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onClicked: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label.text = index + " : " + model.display&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nav.hide()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model: entry_model&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python