简体   繁体   中英

Custom function with PyQt4 and Sqlite3

How to define custom function within QtSql.QSqlDatabase?

Within pure python ( python - sqlite3 ), it seem easy but i don't undestand how to do it within PyQt.

My first question was about registering a function regexp to filter data in QSqlTableModel. I dunno if it's possible to register our own function like in regular python sqlite3, but anyway to solve my problem i use the Qt way using QSortFilterProxyModel.

In the end we have:

view -> proxy -> model


    tbl = self.tableViewPlaylist
    proxy = self.mediasModelProxy
    proxy.setSourceModel(self.mediasModel)
    proxy.setFilterCaseSensitivity(Qt.CaseInsensitive)
    proxy.setFilterKeyColumn(-1)
    tbl.setModel(self.mediasModelProxy)

    def update_filter(self):
        log.info('Updating Filter')
        tokens = str(self.lineEditFilter.text()).split()
        if len(tokens) <= 0:
            self.mediasModelProxy.setFilterRegExp(QRegExp(''))
            return
        nq = '|'.join(tokens)
        rx = QRegExp("(%s)" % nq, Qt.CaseInsensitive)
        self.mediasModelProxy.setFilterRegExp(rx)
        return

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM