如何在QCompleter中禁用科学记数法?

时间:2018-01-30 19:30:05

标签: qt pyqt pyqt4 pyside qcompleter

我有一个表,其中包含一些记录,这些记录使用非常大的数字作为主键进行键控。我有类似于以下代码,使用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)

问题的屏幕截图:Screenshot of the issue:

1 个答案:

答案 0 :(得分:1)

执行此操作的一种方法是在完成者视图上设置项目委托。 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_())