OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Unable to alter the font color within a PYQT6 table. Reading Stack overflow similar questions did not solve my problem

  • Thread starter Thread starter Menachem
  • Start date Start date
M

Menachem

Guest
I wrote a complex program that utilizes a PyQt6 table. I encountered an issue where I was unable to alter the font color within the table. To troubleshoot, I created a smaller program (see below) that generates a single-cell PyQt6 table containing the letter "A" in red font. However, upon executing the code, the character "A" remained black. Below is the code with comments. Your help is highly appreciated.

Code:
import sys
from PyQt6.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QMainWindow
from PyQt6.QtGui import QColor

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

        # Set up the table widget
        self.table = QTableWidget(1, 1)
        self.table.setHorizontalHeaderLabels(["Character"])

        # Create the table item with the "A" character
        item = QTableWidgetItem("A")

        # Set the font color of the item to red
        item.setForeground(QColor('red'))

        # Add the item to the table
        self.table.setItem(0, 0, item)

        # Set the table as the central widget of the window
        self.setCentralWidget(self.table)

        # Set the window title
        self.setWindowTitle("Table")

        # Set the window size
        self.resize(300, 200)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())
<p>I wrote a complex program that utilizes a PyQt6 table. I encountered an issue where I was unable to alter the font color within the table. To troubleshoot, I created a smaller program (see below) that generates a single-cell PyQt6 table containing the letter "A" in red font. However, upon executing the code, the character "A" remained black. Below is the code with comments. Your help is highly appreciated.</p>
<pre><code>import sys
from PyQt6.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QMainWindow
from PyQt6.QtGui import QColor

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

# Set up the table widget
self.table = QTableWidget(1, 1)
self.table.setHorizontalHeaderLabels(["Character"])

# Create the table item with the "A" character
item = QTableWidgetItem("A")

# Set the font color of the item to red
item.setForeground(QColor('red'))

# Add the item to the table
self.table.setItem(0, 0, item)

# Set the table as the central widget of the window
self.setCentralWidget(self.table)

# Set the window title
self.setWindowTitle("Table")

# Set the window size
self.resize(300, 200)

if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
</code></pre>
 

Latest posts

Top