YOGYUI
PyQt5 - QDoubleSpinbox 더블클릭 시 텍스트 전체선택 본문
반응형
QDoubleSpinbox는 실수형 값을 위한 스핀박스인데, 더블클릭 시 다음과 같이 소수점을 기준으로 좌측 혹은 우측 영역만 선택된다
> 전체 값을 선택하고 싶을 때 불편한 경우가 간혹 발생한다
더블클릭 시 전체 영역을 선택하게 하기 위해서는 스핀박스 내부의 QLineEdit 객체를 커스터마이즈해줘야 한다
QLineEdit의 mouseDoubleClickEvent 시그널을 오버라이드해서 마우스 좌클릭일 경우 전체 텍스트를 선택하게 하는 selectAll() 메서드를 호출하면 된다
[예제 코드]
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class CustomLineEdit(QLineEdit):
def __init__(self, parent=None):
super().__init__(parent=parent)
def mouseDoubleClickEvent(self, a0) -> None:
if a0.button() == Qt.LeftButton:
self.selectAll()
a0.accept()
class CustomDoubleSpinBox(QDoubleSpinBox):
def __init__(self, parent=None):
super().__init__(parent=parent)
customLineEdit = CustomLineEdit(self)
self.setLineEdit(customLineEdit)
if __name__ == '__main__':
import sys
app = QCoreApplication.instance()
if app is None:
app = QApplication(sys.argv)
spinbox = CustomDoubleSpinBox()
spinbox.setValue(12.34)
spinbox.show()
app.exec_()
[실행 결과]
좌버튼 더블클릭 시 원하는대로 전체 텍스트가 선택된다
반응형
'Software > Python' 카테고리의 다른 글
PyQt5 - QTextEdit Tab Width 설정하기 (0) | 2021.07.15 |
---|---|
Python - Decorator in Class (0) | 2021.07.05 |
PyQt5 - QTableWidget Column Header Label 가져오기 (0) | 2021.06.21 |
Python::구조적 패턴 매칭 - 파이썬에서 switch/case문을?! (0) | 2021.03.21 |
Python으로 순서도 그리기 (schemdraw) (0) | 2021.01.27 |
Comments