ResizeSection of horizontalHeader doesn't resize a section when setStyleSheet is set
-
I can't figure out what is wrong.
It seems like resizeSection of horizontalHeader in QTableview doesn't make any effect when some else setStyleSheet is set. Here is my code:import sys from PyQt6.QtSql import QSqlTableModel from PyQt6.QtWidgets import ( QApplication, QMainWindow, QVBoxLayout, QWidget, QTableView, ) from db import make_check_db_connection class TestWindow(QMainWindow): def __init__(self, parent=None): super().__init__(parent) layuot = QVBoxLayout() self.model = QSqlTableModel(db=make_check_db_connection()) self.model.setTable("Rests") self.table = QTableView() self.table.setModel(self.model) self.model.select() # self.setStyleSheet("QMainWindow {background-color: yellow;}") self.table.horizontalHeader().setStyleSheet("QHeaderView::section { background-color: green;}") self.table.horizontalHeader().resizeSection(1, 300) layuot.addWidget(self.table) widget = QWidget() widget.setLayout(layuot) self.setCentralWidget(widget) self.show() app = QApplication(sys.argv) window = TestWindow() window.show() app.exec()
img1

Img2
The results for commented and uncommentedself.setStyleSheet("QMainWindow {background-color: yellow;}")are on Img1 and Img2 respectively.
So for uncommented case the width of 1st section remains unchanged.
Ideally I would like to set QSS at the TestWindow level for other widgets but it also breaks resizeSection.
I would appreciate any help. -
I can't figure out what is wrong.
It seems like resizeSection of horizontalHeader in QTableview doesn't make any effect when some else setStyleSheet is set. Here is my code:import sys from PyQt6.QtSql import QSqlTableModel from PyQt6.QtWidgets import ( QApplication, QMainWindow, QVBoxLayout, QWidget, QTableView, ) from db import make_check_db_connection class TestWindow(QMainWindow): def __init__(self, parent=None): super().__init__(parent) layuot = QVBoxLayout() self.model = QSqlTableModel(db=make_check_db_connection()) self.model.setTable("Rests") self.table = QTableView() self.table.setModel(self.model) self.model.select() # self.setStyleSheet("QMainWindow {background-color: yellow;}") self.table.horizontalHeader().setStyleSheet("QHeaderView::section { background-color: green;}") self.table.horizontalHeader().resizeSection(1, 300) layuot.addWidget(self.table) widget = QWidget() widget.setLayout(layuot) self.setCentralWidget(widget) self.show() app = QApplication(sys.argv) window = TestWindow() window.show() app.exec()
img1

Img2
The results for commented and uncommentedself.setStyleSheet("QMainWindow {background-color: yellow;}")are on Img1 and Img2 respectively.
So for uncommented case the width of 1st section remains unchanged.
Ideally I would like to set QSS at the TestWindow level for other widgets but it also breaks resizeSection.
I would appreciate any help.What Qt version do you use?
-
What Qt version do you use?
@Christian-Ehrlicher said in ResizeSection of horizontalHeader doesn't resize a section when setStyleSheet is set:
What Qt version do you use?
Name: PyQt6
Version: 6.6.1 -
@Christian-Ehrlicher said in ResizeSection of horizontalHeader doesn't resize a section when setStyleSheet is set:
What Qt version do you use?
Name: PyQt6
Version: 6.6.1 -
@Christian-Ehrlicher
got it, thanks -
S SGaist moved this topic from General and Desktop on