LoginSignup
0

More than 3 years have passed since last update.

QSpinBox利用で整数専用のLineEdit風

Posted at

事務作業系のアプリなら必要性あるのでは。
WA_InputMethodEnabled,false は無いとwindowsで難ありなので。
keyPressEventは、del back キーで消去されたように見えて消去されていないからです。

from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtWidgets import QSpinBox

class myIntegerBox(QSpinBox):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.setProperty("showGroupSeparator", True)
        self.setAttribute(QtCore.Qt.WA_InputMethodEnabled,False)
        self.setButtonSymbols(QtWidgets.QAbstractSpinBox.NoButtons)

    def keyPressEvent(self,event):
        super().keyPressEvent(event)

        # del back space で0 セット
        if event.key() == QtCore.Qt.Key_Delete:
            self.setValue(0)
        if event.key() == QtCore.Qt.Key_Backspace:
            self.setValue(0)
        if event.key() == QtCore.Qt.Key_Space:
            self.setValue(0)

カーソルの位置などに依然難ありです。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0