unable to get QComboBox::activateto work
-
Qt 5.15
I have a qcombox. I have this line of code for the connectconnect(pLineEdit, &QLineEdit::returnPressed, this, &cSI_SEARCH_BAR::OnItemSelected2);and that works. The user enters (or selects the text) and then presses return. The event fires and my code does what I want.
I don't want the user to have to press return. The user should be able to select an item and have the code trigger based on that.
I have the following code
connect(this, &QComboBox::textActivated, this, &cSI_SEARCH_BAR::OnItemSelected); connect(this, SIGNAL(activated(int)), this, SLOT(OnItemSelected3(int))); connect(this, SIGNAL(activated(const QString&)), this, SLOT(OnItemSelected(const QString&))); public Q_SLOTS: void OnItemSelected(const QString& string); void OnItemSelected2(); void OnItemSelected3(int index);None of the activated signals result in any of the select methods being called
-
Qt 5.15
I have a qcombox. I have this line of code for the connectconnect(pLineEdit, &QLineEdit::returnPressed, this, &cSI_SEARCH_BAR::OnItemSelected2);and that works. The user enters (or selects the text) and then presses return. The event fires and my code does what I want.
I don't want the user to have to press return. The user should be able to select an item and have the code trigger based on that.
I have the following code
connect(this, &QComboBox::textActivated, this, &cSI_SEARCH_BAR::OnItemSelected); connect(this, SIGNAL(activated(int)), this, SLOT(OnItemSelected3(int))); connect(this, SIGNAL(activated(const QString&)), this, SLOT(OnItemSelected(const QString&))); public Q_SLOTS: void OnItemSelected(const QString& string); void OnItemSelected2(); void OnItemSelected3(int index);None of the activated signals result in any of the select methods being called
@james-b-s
Before delving into what might be wrong here, are you sure you want to be looking for "key presses" and "clicks" here? WouldcurrentTextChanged()and/orcurrentIndexChanged()and maybeeditTextChanged()not suit your purpose? You are usually more interested in user choices than just activations? -
I just tried currentIndexChanged and that won't fire either. I believe that currentTextChanged fired every time someone pressed a key. I don't want that.
I'm currently using the
connect(this, SIGNAL(activated(int)), this, SLOT(OnItemSelected3(int)))
form for the connect. I tried the newer form, but just got compile errors
I tried using QOverload<> but I couldn't find the definition. -
I just tried currentIndexChanged and that won't fire either. I believe that currentTextChanged fired every time someone pressed a key. I don't want that.
I'm currently using the
connect(this, SIGNAL(activated(int)), this, SLOT(OnItemSelected3(int)))
form for the connect. I tried the newer form, but just got compile errors
I tried using QOverload<> but I couldn't find the definition.@james-b-s said in unable to get QComboBox::activateto work:
I just tried currentIndexChanged and that won't fire either.
You have something strange going on if this does not fire. Assuming you are changing which item is selected in the dropdown list!? I would concentrate on getting that working, as you don't have to worry about old style syntax of overloads for that signal. Can you produce a 10 line complete program which just tries to use that. You have no need to subclass
QComboBoxfor that. -
I added this
connect(this, qOverload<int>(&QComboBox::activated), this, &cSI_SEARCH_BAR::OnItemSelected3);
connect(this, qOverload<const QString&>(&QComboBox::activated), this, &cSI_SEARCH_BAR::OnItemSelected);didn't make a difference
@james-b-s Well that has nothing to do with what I suggested, so up to you....
-
Just want to make sure that my understanding is correct. The user clicks on the combo box, the list of choices show up. The user selects one, it shows in the combo box QLineEdit, and one of those triggers I posted should activate.
@james-b-s
In principle, yes. If your code is correct. Like a complete example. You don't need to subclass. If you can't getQComboBox::currentIndexChangedto fire, like you said, you have something wrong regardless of activated or overloading syntax. -
Qt 5.15
I have a qcombox. I have this line of code for the connectconnect(pLineEdit, &QLineEdit::returnPressed, this, &cSI_SEARCH_BAR::OnItemSelected2);and that works. The user enters (or selects the text) and then presses return. The event fires and my code does what I want.
I don't want the user to have to press return. The user should be able to select an item and have the code trigger based on that.
I have the following code
connect(this, &QComboBox::textActivated, this, &cSI_SEARCH_BAR::OnItemSelected); connect(this, SIGNAL(activated(int)), this, SLOT(OnItemSelected3(int))); connect(this, SIGNAL(activated(const QString&)), this, SLOT(OnItemSelected(const QString&))); public Q_SLOTS: void OnItemSelected(const QString& string); void OnItemSelected2(); void OnItemSelected3(int index);None of the activated signals result in any of the select methods being called
@james-b-s
Here is a complete example for you to copy, paste and try:#include <QApplication> #include <QComboBox> #include <QDebug> #include <QHBoxLayout> #include <QLineEdit> #if Qt5 #include <QOverload> #endif int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget w; w.setGeometry(100, 100, 200, 200); QHBoxLayout *layout = new QHBoxLayout(&w); QComboBox *cb = new QComboBox; cb->setEditable(true); layout->addWidget(cb); cb->addItems({"Item1", "Item2", "Item3"}); QObject::connect(cb, &QComboBox::currentIndexChanged, &w, [cb](int index) { qDebug() << "currentIndexChanged" << index << cb->itemText(index); }); QObject::connect(cb, &QComboBox::textActivated, &w, [](const QString &text) { qDebug() << "textActivated" << text; }); QObject::connect(cb->lineEdit(), &QLineEdit::returnPressed, &w, []() { qDebug() << "returnPressed"; }); QObject::connect(cb, QOverload<int>::of(&QComboBox::activated), &w, [cb](int index) { qDebug() << "activated(int)" << index << cb->itemText(index); }); #if Qt5 QObject::connect(cb, QOverload<const QString &>::of(&QComboBox::activated), &w, [](const QString &text) { qDebug() << "activated(const QString &)" << text; }); #endif w.show(); return a.exec(); }You should be seeing the
activatedsignal, as well as the others. The fact that I have connected slots as lambdas is just to keep the code down: will be the same if they are slot methods.Note the following. I do not have Qt 5. I am using Qt6. I have put in some stuff for you which I believe will work/be required in your Qt5:
- I do not need need to
#include <QOverload>. You may need to? - At Qt6 the overload
QComboBox::activated(const QString &)has been removed. That means I do not need to haveQOverload<int>::of(&QComboBox::activated)at all, I could just have&QComboBox::activated. But I have put it in anyway, as you will need it. At Qt5.15 that overload is deprecated, but still exists. - I cannot compile
QOverload<const QString &>::of(&QComboBox::activated)as it no longer exists. I have put it in (I believe correctly) if you want to test it. However, since it will get removed if you upgrade I suggest you stick to theactivated(int)version.
Try this. Play with it if you want to move it into your code and use slot methods. If the test works and your later code does not then you have something wrong in your code.
- I do not need need to