PDA

View Full Version : QPushButton Checked Change



nightroad
21st March 2011, 08:04
Hi there,

I'm using QPushButton like a checkbox , so is there any event about Checked state changed in QPushButton thanks for your help.

Gokulnathvc
21st March 2011, 08:11
QPushButton::ischecked() function

nightroad
21st March 2011, 08:38
isChecked() returns checked state of QPushButton. I wanna catch event everytime changed state of qpushbutton like checkboxs stateChanged(int ) event. thanks ...

Archa4
21st March 2011, 08:41
Hm... was looking through documentation found something called QWidget::changeEvent...

nightroad
21st March 2011, 08:55
thx Archa4 but it's not for checked stateChanged ...

Rhayader
21st March 2011, 08:59
Did you try the toggle signal?
http://doc.qt.nokia.com/4.7-snapshot/qabstractbutton.html#toggle

nightroad
21st March 2011, 09:25
Thanks Rhayader , I just override void QAbstractButton::toggled ( bool checked ) [signal] but it doesn't work, btw my buttons in QButtonGroup , but as document said it works in QButtonGroup but doesn't work as i said ...

Rhayader
21st March 2011, 09:34
You don't need to override the signal. It's a signal. Connect it to a Slot.

Gokulnathvc
21st March 2011, 09:38
http://www.qtcentre.org/faq.php?faq=qt_signalslot........... I think this will help you

nightroad
21st March 2011, 09:47
i need to override for check something then i emit signal. samething actually (;, still doesn't work btw ...

edit : I typed mistakely slot there ! so this is my mistake many people confused then :)

Octal
21st March 2011, 09:50
You don't emit a slot.

Connect the toggled signal to a slot and then check what you have to check ?

Gokulnathvc
21st March 2011, 09:51
Then include your codes in the another function and call Q_Emit, it will trigger it

nightroad
21st March 2011, 09:57
class HSPushButton : public QPushButton
{
Q_OBJECT

public:
HSPushButton(QWidget *parent = 0);
~HSPushButton();

...

private:

...

signals:
...
void stateChanged(bool state);

protected:
virtual void toggled(bool checked);
...
};

HSPushButton.cpp

void HSPushButton::toggled(bool checked)
{
emit (stateChanged(checked));
}


so is it wrong ? I don't think so cuz i'm using escapePressed on my buttons with same method ...

Octal
21st March 2011, 10:00
I don't get what is your goal with your HSPushButton class. Can you give us more detail ? :)

wysota
21st March 2011, 10:01
Yes, it is wrong. toggled() as already noted is a signal not a slot. You can't have two functions (a signal and a slot) with the same signature.

nightroad
21st March 2011, 10:04
Guyz anyway thanks for your help , i'm gonna make it with my own way =) . If i use toggled directly i have to check many thing for each buttons own event so that is crapy ... anyway Thanks all ...

pan
21st March 2011, 10:05
I might be wrong but don't you just want to see which button has been clicked in QButtonGroup?
And does not the signal buttonClicked( int id ) work for you? You can use that on the group rather than individual buttons...

wysota
21st March 2011, 10:06
I'd rather say your approach makes it crappy. We don't know what you are doing but you're certainly having an incorrect approach. If you tell us what is the ultimate goal you wish to achieve maybe we'll be able to suggest a better solution.

nightroad
21st March 2011, 10:08
@ wysota i typed something wrong :)

i need to override for check something then i emit signal. samething actually (;, still doesn't work btw ...

edit : I typed mistakely slot there ! so this is my mistake many people confused then :)

wysota
21st March 2011, 10:12
Just please tell us what you want the HSPushButton class to do what QPushButton doesn't already do.

nightroad
21st March 2011, 10:19
Just please tell us what you want the HSPushButton class to do what QPushButton doesn't already do.

I have many things (private variables, Painters etc... ) in HSPushButton if i use QPushButton toggled signal for each button i have to re-type these controls for each button its own event. If i override to toggled signal in my HSPushButton then emit a signal i don't need the check these conditions for each button. Totally i wanna hide these checks from programmers who will use my HSPushButton widget. thanks ...

Rhayader
21st March 2011, 10:21
Without being sure I would suggest that what you need is in your button's constructor to add

connect(this,SIGNAL(toggled(bool)),this,SLOT(chang edState(bool));

in your .cpp


void HSPushButton::changedState(bool checked)
{
// do your checks here
// and if necessary
// emit (stateChanged(checked));
}

wysota
21st March 2011, 10:44
I have many things (private variables, Painters etc... ) in HSPushButton if i use QPushButton toggled signal for each button i have to re-type these controls for each button its own event.
This suggests all those things should not be part of HSPushButton but rather some external class that does what is required upon receiving some signal from the button or some other place. Using QButtonGroup might be a good idea, something like:


QButtonGroup *group = new QButtonGroup(this);
group->setExclusive(...);
QPushButton *button1 = new QPushButton(...); button1->setCheckable(true);
QPushButton *button2 = new QPushButton(...); button2->setCheckable(true);
QPushButton *button3 = new QPushButton(...); button3->setCheckable(true);
group->addButton(button1, 1);
group->addButton(button2, 2);
group->addButton(button3, 3);
connect(group, SIGNAL(buttonClicked(int)), this, SLOT(checkButton(int))); // or buttonChecked if exclusive
// ...
void X::checkButton(int which){
switch(which) {
case 1: ... ; break;
case 2: ... ; break;
case 3: ... ; break;
// ...
}
}

nightroad
21st March 2011, 10:55
Thank you all guys ...