Loop creation of widgets in a tablewidget? [solved]



  • 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.



  • 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?



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



  • [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



  • 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)



  • 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.



  • 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)?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.