Nov-27-2022, 10:42 PM
I wrote some early Qt code using the designer, and have used tkinter and wxpython almost exclusively since.
I am now in early learning of QT using Pyside6, (finally working on an application where GUI will be exclusively QT).
I'm trying to place a QTableWidget on top of a QMainWindow, and am doing something wrong, but not sure what.
Could use some help.
here's the code I attempted to write:
I am now in early learning of QT using Pyside6, (finally working on an application where GUI will be exclusively QT).
I'm trying to place a QTableWidget on top of a QMainWindow, and am doing something wrong, but not sure what.
Could use some help.
here's the code I attempted to write:
import sys from PySide6.QtWidgets import (QApplication, QMainWindow, QWidget, QTableWidget, QTableWidgetItem) 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")] self.table = QTableWidget() 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.table.setRowCount(len(self.colors)) self.table.setColumnCount(len(self.colors[0]) + 1) self.table.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.table.setItem(i, 0, item_name) self.table.setItem(i, 1, item_code) self.table.setItem(i, 2, item_color) def main(): app = QApplication(sys.argv) window = MainWindow() table = Table(parent = window) table.define_table() # window.show() table.show() window.show() app.exec() if __name__ == '__main__': main()