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. [SOLVED] How to initialize a bunch of similar comboBoxes at once in a loop?

[SOLVED] How to initialize a bunch of similar comboBoxes at once in a loop?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.2k 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.
  • D Offline
    D Offline
    devjb
    wrote on last edited by
    #1

    Hi,

    I am wondering if there is a simple way to initialize combo boxes which are all of the same content at once in a loop using:

    @ui->ComboBoxXYZ->addItem("somedescription", someinteger);@

    !http://jochenbauer.net/publicpics/comboExample.png!

    The combo boxes in each line simlpy should contain the same entries.

    I am an absolute qt-newbie and would appreciate any hint on this.

    Thanks in advance

    Regards

    Jochen

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      if you use a nameing schema for your combo boxes, you can use findChildren with QRegularExpression to achieve your result.

      For example in my ui are defined several QComboBox named "comboBox_1", "comboBox_2" and so on.
      This code
      @
      Widget::Widget(QWidget *parent) :
      QWidget(parent),
      ui(new Ui::Widget)
      {
      ui->setupUi(this);

      QList<QComboBox*> combos = findChildren<QComboBox*>( QRegular[removed] "comboBox_.*"));
      QStringList comboItems;
      comboItems << "Foo" << "Bar";

      Q_FOREACH (QComboBox* combo, combos) {
      combo->addItems(comboItems);
      }
      }
      @
      add "Foo" and "Bar" to all of them

      P.S. I don't know why the string QRegularExpression is shown as QRegular[removed]

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leon.anavi
        wrote on last edited by
        #3

        You can also consider using a layout such as "QGridLayout":http://qt-project.org/doc/qt-5.0/qtwidgets/qgridlayout.html and to add the combo boxes using method "addWidget":http://qt-project.org/doc/qt-5.0/qtwidgets/qgridlayout.html#addWidget

        http://anavi.org/

        1 Reply Last reply
        0
        • D Offline
          D Offline
          devjb
          wrote on last edited by
          #4

          mcosta,

          thank you for your suggestion. It is a very nice solution. I just had to change some little things to deal with the item data. I also changed it to work with QT4.

          This is my code now:

          @//Fill comboboxes for area and formats with data

            QList<QComboBox*> comboBoxesArea = findChildren<QComboBox*>(QRegExp("comboBoxArea_.*"));
            QList<QComboBox*> comboBoxesFormat = findChildren<QComboBox*>(QRegExp("comboBoxFormat_.*"));
            QStringList comboItemsArea;
            QStringList comboItemsFormat;
            QList<int> comboValuesArea;
            QList<int> comboValuesFormat;
          
            comboItemsArea << " " << "E" << "A" << "M" << "DB" << "Z" << "T" << "IEC_Z" << "IEC_T";
            comboItemsFormat << " " << "BOOL" << "BYTE" << "WORD" << "DWORD" << "INT" << "DINT" << "REAL";
            comboValuesArea << 0x0 << daveInputs << daveOutputs << daveFlags << daveDB << daveCounter << daveTimer << daveCounter200 << daveTimer200;
            comboValuesFormat << 0x0 << AnzFormatBool << AnzFormatHexadezimal << AnzFormatHexadezimal << AnzFormatHexadezimal << AnzFormatDezimal << AnzFormatDezimal << AnzFormatGleitpunkt;
          
            int areaItemsCount = 0;
            int formatItemsCount = 0;
          
            Q_FOREACH(QComboBox* areaBox,comboBoxesArea)
            {
                areaBox->addItems(comboItemsArea);
                areaBox->itemData(areaItemsCount,comboValuesArea.value(areaItemsCount));
                areaItemsCount++;
            }
            Q_FOREACH (QComboBox* formatBox,comboBoxesFormat) {
                formatBox->addItems(comboItemsFormat);
                formatBox->itemData(formatItemsCount,comboValuesFormat.value(formatItemsCount));
                formatItemsCount++;
            }@
          
          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