티스토리 뷰
728x90
반응형
다음 코드처럼 PyQt5로 테이블을 만드는 중에 데이블 안의 아이템안에 체크박스를 넣게 되었다.
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QTableWidget,QTableWidgetItem,QVBoxLayout,QCheckBox
from PyQt5 import Qt,QtCore
class TableWidget(QWidget):
def __init__(self):
super().__init__()
self.title = 'Checkbox in Table'
self.left = 0
self.top = 0
self.width = 640
self.height = 480
self.numRow = 5
self.numCol = 5
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.createTable()
self.layout = QVBoxLayout()
self.layout.addWidget(self.tableWidget)
self.setLayout(self.layout)
# Show widget
self.show()
def createTable(self):
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(self.numRow)
self.tableWidget.setColumnCount(self.numCol)
self.tableWidget.move(0,0)
self.InsertTable()
def InsertTable(self):
self.checkBoxList = []
for i in range(self.numRow):
ckbox = QCheckBox()
self.checkBoxList.append(ckbox)
for i in range(self.numRow):
self.tableWidget.setCellWidget(i,0,self.checkBoxList[i])
self.tableWidget.move(0,0)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = TableWidget()
sys.exit(app.exec_())
살펴볼 함수는 InsertTable로 간단하게 checkBoxList라는 리스트에 QCheckBox를 생성하여 넣고 이를 다음과 같이 테이블 위젯의 setCellWidget함수를 이용하여 넣었다.
self.tableWidget.setCellWidget(i,0,self.checkBoxList[i])
결과는 다음과 같다.

셀 안에서 가운데 정렬을 하고 싶은데 방법이 마땅치 않았다.
알아낸 방법은 다음 자료를 참조하여 해결하였다.
https://stackoverflow.com/questions/16237708/align-checkable-items-in-qtablewidget
stackoverflow.com
간단한 방법은 없는 듯 하고 새로운 셀용 위젯을 하나 만들어 체크박스를 추가하고 가운데 정렬(QtCore.Qt.AlignCenter) 레이아웃을 붙여서 이 위젯을 넣는 방법이다.
주로 바뀐 부분은 InsertTable 함수 부분이다.
def InsertTable(self):
self.checkBoxList = []
for i in range(self.numRow):
ckbox = QCheckBox()
self.checkBoxList.append(ckbox)
for i in range(self.numRow):
cellWidget = QWidget()
layoutCB = QHBoxLayout(cellWidget)
layoutCB.addWidget(self.checkBoxList[i])
layoutCB.setAlignment(QtCore.Qt.AlignCenter)
layoutCB.setContentsMargins(0,0,0,0)
cellWidget.setLayout(layoutCB)
#self.tableWidget.setCellWidget(i,0,self.checkBoxList[i])
self.tableWidget.setCellWidget(i,0,cellWidget)
self.tableWidget.move(0,0)
결과는 다음과 같다.

전체 코드는 다음과 같다.
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QTableWidget,QTableWidgetItem,QVBoxLayout,QCheckBox, QHBoxLayout
from PyQt5 import Qt,QtCore
class TableWidget(QWidget):
def __init__(self):
super().__init__()
self.title = 'Checkbox in Table'
self.left = 0
self.top = 0
self.width = 640
self.height = 480
self.numRow = 5
self.numCol = 5
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.createTable()
self.layout = QVBoxLayout()
self.layout.addWidget(self.tableWidget)
self.setLayout(self.layout)
# Show widget
self.show()
def createTable(self):
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(self.numRow)
self.tableWidget.setColumnCount(self.numCol)
self.tableWidget.move(0,0)
self.InsertTable()
def InsertTable(self):
self.checkBoxList = []
for i in range(self.numRow):
ckbox = QCheckBox()
self.checkBoxList.append(ckbox)
for i in range(self.numRow):
cellWidget = QWidget()
layoutCB = QHBoxLayout(cellWidget)
layoutCB.addWidget(self.checkBoxList[i])
layoutCB.setAlignment(QtCore.Qt.AlignCenter)
layoutCB.setContentsMargins(0,0,0,0)
cellWidget.setLayout(layoutCB)
self.tableWidget.setCellWidget(i,0,cellWidget)
self.tableWidget.move(0,0)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = TableWidget()
sys.exit(app.exec_())
728x90
반응형
'Programming > python' 카테고리의 다른 글
[python] localtime으로 로컬 시간 정보 만들기 (0) | 2019.05.30 |
---|---|
format 관련 자료 링크 (0) | 2019.05.29 |
[jupyter] ImportError: cannot import name 'create_prompt_application' (0) | 2019.05.22 |
requirements.txt 만들고 설치하기 (0) | 2019.05.19 |
파이썬 리스트 슬라이싱 (0) | 2019.04.08 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- comfyUI
- nodejs
- vvvv
- Streamlit
- opencv
- Stable Diffusion
- cura
- 단축키
- Maker
- ubuntu
- docker
- fablab
- WSL
- ssh
- 파이썬
- MicroBit
- 메이커
- git
- Linux
- 한글
- conda
- CAD
- 3d프린터
- Python
- Arduino
- 우분투
- Fusion360
- vscode
- nvidia
- tensorflow
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함