[PyQt] Python3.4 + PyQt5 QML ListView - Cannot add entries

Sylvie iamsylvie at openmailbox.org
Mon Dec 21 21:06:08 GMT 2015


With the help of Avaris on #pyqt, I have figured it out. I find it hard
to explain what was changed, but I will put the complete code here, as
it may help someone. Thanks again, Avaris!

main.py:

#!/usr/bin/python3
import sys
import os
from os.path import expanduser
from subprocess import call

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.Qt import *
from PyQt5.QtQuick import *

class ViewModel():
    def __init__(self):
        self.getPasswords()
        self.filteredPasswordList = self.passwordList
        self.listViewModelPasswordList =
QStringListModel(self.filteredPasswordList)

    def bindListView(self, listView):
        self.listView = listView

    def getPasswords(self):
        self.passwordList = []

        passDir = expanduser("~") + "/.password-store/"
        for root, dirs, files in os.walk(passDir):
            for name in files:
                if name[0] == ".":
                    continue

                self.passwordList.append(os.path.join(root,
name)[len(passDir):-4])

    def search(self, text):
        self.filteredPasswordList = [];
        searchString = text.lower()
        for password in self.passwordList:
            if searchString in password.lower():
                self.filteredPasswordList.append(password)

        QQmlProperty.write(self.listView, "model",
QStringListModel(self.filteredPasswordList))

    def select(self):
        if len(self.filteredPasswordList) == 0: return

        exit(call(["pass", "-c", self.filteredPasswordList[0]]))
       
class Window(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.engine = QQmlApplicationEngine(self)

        self.vm = ViewModel()
        self.engine.rootContext().setContextProperty("listViewModel",
self.vm.listViewModelPasswordList)

       
self.engine.load(QUrl.fromLocalFile(os.path.dirname(os.path.realpath(__file__))
+ "/main.qml"))

        self.window = self.engine.rootObjects()[0]

        searchInput = self.window.findChild(QObject, "searchInput")
        resultList = self.window.findChild(QObject, "resultList")

        self.vm.bindListView(resultList)

        searchInput.textChanged.connect(lambda:
self.vm.search(QQmlProperty.read(searchInput, "text")))
        searchInput.accepted.connect(self.vm.select)

    def show(self):
        self.window.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())

main.qml:

import QtQuick 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.2
import QtQuick.Window 2.1

ApplicationWindow {
    title: 'PyPass'
    property int margin: 10
    width: 500

    x: (Screen.width - width) / 2

    flags: Qt.FramelessWindowHint | Qt.Window

    ColumnLayout {
        anchors.fill: parent
        anchors.margins: margin
        RowLayout {
            Layout.fillWidth: true
            TextField {
                objectName: "searchInput"

                font.pixelSize: 24
                focus: true

                Layout.fillWidth: true
            }
        }
        ScrollView {
            objectName: "scrollView"

            Layout.fillHeight: true
            Layout.fillWidth: true

            ListView {
                objectName: "resultList"

                model: listViewModel
                delegate: Text { text: display }

                Layout.fillHeight: true
                Layout.fillWidth: true
            }
        }
    }
}

On 21/12/15 19:44, Sylvie wrote:
> Hello Phil,
>
> Keeping a reference indeed prevents the segmentation fault, but
> unfortunately the ListView still does not get populated. Still a helpful
> pointer, though.
>
> - Sylvie
>
> On 21/12/15 19:39, Phil Thompson wrote:
>> You might want to try keeping a reference to the QStringListModel.
>>
>> Phil
>>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt



More information about the PyQt mailing list