qt - How to force QActionGroup to allow uncheck? -
i have number of actions put in qactiongroup, because want them exclusive (if 1 checked, others should unchecked). however, want able uncheck of them. @ moment if check action1, , click button again, not uncheck, way uncheck click on different action. in short, qactiongroup ensures 1 of actions checked - want ensure 1 or 0 actions checked. there workaround this?
i'm using pyside i'm comfortable qt in c++ well, solution in c++ fine - i'm sure convert it.
code (not helpful, i'm posting in case relevant)
def definegroups(self): navigation = qactiongroup(self) navigation.addaction(self.action1) navigation.addaction(self.action2) navigation.addaction(self.action3) navigation.addaction(self.action4) navigation.addaction(self.action5) navigation.addaction(self.action6) navigation.addaction(self.action7) all actions checkable (set in qt designer).
related: there bug report similar this, designers have decided not change anything: https://bugreports.qt-project.org/browse/qtbug-3512
i using qt in c++ instead of python, hope these information help.
unfortunately there seems no direct solution problem @ present. "exclusive" property of qactiongroup limits feature of selection, ensures @ least , 1 action selected @ 1 time (but @ begining of program, have manually set 1 of actions "checked")
here part of source code, can see how works on qaction if "exclusive" property installed qactiongroup
{ if ( !isexclusive() ) return; qaction* s = (qaction*) sender(); if ( b ) { if ( s != d->selected ) { d->selected = s; ( qptrlistiterator<qaction> it( d->actions); it.current(); ++it ) { if ( it.current()->istoggleaction() && it.current() != s ) it.current()->seton( false ); } emit activated(); emit selected( s ); } else if ( !s->istoggleaction() ) { emit activated(); } } else { if ( s == d->selected ) { // @ least 1 has selected s->seton( true ); } } } (ref: http://qt-x11-free.sourcearchive.com/documentation/3.3./classqactiongroup_182fe3a5a859e6397a4ba05be346d894.html older version of qt, concept same)
to solve problem, @ least have 2 options:
still use
qactiongroup, addqaction"none selected" (or similar) in list. might proper option since considers "no selection" selection , can exploit feature ofqactiongroup.not use
qactiongroup. in case, have manually implement exclusiveness. onceqactionselected,emitsignal sends pointerqaction*slotcompares present selection previous 1 buffer (default = 0), if difference occurs, deselect (usesetcheck(false)) previous 1 , assign present selection buffer.
it's rough idea, still feasible if need feature
[edit]: though not sure exact functionality want, wrote c++ code here fyr. notice demo of "ostensible" feature of exclusively checkable qactions , ability of unchecking checked one.the according responses of checked/unchecked state still need further implementation.
theclass.h:
class theclass { q_object ...... public slots: void action0checked(); void action1checked(); void action2checked(); void comparechecked(qaction *newchecked); signals: void selectedid(qaction *newchecked); private: qaction *buffer; qlist<qaction*> actionlist; } theclass.cpp:
theclass::theclass() { qmenubar *menubar = new qmenubar(this); buffer = 0; qmenu *menu = menubar->addmenu("actionlist"); actionlist.append(menu->addaction("action0")); actionlist.append(menu->addaction("action1")); actionlist.append(menu->addaction("action2")); (int i=0; i<3; i++) { actionlist[i]->setcheckable(true); } connect(actionlist[0], signal(triggered()), this, slot(action0checked())); connect(actionlist[1], signal(triggered()), this, slot(action1checked())); connect(actionlist[2], signal(triggered()), this, slot(action2checked())); connect(this, signal(selectedid(qaction*)), this, slot(comparechecked(qaction*))); } void theclass::action0checked() { if (actionlist[0]->ischecked()) { // sth. emit selectedid(actionlist[0]); } } void theclass::action1checked() { if (actionlist[1]->ischecked()) { // sth. emit selectedid(actionlist[1]); } } void theclass::action2checked() { if (actionlist[2]->ischecked()) { // sth. emit selectedid(actionlist[2]); } } void theclass::comparechecked(qaction *newchecked) { if (newchecked != buffer) { if (buffer != 0) buffer->setchecked(false); buffer = newchecked; } }
Comments
Post a Comment