Results 1 to 17 of 17

Thread: qpushbutton setfocus signal

  1. #1
    Join Date
    Oct 2007
    Posts
    65
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question 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?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qpushbutton setfocus signal

    Quote Originally Posted by skuda View Post
    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.
    J-P Nurmi

  3. #3
    Join Date
    Oct 2007
    Posts
    65
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qpushbutton setfocus signal

    well it is python code but i think it is self-explained:

    Qt Code:
    1. self.connect(self.cancelButton, SIGNAL("clicked()"), lambda: self.accionBotones("cancel"))
    2. self.connect(self.okButton, SIGNAL("clicked()"), lambda: self.accionBotones("ok"))
    3. self.temporal = 0
    4.  
    5. def accionBotones(self, accion):
    6. if accion == "cancel":
    7. self.efectivoSpinBox.setFocus()
    8. self.efectivoSpinBox.selectAll()
    9. self.activaBotones(False)
    10. self.temporal = 0
    11. elif accion == "ok":
    12. self.temporal = self.temporal + 1
    13. #print self.temporal
    14. if self.temporal % 2 == 0:
    15. self.guardaDatos()
    16. QDialog.accept(self)
    17.  
    18. def activaBotones(self, valor):
    19. self.correctoLabel.setEnabled(valor)
    20. self.okButton.setEnabled(valor)
    21. self.cancelButton.setEnabled(valor)
    22. if valor:
    23. self.okButton.setFocus()
    To copy to clipboard, switch view to plain text mode 

    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

  4. #4
    Join Date
    Oct 2007
    Posts
    65
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qpushbutton setfocus signal

    any idea on this one? maybe i should fill a bug report in qt?

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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?
    J-P Nurmi

  6. #6
    Join Date
    Oct 2007
    Posts
    65
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile 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.

    Qt Code:
    1. #!/usr/bin/env python2.5
    2. from PyQt4.QtCore import *
    3. from PyQt4.QtGui import *
    4.  
    5. class TestDlg(QDialog):
    6. def __init__(self, parent=None):
    7. super(TestDlg, self).__init__(parent)
    8. self.testButton = QPushButton()
    9. self.spinBox = QDoubleSpinBox()
    10. self.emitButton = QPushButton()
    11. self.testButton.setText("Test Button")
    12. self.emitButton.setText("Emit Button")
    13. layout = QVBoxLayout()
    14. layout.addWidget(self.testButton)
    15. layout.addWidget(self.spinBox)
    16. layout.addWidget(self.emitButton)
    17. self.setLayout(layout)
    18. self.connect(self.testButton, SIGNAL("clicked()"), self.testButtonPressed)
    19. self.connect(self.spinBox, SIGNAL("editingFinished()"), self.testing)
    20. self.connect(self.emitButton, SIGNAL("clicked()"), self.messageLaunch)
    21.  
    22. def testing(self):
    23. if self.spinBox.value() > 50:
    24. self.emitButton.setFocus()
    25.  
    26. def testButtonPressed(self):
    27. self.emitButton.setFocus()
    28.  
    29. def messageLaunch(self):
    30. QMessageBox.warning(self, "clicked button", "signal clicked() emmited from emitButton", QMessageBox.Ok)
    31.  
    32. if __name__ == "__main__":
    33. import sys
    34. app = QApplication(sys.argv)
    35. dialog = TestDlg()
    36. dialog.show()
    37. app.exec_()
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qpushbutton setfocus signal

    Quote Originally Posted by skuda View Post
    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.

    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.
    Attached Files Attached Files
    J-P Nurmi

  8. #8
    Join Date
    Oct 2007
    Posts
    65
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question 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?

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qpushbutton setfocus signal

    Quote Originally Posted by skuda View Post
    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).. 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
    Qt Code:
    1. emitButton->setAutoDefault(false);
    To copy to clipboard, switch view to plain text mode 
    and the problem is gone. Refer to QPushButton docs for more information about "default buttons" and "auto default buttons".
    J-P Nurmi

  10. The following user says thank you to jpn for this useful post:

    skuda (23rd November 2007)

  11. #10
    Join Date
    Oct 2007
    Posts
    65
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Cool Re: qpushbutton setfocus signal

    Thanks !! i can now delete my super-mega-ugly hack hehehe

  12. #11
    Join Date
    Oct 2007
    Posts
    65
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question 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.

  13. #12
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: qpushbutton setfocus signal

    Maybe using QPushButton::setDefault(true) in Addition to setAutoDefault(false).

  14. #13
    Join Date
    Oct 2007
    Posts
    65
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  15. #14
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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):
    Qt Code:
    1. void keyPressEvent(QKeyEvent* event)
    2. {
    3. if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
    4. if (testButton->hasFocus()) testButton->animateClick();
    5. if (emitButton->hasFocus()) emitButton->animateClick();
    6. }
    7. QDialog::keyPressEvent(event);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by momesana; 23rd November 2007 at 11:26. Reason: adding code

  16. #15
    Join Date
    Oct 2007
    Posts
    65
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: qpushbutton setfocus signal

    i have made this:

    Qt Code:
    1. def keyPressEvent(self, event):
    2. if event.key() == Qt.Key_Enter or event.key() == Qt.Key_Return and (self.okButton.hasFocus() or self.cancelButton.hasFocus()):
    3. print "test"
    4. event.accept()
    5. else:
    6. QDialog.keyPressEvent(self, event)
    To copy to clipboard, switch view to plain text mode 

    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.

  17. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qpushbutton setfocus signal

    Quote Originally Posted by skuda View Post
    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:
    Qt Code:
    1. ...
    2. connect(testButton, SIGNAL(clicked()), SLOT(testButtonPressed()));
    3. connect(spinBox, SIGNAL(editingFinished()), SLOT(testing()));
    4. connect(emitButton, SIGNAL(clicked()), SLOT(messageLaunch()));
    5.  
    6. emitButton->installEventFilter( this );
    7. emitButton->setAutoDefault( false );
    8. emitButton->setDefault( false );
    9.  
    10. testButton->installEventFilter( this );
    11. testButton->setAutoDefault( false );
    12. testButton->setDefault( false );
    13. }
    14.  
    15. bool TestDlg::eventFilter( QObject * o, QEvent * e )
    16. {
    17. QPushButton * b = qobject_cast< QPushButton * >( o );
    18. if( b != 0 && e->type() == QEvent::KeyPress ) {
    19. QKeyEvent * ke = static_cast< QKeyEvent * >( e );
    20. if( ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return ) {
    21. b->animateClick();
    22. return true;
    23. }
    24. }
    25. return false;
    26. }
    To copy to clipboard, switch view to plain text mode 

  18. The following user says thank you to jacek for this useful post:

    skuda (23rd November 2007)

  19. #17
    Join Date
    Oct 2007
    Posts
    65
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Talking Re: qpushbutton setfocus signal

    yeah, this is the real solution, thanks jacek and too all that have helped this

Similar Threads

  1. Replies: 2
    Last Post: 17th May 2006, 21:01
  2. no such signal QListBox::currentChanged()
    By jopie bakker in forum Newbie
    Replies: 2
    Last Post: 2nd March 2006, 15:17
  3. Mouseover event on QPushButton
    By Twey in forum Newbie
    Replies: 2
    Last Post: 13th January 2006, 18:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.