Issue
I have a layout named self.box
and I want to select one of the indexes of this layout with the itemAt
method
and change the related style
but i get this error:
'QWidgetItem' object has no attribute 'setStyleSheet'
Note that I know that I can style each of the buttons separately
But I want to know if there is a way to style the QWidgetItem
code:
from PyQt6.QtWidgets import *
from PyQt6.QtCore import *
from PyQt6.QtGui import *
class main(QMainWindow):
def __init__(self):
super().__init__()
self.box = QHBoxLayout()
self.widget = QWidget()
self.widget.setLayout(self.box)
self.button = QPushButton('button1')
self.button2 = QPushButton('button2')
self.button3 = QPushButton('button3')
self.box.addWidget(self.button)
self.box.addWidget(self.button2)
self.box.addWidget(self.button3)
self.box.itemAt(1).setStyleSheet('background-color:red;')
self.setCentralWidget(self.widget)
self.show()
app = QApplication([])
m = main()
m.show()
app.exec()
Solution
I found the answer myself:
self.box.itemAt(1).widget().setStyleSheet('background-color:red')
Answered By - coder
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.