Friday, 15 July 2011

pyqt - Using QAction with QLabel and creating accordion using QStackedWidget -



pyqt - Using QAction with QLabel and creating accordion using QStackedWidget -

i trying add together qaction object qlabel object using qlabel.addaction() method, doesn't seem work. not supposed work or doing wrong?

i trying create accordion using qstackedwidget.

for need section title either hide or show title's section when user presses on title. utilize mousereleasedevent, prefer proper qaction toggle() implementation. maybe utilize else qlabel matter?

the addaction functionality of qwidget used providing context menus , not straight relate action triggered when mouse clicked on label.

you hence must utilize kind of mousexxxevent.

if prefer signals instead, quite easy:

from pyside.qtgui import * pyside.qtcore import * class clickablelabel(qlabel): """ label emits signal when clicked. """ clicked = signal() def __init__(self, *args): super().__init__(*args) def mousepressevent(self, event): self.clicked.emit() # illustration app = qapplication([]) window = qwidget() layout = qvboxlayout(window) labela = clickablelabel('click on me more.') layout.addwidget(labela) labelb = qlabel('here am.') layout.addwidget(labelb) labelb.hide() labela.clicked.connect(labelb.show) window.show() app.exec_()

or if want action instead, create this:

from pyside.qtgui import * pyside.qtcore import * class clickablelabel(qlabel): """ label emits signal when clicked. """ def __init__(self, *args): super().__init__(*args) def mousepressevent(self, event): self.action.triggered.emit() # illustration app = qapplication([]) window = qwidget() layout = qvboxlayout(window) labela = clickablelabel('click on me more.') layout.addwidget(labela) labelb = qlabel('here am.') layout.addwidget(labelb) labelb.hide() action = qaction('action', labela) labela.action = action action.triggered.connect(labelb.show) window.show() app.exec_()

the illustration in python 3.x notation , pyside translation python 2.x or pyqt quite simple.

pyqt

No comments:

Post a Comment