qtpyqtpyqt4pysideqcompleter

How to disable scientific notation in QCompleter?


I have a table with some records that are keyed using a very large number as the primary key. I have code similar to the following which uses QCompleter to autocomplete lookups in this table. It works, but the displayed completions are formatted using the scientific notation (1234567 => 1.23e6). I'd like to have my completions displayed as-is. In my opinion, I'd either need to cast the response from the query as a string (can't figure out how to do this) or set a property on the QLineEdit to disable scientific notation formatting (can't figure this out either). Any ideas?

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: Screenshot of the issue:


Solution

  • One way to do this would be to set an item-delegate on the completer's view. The QStyledItemDelegate class has a displayText method that can be overridden, which makes implementing this quite easy.

    Here is a simple demo:

    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_())