QComboBox 위젯 > Pyside6 - GUI for Python

본문 바로가기
사이트 내 전체검색

회원로그인

올서베이넷
무료 온라인 설문 사이트
OVITII
웹 프레젠테이션 도구

Pyside6 - GUI for Python

QComboBox 위젯

페이지정보

글쓴이 관리자 조회 23,381 조회 날짜 19-12-13 16:28 / Update:22-12-05 02:15
댓글 0 댓글

SNS 공유

  • 트위터로 보내기
  • 페이스북으로 보내기
  • 구글플러스로 보내기

내용

QComboBox 위젯

 

QComboBox 위젯은 인터넷에서 사용하는 selectbox의 기능과 같다.

 

QComboBox에는 아이콘, 텍스트, 데이터가 인수로 들어간다.

 

그리고 순서는 index로 구분된다.

 

UI file

1269731307_1576270585.1881.png

 

 

 

1. Item 입력

 

QComboBox에 Item을 입력하는 메서드는 addItem()이다. ( icon과 userData는 생략 가능 )

 

addItem(icon, text, userData=None)

 

Item을 입력하는 방법은 다음과 같다.

 

cbox = QComboBox()

cbox.addItem("Sunny", "data s")

cbox.addItem("Cloudy", "data c")

cbox.addItem("Rainy", "data r")

cbox.addItem("Foggy", "data f")

 

addItem은 마지막에 하나씩 추가하는 메서드이다.

만약 기존에 있는 Item 사이에 새로운 Item을 넣고 싶다면 insertItem(index, icon, textuserData=None)를 사용한다.( icon과 userData는 생략 가능 )

 

 

 

2. 현재 Item 받기

 

QComboBox에서 선택된 값은 index, text, data가 있다. 이 세개의 값을 받아오는 메서드는 다음과 같다.

 

currentData()

currentIndex()

currentText()

 

사용법

    index = UI_set.comboBox.currentIndex()
    text = UI_set.comboBox.currentText()
    data = UI_set.comboBox.currentData()

 

 

 

3. Item값이 바뀌었을 때의 이벤트(시그널)

 

QComboBox의 값을 선택했을 때, 이벤트를 처리하기 위해서는 currentIndexChanged 시그널을 이용한다.

 

UI_set.comboBox.currentIndexChanged.connect(checkcombobox)

 

 

 

4. QComboBox의 현재 선택된 Item을 프로그램 코드로 변경하기

 

1) index값으로 변경

UI_set.comboBox.setCurrentIndex(0)

 

2) text 값으로 변경

UI_set.comboBox.setCurrentText('Sunny')

 

 

 

5. Item 삭제

 

QComboBox의 Item을 삭제하기 위해서는 clear(), removeItem(index) 메서드를 사용한다.

 

전체삭제

UI_set.comboBox.clear()

 

 

하나씩 삭제

UI_set.comboBox.removeItem(0)

 

Item의 갯수보다 많은 index를 지정할 때는 삭제가 되지 않는다.

 

Item의 모든 갯수를 추출하려면 count() 메서드를 사용한다.

text를 이용하여 Item의 index를 찾아내려면 findText()를 사용한다.

 

사용법

count = UI_set.comboBox.count()

 

idx = UI_set.comboBox.findText("Foggy")

UI_set.comboBox.removeItem(idx)

 

 

출처 : https://doc.qt.io/qtforpython/PySide6/QtWidgets/QComboBox.html#

 

 

 

전체 코드

import sys
import os
from PySide6 import QtUiTools, QtGui, QtCore
from PySide6.QtWidgets import QApplication, QMainWindow


class MainView(QMainWindow):


    def __init__(self):
        super().__init__()
        self.setupUI()

 

    def etupUI(self):
        global UI_set, cbox

 

        UI_set = QtUiTools.QUiLoader().load(resource_path("datetest.ui"))

 

        UI_set.BTN_check.clicked.connect(checkcalendar)

        UI_set.comboBox.currentIndexChanged.connect(checkcombobox)

 

        # comboBox 아이템 모두 삭제
        UI_set.comboBox.clear()

 

        # Item 추가
        UI_set.comboBox.addItem("Sunny", "data s")
        UI_set.comboBox.addItem("Cloudy", "data c")
        UI_set.comboBox.addItem("Rainy", "data r")
        UI_set.comboBox.addItem("Foggy", "data f")

 

        # comboBox 기본 값 설정

        UI_set.comboBox.setCurrentText('Foggy')

 

        self.setCentralWidget(UI_set)
        self.setWindowTitle("GUI Program Test")
        self.setWindowIcon(QtGui.QPixmap(resource_path("./images/jbmpa.png")))
        self.resize(730, 490)
        self.show()

 

def checkcombobox():
    index = UI_set.comboBox.currentIndex()
    text = UI_set.comboBox.currentText()
    data = UI_set.comboBox.currentData()

 

    UI_set.TE_result.setText(str(index))
    UI_set.TE_result.append(text)
    UI_set.TE_result.append(data)

 

def checkcalendar():

    # 현재 선택된 시간(시스템 시간)을 저장

    date = UI_set.Calendar.selectedDate()
    UI_set.TE_result.setText(date.toString("yyyy-MM-dd"))

 

    # 현재 선택된 시간을 변경, 자동으로 위젯 페이지 변경
    UI_set.Calendar.setSelectedDate(QtCore.QDate(2012, 7, 12))

    #  변경된 시간을 다시 저장
    date = UI_set.Calendar.selectedDate()
    UI_set.TE_result.append(date.toString("yyyy-MM-dd"))

 

    # 달력 위젯 페이지만 변경

    UI_set.Calendar.setCurrentPage(2012, 7)

 

    # 메서드로 년, 월, 일, 요일 출력하기

    UI_set.TE_result.append(str(date))
    UI_set.TE_result.append(str(date.year()))
    UI_set.TE_result.append(str(date.month()))
    UI_set.TE_result.append(str(date.day()))
    UI_set.TE_result.append(str(date.dayOfWeek()))

# 파일 경로
# pyinstaller로 원파일로 압축할때 경로 필요함
def resource_path(relative_path):
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, relative_path)
    return os.path.join(os.path.abspath("."), relative_path)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = MainView()
    # main.show()
    sys.exit(app.exec())

 

1269731307_1576279299.0106.png

 

1269731307_1576279309.7449.png

 

 

 

 

출처 :

QComboBox - https://doc.qt.io/qtforpython/PySide6/QtWidgets/QComboBox.html

댓글목록 sfs

총 12 건 , 1 페이지
게시물 검색
Pyside6 - GUI for Python 목록
번호 제목 글쓴이 조회 날짜
1 관리자 37408 05-02
2 관리자 31863 05-02
3 관리자 35029 05-03
4 관리자 25287 05-28
5 관리자 29642 05-28
6 관리자 48113 06-11
7 관리자 48614 06-13
8 관리자 40028 12-04
9 관리자 38411 12-04
10 관리자 30979 12-13
11 관리자 21426 12-13
열람중 관리자 23382 12-13
GNUBOARD_M
Copyright © JBMPA.com All rights reserved.