2

我有一个表,其中包含一些使用非常大的数字作为主键的记录。我有类似于以下的代码,用于QCompleter自动完成此表中的查找。它可以工作,但显示的补全是使用科学记数法(1234567 => 1.23e6)格式化的。我想按原样显示我的完成。在我看来,我要么需要将查询的响应转换为字符串(无法弄清楚如何做到这一点),要么在 上设置一个属性QLineEdit以禁用科学记数法格式(也无法弄清楚) . 有任何想法吗?

class MyDialog(BaseObject, QtGui.QDialog):
     def __init__(self, ... db=None):
         super(MyDialog, self).__init__(parent, logger)
        self.qsql_db = db
        self.init_ui()

    def mk_model(self, completer, pFilterModel, table_name, filter_=None):
        model = QtSql.QSqlTableModel()
        model.setTable(table_name)
        if filter_:
            model.setFilter(filter_)
        model.select()
        pFilterModel.setSourceModel(model)
        completer.setModel(pFilterModel)
        return model

    def setModelColumn(self, completer, pFilterModel, column):
        completer.setCompletionColumn(column)
        pFilterModel.setFilterKeyColumn(column)

    def mk_receipt_id_grid(self):
        font = self.mk_font()
        label_receipt_id = QtGui.QLabel(self)
        label_receipt_id.setText("Order ID")
        label_receipt_id.setFont(font)
        self.text_edit_receipt_id = QtGui.QLineEdit()
        self.text_edit_receipt_id.setFont(font)
        label_receipt_id.setBuddy(self.text_edit_receipt_id)
        self.formGridLayout.addWidget(label_receipt_id, 0, 0)
        self.formGridLayout.addWidget(self.text_edit_receipt_id, 0, 1)
        self.connect(self.text_edit_receipt_id,
                 QtCore.SIGNAL("editingFinished()"),
                 self.get_order_details)

        completer = QtGui.QCompleter(self)
        completer.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion)
        pFilterModel = QtGui.QSortFilterProxyModel(self)
        pFilterModel.setFilterCaseSensitivity(Qt.CaseInsensitive)
        completer.setPopup(completer.popup())
        self.text_edit_receipt_id.setCompleter(completer)
        model = self.mk_model(completer, pFilterModel, "orders", "created_at > date_trunc('day', now())")
        self.setModelColumn(completer, pFilterModel, model.fieldIndex("receipt_id"))
        self.text_edit_receipt_id.textEdited.connect(pFilterModel.setFilterFixedString)

问题截图:问题截图:

4

1 回答 1

2

一种方法是在完成者的视图上设置一个项目委托。QStyledItemDelegate类有一个可被覆盖的dispalyText方法,这使得实现这一点非常容易。

这是一个简单的演示:

import sys
from PySide import QtGui, QtCore

class ItemDelegate(QtGui.QStyledItemDelegate):
    def displayText(self, data, locale):
        if isinstance(data, (int, float)):
            data = '%d' % data
        return super(ItemDelegate, self).displayText(data, locale)

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        edit = QtGui.QLineEdit()
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(edit)
        completer = QtGui.QCompleter(self)
        model = QtGui.QStandardItemModel(self)
        for value in (
            17596767040000.0, 47993723466378568.0,
            1219073478568475.0, 43726487587345.0,
            29928757235623.0, 2245634345639486934.0,
            ):
            item = QtGui.QStandardItem()
            item.setData(value, QtCore.Qt.EditRole)
            model.appendRow([item])
        completer.setModel(model)
        completer.popup().setItemDelegate(ItemDelegate(self))
        edit.setCompleter(completer)

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(600, 100, 300, 50)
    window.show()
    sys.exit(app.exec_())
于 2018-01-30T22:22:37.683 回答