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.