简体   繁体   English

使用PyQt4和Sqlite3的自定义函数

[英]Custom function with PyQt4 and Sqlite3

How to define custom function within QtSql.QSqlDatabase? 如何在QtSql.QSqlDatabase中定义自定义函数?

Within pure python ( python - sqlite3 ), it seem easy but i don't undestand how to do it within PyQt. 在纯python( python-sqlite3 )中,这似乎很简单,但我不理解如何在PyQt中进行操作。

My first question was about registering a function regexp to filter data in QSqlTableModel. 我的第一个问题是关于注册函数regexp来过滤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. 我不知道是否可以像在常规python sqlite3中那样注册我们自己的函数,但是无论如何要解决我的问题,我使用QSortFilterProxyModel使用Qt方式。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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