Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. unable to get QComboBox::activateto work

unable to get QComboBox::activateto work

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 156 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    james b-s
    wrote last edited by
    #1

    Qt 5.15
    I have a qcombox. I have this line of code for the connect

    connect(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

    JonBJ 2 Replies Last reply
    0
    • J james b-s

      Qt 5.15
      I have a qcombox. I have this line of code for the connect

      connect(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

      JonBJ Online
      JonBJ Online
      JonB
      wrote last edited by
      #2

      @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? Would currentTextChanged() and/or currentIndexChanged() and maybe editTextChanged() not suit your purpose? You are usually more interested in user choices than just activations?

      1 Reply Last reply
      0
      • J Offline
        J Offline
        james b-s
        wrote last edited by
        #3

        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.

        JonBJ 1 Reply Last reply
        0
        • J james b-s

          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.

          JonBJ Online
          JonBJ Online
          JonB
          wrote last edited by
          #4

          @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 QComboBox for that.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            james b-s
            wrote last edited by
            #5

            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

            JonBJ 1 Reply Last reply
            0
            • J james b-s

              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

              JonBJ Online
              JonBJ Online
              JonB
              wrote last edited by
              #6

              @james-b-s Well that has nothing to do with what I suggested, so up to you....

              1 Reply Last reply
              0
              • J Offline
                J Offline
                james b-s
                wrote last edited by
                #7

                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.

                JonBJ 1 Reply Last reply
                0
                • J james b-s

                  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.

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote last edited by
                  #8

                  @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 get QComboBox::currentIndexChanged to fire, like you said, you have something wrong regardless of activated or overloading syntax.

                  1 Reply Last reply
                  0
                  • J james b-s

                    Qt 5.15
                    I have a qcombox. I have this line of code for the connect

                    connect(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

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote last edited by JonB
                    #9

                    @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 activated signal, 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 have QOverload<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 the activated(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.

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved