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. Loop creation of widgets in a tablewidget? [solved]
Forum Updated to NodeBB v4.3 + New Features

Loop creation of widgets in a tablewidget? [solved]

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.1k Views 1 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.
  • I Offline
    I Offline
    itsmemax
    wrote on last edited by
    #1

    Hi, I've got 64 rows and 3 columns in a QTableWidget. I'm trying to add QComboBoxes to all of the 64 rows (in the third column). Here's my code for 1 row:

    @QComboBox *tablecombo1 = new QComboBox();
    ui->tableWidget->setCellWidget(1,3,tablecombo1);@

    It works fine. But how can I loop this code, so that in all of the 64 rows are these ComboBoxes inserted (difficult to explain with my English)? I don't want to copy, paste and edit this 64 times.

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

      Hi,

      try with this

      @
      const int ROWS = 64;

      QComboBox *combos[ROWS];

      for (int r = 0; r < ROWS; ++r) {
      combos[r] = new QComboBox;
      ui->tableWidget->setCellWidget (r + 1, 3, combos[r]);
      }
      @

      QUESTION Are you sure rows and columns are 1 based?

      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
      • I Offline
        I Offline
        itsmemax
        wrote on last edited by
        #3

        Nothing happens when I try to debug (also no error, just nothing happens).. :S

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

          [quote author="itsmemax" date="1368453665"]Nothing happens when I try to debug (also no error, just nothing happens).. :S[/quote]

          What means?
          My Code is an example, post yours please

          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
          • I Offline
            I Offline
            itsmemax
            wrote on last edited by
            #5

            Here's my result, thanks mcosta:
            @//Comboboxen laden
            //Start

            const int ROWS = 64;
            
            QComboBox *combos[ROWS];
            
            for (int r = 0; r < UisList.listSize(); ++r) {
                AllStrings << UisList.getStringById(r);
            }
            
            for (int r = 0; r < ROWS; ++r) {
                combos[r] = new QComboBox;
                ui->tableWidget_5->setCellWidget (r, 2, combos[r]);
                combos[r]->addItems(AllStrings);
            
            }
            
            for (int r = 0; r < ROWS; ++r) {
                combos[r] = new QComboBox;
                ui->tableWidget_6->setCellWidget (r, 2, combos[r]);
                combos[r]->addItems(AllStrings);
            
            }
            
            for (int r = 0; r < ROWS; ++r) {
                combos[r] = new QComboBox;
                ui->tableWidget_7->setCellWidget (r, 2, combos[r]);
                combos[r]->addItems(AllStrings);
            
            }
            
            for (int r = 0; r < ROWS; ++r) {
                combos[r] = new QComboBox;
                ui->tableWidget_8->setCellWidget (r, 2, combos[r]);
                combos[r]->addItems(AllStrings);
            
            }
            
            //End@
            

            (I've got 4 tablewidgets, don't wonder)

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

              Hi,

              your code has a BIG problem.
              In each for loop you assign to combos[r] to new value; the old one cannot be accessible.

              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
              • jazzycamelJ Offline
                jazzycamelJ Offline
                jazzycamel
                wrote on last edited by
                #7

                First off, mcosta is right:

                [quote author="mcosta" date="1368615813"]Hi,

                your code has a BIG problem.
                In each for loop you assign to combos[ r ] to new value; the old one cannot be accessible.
                [/quote]

                If you want to do this you'll need an array for each table, or a multi-dimensional array.

                Second, why are you storing a reference to each QComboBox in an array when you already have a reference to each one by simply calling QTableWidget::cellWidget(row, col)?

                For the avoidance of doubt:

                1. All my code samples (C++ or Python) are tested before posting
                2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
                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