Nov-28-2022, 12:02 AM
Yoriz Wrote:As you are subclassing QTableWidget I'm not sure why you haveI'm not sure either... I just started the tutorial, and somewhat confused.
That fixed the problem with the table being within the window widget, but nor I'm having a table size issue.
I'm pushing myself, and trying to expand on the exercise, but when I make errors, I don't forget how to do it right.
I beleive if properly resized, the table will show properly
At any rate, here's what I have now:
import sys from PySide6.QtWidgets import (QApplication, QMainWindow, QWidget, QTableWidget, QTableWidgetItem, QHeaderView) from PySide6.QtGui import QPalette, QColor class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.setWindowTitle("Table Widget") class Table(QTableWidget): def __init__(self, parent=None): super(Table, self).__init__(parent) self.colors = [("Red", "#FF0000"), ("Green", "#00FF00"), ("Blue", "#0000FF"), ("Black", "#000000"), ("White", "#FFFFFF"), ("Electric Green", "#41CD52"), ("Dark Blue", "#222840"), ("Yellow", "#F9E56d")] def get_rgb_from_hex(self, code): code_hex = code.replace("#", "") rgb = tuple(int(code_hex[i:i+2], 16) for i in (0, 2, 4)) return QColor.fromRgb(rgb[0], rgb[1], rgb[2]) def define_table(self): self.setItemDelegate(self.itemDelegate()) self.setRowCount(len(self.colors)) self.setColumnCount(len(self.colors[0]) + 1) self.setHorizontalHeaderLabels(["Name", "Hex Code", "Color"]) for i, (name, code) in enumerate(self.colors): item_name = QTableWidgetItem(name) item_code = QTableWidgetItem(code) item_color = QTableWidgetItem() item_color.setBackground(self.get_rgb_from_hex(code)) self.setItem(i, 0, item_name) self.setItem(i, 1, item_code) self.setItem(i, 2, item_color) hh = self.horizontalHeader() hh.setSectionResizeMode(QHeaderView.ResizeToContents) def main(): app = QApplication(sys.argv) window = MainWindow() table = Table(parent=window) table.define_table() window.show() table.show() app.exec() if __name__ == '__main__': main()