Issue
Here is my sample code. I have a horizontal and vertical row, and I will take my text in both horizontal and vertical. When I select the any button in layouts, that key button I want to change as blue colour when i choose the another button that button automatically update the blue colour and previous button will come in original colour Given below is my code:
import sys
from PySide import QtGui,QtCore
from functools import partial
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.hbox = QtGui.QHBoxLayout()
self.v1box = QtGui.QVBoxLayout()
self.v2box = QtGui.QVBoxLayout()
self.labl = QtGui.QLabel("horizontal")
self.v1box.addWidget(self.labl)
for key in ['1', '2', '3', '4', '5', '6']:
self.btns1 = QtGui.QPushButton(str(key))
self.btns1.clicked.connect(partial(self.text1, key, "horizontal"))
self.v1box.addWidget(self.btns1)
self.lab2 = QtGui.QLabel("vertical")
self.v2box.addWidget(self.lab2)
for key in ['1', '2', '3', '4', '5', '6']:
self.btns2 = QtGui.QPushButton(str(key))
self.btns2.clicked.connect(partial(self.text2, key,"Vertical"))
self.v2box.addWidget(self.btns2)
self.hbox.addLayout(self.v1box)
self.hbox.addLayout(self.v2box)
self.setLayout(self.hbox)
def text1(self,key,type):
if type == "horizontal":
X = int(str(key))
print X, "xxx"
def text2(self,key,type):
if type == "vertical":
Y = int(str(key))
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Solution
You can enable the checkeable property of the QPushButton and use a QButtonGroup so that only one of them is checked. Then using the checked pseudo-state sets the desired style.
import sys
from PySide import QtCore, QtGui
from functools import partial
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.setStyleSheet("QPushButton::checked{ background-color:blue; border: none; }")
v1box = QtGui.QVBoxLayout()
v1box.addWidget(QtGui.QLabel("horizontal"))
group1 = QtGui.QButtonGroup(self)
for key in ['1', '2', '3', '4', '5', '6']:
btn = QtGui.QPushButton(str(key), checkable=True)
btn.clicked.connect(partial(self.text1, key, "horizontal"))
v1box.addWidget(btn)
group1.addButton(btn)
v2box = QtGui.QVBoxLayout()
v2box.addWidget(QtGui.QLabel("vertical"))
group2 = QtGui.QButtonGroup(self)
for key in ['1', '2', '3', '4', '5', '6']:
btn = QtGui.QPushButton(str(key), checkable=True)
btn.clicked.connect(partial(self.text2, key,"vertical"))
v2box.addWidget(btn)
group2.addButton(btn)
hbox = QtGui.QHBoxLayout(self)
hbox.addLayout(v1box)
hbox.addLayout(v2box)
def text1(self, key, direction):
if direction == "horizontal":
X = int(str(key))
print(X, "xxx")
def text2(self, key, direction):
if direction == "vertical":
Y = int(str(key))
print(Y, "yyy")
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.