Coder Social home page Coder Social logo

yjg30737 / pyqt-checkbox-table-widget Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 4.0 32 KB

PyQt's QTableWidget which has checkbox as first header item

License: MIT License

Python 100.00%
pyqt5-checkbox-table-widget pyqt-checkbox-table-widget pyqt5-table-widget pyqt-table-widget qtablewidget qcheckbox pyqt5-examples pyqt5-tutorial pyqt5 pyqt qt python3 python

pyqt-checkbox-table-widget's Introduction

pyqt-checkbox-table-widget

PyQt's QTableWidget which has checkbox as first header item

Requirements

PyQt5 >= 5.8

Setup

python -m pip install pyqt-checkbox-table-widget

Method Overview

Many methods is changed/overriden because of the fact that check box being used in first column.

  • setHorizontalHeaderLabels(labels: typing.Iterable[str]) - Sets horizontal header label except for first column (for check box), automatically set the count of the columns based on labels
  • clearContents(start_r_idx=0) - Remove all contents including check box from start_r_idx
  • setDefaultValueOfCheckBox(flag: bool) - Self-explanatory.
  • stretchEveryColumnExceptForCheckBox() - Apply self.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) except for first column, which is for check box.
  • setRowCount(rows: int)
  • insertRow(rows: int)
  • toggleState(state) - Toggle every single checkbox. State should be Qt.CheckState. You can see how to use this feature in Code Example 2 below.
  • getCheckedRows() - Return the list of indexes of row which are checked.
  • getUncheckedRows()
  • setCheckedAt(idx: int, f: bool) - If f is True, the check box at idx is checked. f is False, the check box at idx is unchecked.
  • removeCheckedRows()
  • removeUncheckedRows()

Example

Code Example 1

from PyQt5.QtWidgets import QApplication, QTableWidgetItem
from pyqt_checkbox_table_widget.checkBoxTableWidget import CheckBoxTableWidget

if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    widget = CheckBoxTableWidget()
    widget.setRowCount(3)
    widget.setItem(0, 1, QTableWidgetItem('abc')) # Remember column argument should be at least 1 (if it is zero, item will cover the checkbox cell)
    widget.show()
    app.exec_()

Result

image

Code Example 2

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QTableWidgetItem, QMainWindow, QCheckBox, QVBoxLayout, QWidget
from pyqt_checkbox_table_widget.checkBoxTableWidget import CheckBoxTableWidget


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.__initUi()

    def __initUi(self):
        allChkBox = QCheckBox('Check all')
        tableWidget = CheckBoxTableWidget()
        tableWidget.setRowCount(10)
        tableWidget.stretchEveryColumnExceptForCheckBox() # stretch every section of tablewidget except for check box section
        for i in range(tableWidget.rowCount()):
            item = QTableWidgetItem()
            item.setTextAlignment(Qt.AlignCenter) # align
            item.setText(str(i)*50) # text sample
            tableWidget.setItem(i, 1, item)
        allChkBox.stateChanged.connect(tableWidget.toggleState) # if allChkBox is checked, tablewidget checkboxes will also be checked 

        lay = QVBoxLayout()
        lay.addWidget(allChkBox)
        lay.addWidget(tableWidget)

        mainWidget = QWidget()
        mainWidget.setLayout(lay)

        self.setCentralWidget(mainWidget)


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()

Result

example.mp4

Similar package

pyqt-checkbox-list-widget

pyqt-checkbox-table-widget's People

Contributors

demon071 avatar yjg30737 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

pyqt-checkbox-table-widget's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.