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. Add item to combobox when it is "touched"
Forum Updated to NodeBB v4.3 + New Features

Add item to combobox when it is "touched"

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 660 Views 2 Watching
  • 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.
  • ademmlerA Offline
    ademmlerA Offline
    ademmler
    wrote on last edited by
    #1

    I would like to add a list of items (as example a list of files in a folder) to a combobox
    dynamically when the user clicks on this combobox.

    I tried this - by the way how would this be in the new syntax?

    connect(ui->combo_Measurements, SIGNAL(aboutToShow()), this, SLOT(slotGetMeasurementsList()));
    

    And I tried:

    void MyApp::on_combo_Measurements_highlighted(int index)
    {
      QStringList files = measurementsFolder.entryList(QStringList() << "*.*", QDir::Files);
        ui->combo_Measurements->clear();
        if(! files.isEmpty()) {
            foreach (QString fileName, files) {
                QString filePath = QDir::toNativeSeparators(measurementsFolder.filePath(fileName));
                ui->combo_Measurements->addItem(fileName, filePath);
            }
        }
    }
    

    The later is partially "working" but I cant select any entry then, because the actualisation takes permanently place. Therefore I am looking for the better (correct) way of doing this.

    Pl45m4P 1 Reply Last reply
    0
    • ademmlerA ademmler

      I would like to add a list of items (as example a list of files in a folder) to a combobox
      dynamically when the user clicks on this combobox.

      I tried this - by the way how would this be in the new syntax?

      connect(ui->combo_Measurements, SIGNAL(aboutToShow()), this, SLOT(slotGetMeasurementsList()));
      

      And I tried:

      void MyApp::on_combo_Measurements_highlighted(int index)
      {
        QStringList files = measurementsFolder.entryList(QStringList() << "*.*", QDir::Files);
          ui->combo_Measurements->clear();
          if(! files.isEmpty()) {
              foreach (QString fileName, files) {
                  QString filePath = QDir::toNativeSeparators(measurementsFolder.filePath(fileName));
                  ui->combo_Measurements->addItem(fileName, filePath);
              }
          }
      }
      

      The later is partially "working" but I cant select any entry then, because the actualisation takes permanently place. Therefore I am looking for the better (correct) way of doing this.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @ademmler said in Add item to combobox when it is "touched":

      connect(ui->combo_Measurements, SIGNAL(aboutToShow()), this, SLOT(slotGetMeasurementsList()));

      What is this signal?
      IIRC aboutToShow() is a signal from QMenu. There is nothing like that in QComboBox.

      he later is partially "working" but I cant select any entry then, because the actualisation takes permanently place

      Then don't choose the highlighted signal as trigger for your code.
      Put it in some extra function and call this function in mousePressEvent, for example.

      Or work with QComboBox ability to display model data. Create a model which holds your data and let the QComboBox view display it.

      • https://doc.qt.io/qt-6/qcombobox.html#model-view-framework

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      ademmlerA 1 Reply Last reply
      1
      • Pl45m4P Pl45m4

        @ademmler said in Add item to combobox when it is "touched":

        connect(ui->combo_Measurements, SIGNAL(aboutToShow()), this, SLOT(slotGetMeasurementsList()));

        What is this signal?
        IIRC aboutToShow() is a signal from QMenu. There is nothing like that in QComboBox.

        he later is partially "working" but I cant select any entry then, because the actualisation takes permanently place

        Then don't choose the highlighted signal as trigger for your code.
        Put it in some extra function and call this function in mousePressEvent, for example.

        Or work with QComboBox ability to display model data. Create a model which holds your data and let the QComboBox view display it.

        • https://doc.qt.io/qt-6/qcombobox.html#model-view-framework
        ademmlerA Offline
        ademmlerA Offline
        ademmler
        wrote on last edited by ademmler
        #3

        @Pl45m4 said in Add item to combobox when it is "touched":

        Hi, thx for your feedback.

        @ademmler said in Add item to combobox when it is "touched":

        connect(ui->combo_Measurements, SIGNAL(aboutToShow()), this, SLOT(slotGetMeasurementsList()));

        What is this signal?
        IIRC aboutToShow() is a signal from QMenu. There is nothing like that in QComboBox.

        This was simply a try - because with menus for example it works and the compiler/Qt did not complained about this.

        he later is partially "working" but I cant select any entry then, because the actualisation takes permanently place

        Then don't choose the highlighted signal as trigger for your code.
        Put it in some extra function and call this function in mousePressEvent, for example.

        Or work with QComboBox ability to display model data. Create a model which holds your data and let the QComboBox view display it.

        • https://doc.qt.io/qt-6/qcombobox.html#model-view-framework

        Ok, thx for the hint. I actually try this and it seems to work:

        ui->combo_Measurements->installEventFilter(this);
        ....
        bool MyApp::eventFilter(QObject *f_object, QEvent *f_event){
           if(f_object == ui->combo_Measurements){
               if(f_event->type() == QEvent::MouseButtonPress){
                   slotGetMeasurementsList(); 
               }
               return false;
           }
           return false;
        }
        
        JonBJ Pl45m4P 2 Replies Last reply
        0
        • ademmlerA ademmler

          @Pl45m4 said in Add item to combobox when it is "touched":

          Hi, thx for your feedback.

          @ademmler said in Add item to combobox when it is "touched":

          connect(ui->combo_Measurements, SIGNAL(aboutToShow()), this, SLOT(slotGetMeasurementsList()));

          What is this signal?
          IIRC aboutToShow() is a signal from QMenu. There is nothing like that in QComboBox.

          This was simply a try - because with menus for example it works and the compiler/Qt did not complained about this.

          he later is partially "working" but I cant select any entry then, because the actualisation takes permanently place

          Then don't choose the highlighted signal as trigger for your code.
          Put it in some extra function and call this function in mousePressEvent, for example.

          Or work with QComboBox ability to display model data. Create a model which holds your data and let the QComboBox view display it.

          • https://doc.qt.io/qt-6/qcombobox.html#model-view-framework

          Ok, thx for the hint. I actually try this and it seems to work:

          ui->combo_Measurements->installEventFilter(this);
          ....
          bool MyApp::eventFilter(QObject *f_object, QEvent *f_event){
             if(f_object == ui->combo_Measurements){
                 if(f_event->type() == QEvent::MouseButtonPress){
                     slotGetMeasurementsList(); 
                 }
                 return false;
             }
             return false;
          }
          
          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @ademmler said in Add item to combobox when it is "touched":

          This was simply a try - because with menus for example it works and the compiler/Qt did not complained about this.

          Do not ever use old-style signal/slot syntax. It was replaced over a decade ago.

          connect(ui->combo_Measurements, SIGNAL(aboutToShow()), this, SLOT(slotGetMeasurementsList()));
          # replace by
          connect(ui->combo_Measurements, &QMenu::aboutToShow, this, &ThisClass::slotGetMeasurementsList);
          

          If you did this you would see, and the compiler would complain, that QMenu::aboutToShow does not go with ui->combo_Measurements, which is presumably a QComboBox.

          ademmlerA 1 Reply Last reply
          2
          • ademmlerA ademmler

            @Pl45m4 said in Add item to combobox when it is "touched":

            Hi, thx for your feedback.

            @ademmler said in Add item to combobox when it is "touched":

            connect(ui->combo_Measurements, SIGNAL(aboutToShow()), this, SLOT(slotGetMeasurementsList()));

            What is this signal?
            IIRC aboutToShow() is a signal from QMenu. There is nothing like that in QComboBox.

            This was simply a try - because with menus for example it works and the compiler/Qt did not complained about this.

            he later is partially "working" but I cant select any entry then, because the actualisation takes permanently place

            Then don't choose the highlighted signal as trigger for your code.
            Put it in some extra function and call this function in mousePressEvent, for example.

            Or work with QComboBox ability to display model data. Create a model which holds your data and let the QComboBox view display it.

            • https://doc.qt.io/qt-6/qcombobox.html#model-view-framework

            Ok, thx for the hint. I actually try this and it seems to work:

            ui->combo_Measurements->installEventFilter(this);
            ....
            bool MyApp::eventFilter(QObject *f_object, QEvent *f_event){
               if(f_object == ui->combo_Measurements){
                   if(f_event->type() == QEvent::MouseButtonPress){
                       slotGetMeasurementsList(); 
                   }
                   return false;
               }
               return false;
            }
            
            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #5

            @ademmler said in Add item to combobox when it is "touched":

            This was simply a try - because with menus for example it works and the compiler/Qt did not complained about this.

            Haha, you can't just throw signals and classes together at random. It won't work like this ;-)
            I think you know that :)

            It doesn't complain, because the string based (with SIGNAL and SLOT) connection does not perform a check (signal + slot compatible, signal/slot valid, matching args, etc...) while compiling.
            Like @JonB said, do yourself a favor and stop using this type of connection ;-)
            With function pointer based connections, you can even connect to functions which are not explicitely declared Q_SLOT ... and you see right away whether your connection will work or not.

            Btw: QObject::connect(....) has a return value, that you can check or output with qDebug() for example.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            1
            • JonBJ JonB

              @ademmler said in Add item to combobox when it is "touched":

              This was simply a try - because with menus for example it works and the compiler/Qt did not complained about this.

              Do not ever use old-style signal/slot syntax. It was replaced over a decade ago.

              connect(ui->combo_Measurements, SIGNAL(aboutToShow()), this, SLOT(slotGetMeasurementsList()));
              # replace by
              connect(ui->combo_Measurements, &QMenu::aboutToShow, this, &ThisClass::slotGetMeasurementsList);
              

              If you did this you would see, and the compiler would complain, that QMenu::aboutToShow does not go with ui->combo_Measurements, which is presumably a QComboBox.

              ademmlerA Offline
              ademmlerA Offline
              ademmler
              wrote on last edited by ademmler
              #6

              @JonB

              thx for teaching me this - its very helpful to see old and new syntax side by side.

              However I try to use eventFilter instead. As you can see from my post above.
              Basically it works, but after a while (some mouse clicks in the UI) the event filter seems to be "vanished" or better to say does not work any longer.
              Any idea what's causing this or what I do wrong here?

              JonBJ 1 Reply Last reply
              0
              • ademmlerA ademmler

                @JonB

                thx for teaching me this - its very helpful to see old and new syntax side by side.

                However I try to use eventFilter instead. As you can see from my post above.
                Basically it works, but after a while (some mouse clicks in the UI) the event filter seems to be "vanished" or better to say does not work any longer.
                Any idea what's causing this or what I do wrong here?

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #7

                @ademmler Not other than put in some qDebug()s if you want to know what it is doing.

                ademmlerA 1 Reply Last reply
                0
                • JonBJ JonB

                  @ademmler Not other than put in some qDebug()s if you want to know what it is doing.

                  ademmlerA Offline
                  ademmlerA Offline
                  ademmler
                  wrote on last edited by
                  #8

                  @JonB

                  And I found a bug in my code. Hence the approach of using "installEventFilter" is the right way of doing.

                  Thx to all!

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Milly
                    wrote on last edited by
                    #9

                    I achieved something similar by subclassing QComboBox and adding the aboutToShow signal myself. I override the showPopup method so it emits the signal before showing the comboBox options.

                    1 Reply Last reply
                    0

                    • Login

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