qpushbutton setfocus signal
Hello all,
i want to enable a button and set focus to it but when i use the setfocus method the button launch signals "pressed()" and "clicked()" so the connected function launch too without press the button, i have workaround actually with a autoincremental number, testing if it is par but how can i really fix this?
Re: qpushbutton setfocus signal
Quote:
Originally Posted by
skuda
when i use the setfocus method the button launch signals "pressed()" and "clicked()"
Can we see the code? I just wrote a test and it doesn't do anything like that for me with Qt 4.3.2.
Re: qpushbutton setfocus signal
well it is python code but i think it is self-explained:
Code:
self.connect(self.cancelButton, SIGNAL("clicked()"), lambda: self.accionBotones("cancel"))
self.connect(self.okButton, SIGNAL("clicked()"), lambda: self.accionBotones("ok"))
self.temporal = 0
def accionBotones(self, accion):
if accion == "cancel":
self.efectivoSpinBox.setFocus()
self.efectivoSpinBox.selectAll()
self.activaBotones(False)
self.temporal = 0
elif accion == "ok":
self.temporal = self.temporal + 1
#print self.temporal
if self.temporal % 2 == 0:
self.guardaDatos()
def activaBotones(self, valor):
self.correctoLabel.setEnabled(valor)
self.okButton.setEnabled(valor)
self.cancelButton.setEnabled(valor)
if valor:
self.okButton.setFocus()
when i use activaBotones function with True value in valor variable, the okButton launch the signal clicked so i maintain a number and only launch the relevant code if it is the second exec on the slot (when the user really press the button) i reset too the number if the user press cancel to fix any values
Re: qpushbutton setfocus signal
any idea on this one? maybe i should fill a bug report in qt?
Re: qpushbutton setfocus signal
Well, PyQt is not a Trolltech product. :) Anyway, could you write a minimal but complete app we can run ourselves to see the problem?
Re: qpushbutton setfocus signal
Yes i know it is not a Trolltech product hehehe but i have found 3 bugs in qt 4.3.2 from pyqt and all of them was from qt (i reported it in task tracker) pyqt only wraps the qt libraries so if the problem is inside qt objects usually is a qt problem, here i paste a working example using python 2.5.1, PyQt 4.3.1 and QT 4.3.2, i have found that the problem happens when i come from a spinbox (testing after editingfinished signal that the doublespinbox value is > 50) but if i execute setFocus from other button for example all works ok.
Code:
#!/usr/bin/env python2.5
from PyQt4.QtCore import *
from PyQt4.QtGui import *
def __init__(self, parent=None):
super(TestDlg, self).__init__(parent)
self.testButton.setText("Test Button")
self.emitButton.setText("Emit Button")
layout.addWidget(self.testButton)
layout.addWidget(self.spinBox)
layout.addWidget(self.emitButton)
self.setLayout(layout)
self.connect(self.testButton, SIGNAL("clicked()"), self.testButtonPressed)
self.connect(self.spinBox, SIGNAL("editingFinished()"), self.testing)
self.connect(self.emitButton, SIGNAL("clicked()"), self.messageLaunch)
def testing(self):
if self.spinBox.value() > 50:
self.emitButton.setFocus()
def testButtonPressed(self):
self.emitButton.setFocus()
def messageLaunch(self):
if __name__ == "__main__":
import sys
dialog = TestDlg()
dialog.show()
app.exec_()
1 Attachment(s)
Re: qpushbutton setfocus signal
Quote:
Originally Posted by
skuda
Yes i know it is not a Trolltech product hehehe but i have found 3 bugs in qt 4.3.2 from pyqt and all of them was from qt (i reported it in task tracker) pyqt only wraps the qt libraries so if the problem is inside qt objects usually is a qt problem
Yes, but the point was (as I said in my first post) that I couldn't reproduce the problem with Qt 4.3.2 in the first place.
Quote:
i have found that the problem happens when i come from a spinbox (testing after editingfinished signal that the doublespinbox value is > 50) but if i execute setFocus from other button for example all works ok.
I have ported the test application to C++ and I still can't reproduce the problem. I have attached the C++ version in case anyone else wants to give it a try.
Re: qpushbutton setfocus signal
wow jpn i have compiled your test and i have the same problem with it that with my pyqt code, if i set the spinbox above 50 and press enter i get the messagebox of the emitted signal from the button, hmmm can be any problem with my qt compilation?
Re: qpushbutton setfocus signal
Quote:
Originally Posted by
skuda
wow jpn i have compiled your test and i have the same problem with it that with my pyqt code, if i set the spinbox above 50 and press enter i get the messagebox of the emitted signal from the button, hmmm can be any problem with my qt compilation?
Ahh, that's it! I didn't realize to press enter but only switched focus to other widget (which causes editingFinished() to be emitted as well).. :p Anyway, the "emit button" is the default button of the dialog so that's why it gets clicked by pressing enter. In C++, you would add
Code:
emitButton->setAutoDefault(false);
and the problem is gone. Refer to QPushButton docs for more information about "default buttons" and "auto default buttons".
Re: qpushbutton setfocus signal
Thanks !! i can now delete my super-mega-ugly hack hehehe
Re: qpushbutton setfocus signal
i now have other problem, with autodefault false if i hit enter the button is not clicked, i have to press space, this is not a problem for me but for my users can be like burn in the hell (aprox.), any idea about how can fix this or should i go to my ugly hack other time? PD: i would like to avoid subclass qpushbutton if possible.
Re: qpushbutton setfocus signal
Maybe using QPushButton::setDefault(true) in Addition to setAutoDefault(false).
Re: qpushbutton setfocus signal
if i set default after the setfocus it is automatically clicked because the enter pressed before to set the value in the spinbox and i have two buttons in the same dialog, the two should be accept enter key on focus.
Re: qpushbutton setfocus signal
Then it may be appropriate to use an event handler for that purpose. Reimplement keyPressEvent(QKeyEvent *)
The C++ version would look like this (both buttons must be implemented as members):
Code:
{
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
if (testButton->hasFocus()) testButton->animateClick();
if (emitButton->hasFocus()) emitButton->animateClick();
}
}
Re: qpushbutton setfocus signal
i have made this:
Code:
def keyPressEvent(self, event):
if event.key() == Qt.Key_Enter or event.key() == Qt.Key_Return and (self.okButton.hasFocus() or self.cancelButton.hasFocus()):
print "test"
event.accept()
else:
and i still have the same problem, when i press the enter in the doublespinbox, the okButton get the focus and automatically it is clicked.
Re: qpushbutton setfocus signal
Quote:
Originally Posted by
skuda
when i press the enter in the doublespinbox, the okButton get the focus and automatically it is clicked.
Only "default" buttons react to return/enter and they disregard the focus in that matter.
Try this:
Code:
...
connect(testButton, SIGNAL(clicked()), SLOT(testButtonPressed()));
connect(spinBox, SIGNAL(editingFinished()), SLOT(testing()));
connect(emitButton, SIGNAL(clicked()), SLOT(messageLaunch()));
emitButton->installEventFilter( this );
emitButton->setAutoDefault( false );
emitButton->setDefault( false );
testButton->installEventFilter( this );
testButton->setAutoDefault( false );
testButton->setDefault( false );
}
{
if( b
!= 0 && e
->type
() == QEvent::KeyPress ) { if( ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return ) {
b->animateClick();
return true;
}
}
return false;
}
Re: qpushbutton setfocus signal
yeah, this is the real solution, thanks jacek and too all that have helped this :)