vendredi 17 septembre 2021

How can I Align to center a CheckBox inside of a QTable Widget? - PyQt5 [duplicate]

I wish to align all checkboxes in the column highlighted in the picture.

enter image description here

As of now, I've tried to do Barry Marvin's answer form here

This is the code I've tried from Barry Marvin's answer:

app = QApplication(sys.argv)
app.setStyleSheet('''
    QTableWidget.QTextBox {margin-left:50%; margin-right:50%;}
    ''')

This is the code I used to reproduce the issue:

from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5 import uic
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt
from PyQt5 import QtCore, QtWidgets
import sys

class MyApp(QWidget):
    def __init__(self):
        super().__init__()
        uic.loadUi("D:\\OCR\\untitled.ui", self)
        self.setWindowTitle("Editor")

        for i in range(10):
            rowCount = self.tableWidget.rowCount()
            self.tableWidget.insertRow(rowCount)
            #add checkboxes
            item = QtWidgets.QTableWidgetItem()
            item.setCheckState(QtCore.Qt.Unchecked)
            self.tableWidget.setItem(rowCount, 1, item)
            #add words
            item2 = QtWidgets.QTableWidgetItem()
            item2.setText(i)
            self.tableWidget.setItem(rowCount, 0, item2)

def gui():
    app = QApplication(sys.argv)
    app.setStyleSheet('''
        QTableWidget.QTextBox {margin-left:50%; margin-right:50%;}
        ''')
    myApp = MyApp()
    myApp.show()

    try:
        sys.exit(app.exec())
    except:
        print('Closing Window')

gui()

If needed, I also included the names of the widget inside my untitled.ui file

names of the widget inside my untitled.ui file




Aucun commentaire:

Enregistrer un commentaire