YOGYUI

PyQt5 - QDoubleSpinbox 더블클릭 시 텍스트 전체선택 본문

Software/Python

PyQt5 - QDoubleSpinbox 더블클릭 시 텍스트 전체선택

요겨 2021. 6. 21. 15:13
반응형

 

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

[실행 결과]

좌버튼 더블클릭 시 원하는대로 전체 텍스트가 선택된다

반응형
Comments