Results 1 to 3 of 3

Thread: Avoiding unwanted button signals.

  1. #1
    Join Date
    Aug 2009
    Posts
    50
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Avoiding unwanted button signals.

    Hello,

    I wrote the following code example when I was trying to troubleshoot a problem with an editable QComboBox. I solved the original problem, but along the way encountered another issue that has so far defeated me.

    Essentially, the QPushButton in the dialog box seems to emit a 'clicked' signal whenever I press Enter, even if it is the QComboBox that has focus at the time. I discovered this because I was trying to find out why, whenever I edited the text in the QComboBox, an unwanted new item appeared under my newly-edited text. I eventually discovered that the unwanted new item wasn't being inserted by the QComboBox (at least, not so long as I explicitly set the InsertPolicy to QComboBox::InsertAtCurrent), but rather by the "addItem" slot being called in response to a perceived clicked() signal from the button.

    So how can I force the QPushButton to ignore keypresses that are none of its business? Explicitly putting "setDefault(false)" has made no difference.

    Here is the code:

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <Qt/qglobal.h>
    3.  
    4. #include <QDialog>
    5. #include <QLabel>
    6. #include <QComboBox>
    7. #include <QPushButton>
    8. #include <QGridLayout>
    9. #include <QVBoxLayout>
    10.  
    11. class ComboTest : public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. ComboTest(QWidget *parent = 0)
    17. : QWidget(parent)
    18. {
    19. Layout = new QGridLayout;
    20. Label = new QLabel;
    21. Combo = new QComboBox;
    22. Combo -> setEditable(true);
    23. Combo -> addItem("Original default");
    24. Combo -> setInsertPolicy(QComboBox::InsertAtCurrent);
    25. Button = new QPushButton("Add Item");
    26. Button -> setDefault(false);
    27. Label -> setText(Combo -> currentText());
    28.  
    29. Layout->addWidget(Label, 0, 2);
    30. Layout->addWidget(Button, 2, 0);
    31. Layout->addWidget(Combo, 2, 2);
    32. setLayout(Layout);
    33.  
    34. connect(Button, SIGNAL(clicked(const bool &)),
    35. this, SLOT(addItem(const bool &)));
    36. connect(Combo, SIGNAL(editTextChanged(const QString &)),
    37. this, SLOT(synchroniseText(const QString &)));
    38. connect(Combo, SIGNAL(currentIndexChanged(int)),
    39. this, SLOT(showSelection(int)));
    40. }
    41.  
    42. public slots:
    43. void addItem(const bool &checked)
    44. {
    45. Combo -> addItem("New default");
    46. }
    47.  
    48. void synchroniseText(const QString &newText)
    49. {
    50. Label -> setText(newText);
    51. }
    52.  
    53. void showSelection(int newSelection)
    54. {
    55. Label -> setText(Combo -> itemText(newSelection));
    56. }
    57.  
    58. private:
    59. QGridLayout *Layout;
    60. QLabel *Label;
    61. QComboBox *Combo;
    62. QPushButton *Button;
    63. };
    64.  
    65. #include "main.moc"
    66.  
    67. int main(int argc, char *argv[])
    68. {
    69. QApplication app(argc, argv);
    70. QDialog dialog;
    71. QVBoxLayout* layout = new QVBoxLayout(&dialog);
    72. layout->addWidget(new ComboTest);
    73. dialog.setLayout(layout);
    74. return dialog.exec();
    75. }
    To copy to clipboard, switch view to plain text mode 

    Many thanks,
    Stephen.

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Avoiding unwanted button signals.

    Try adding:
    Qt Code:
    1. Button -> setAutoDefault(false);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2009
    Posts
    50
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Avoiding unwanted button signals.

    Quote Originally Posted by numbat View Post
    Try adding:
    Qt Code:
    1. Button -> setAutoDefault(false);
    To copy to clipboard, switch view to plain text mode 
    Yes indeed, that fixes it.

    Excellent!

Similar Threads

  1. how to set Push Button PopMenu Signals?
    By augusbas in forum Newbie
    Replies: 4
    Last Post: 6th July 2009, 05:18
  2. Avoiding system signals
    By brevleq in forum Qt Programming
    Replies: 2
    Last Post: 7th January 2009, 15:37
  3. Unwanted padding on QWidget
    By etru1927 in forum Qt Programming
    Replies: 4
    Last Post: 7th August 2008, 08:42
  4. QMdiArea unwanted actvation
    By fullmetalcoder in forum Qt Programming
    Replies: 7
    Last Post: 12th November 2007, 08:09
  5. Button with signals and slots
    By probine in forum Qt Programming
    Replies: 1
    Last Post: 4th December 2006, 23:24

Tags for this Thread

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.