0

I'm creating a new instance of QTableWidgetItem for each row the user may add, and adding it to a QVector of QTableWidgetItems.

I'd like to do soemthing like the following to name each instance in the following iteration with the row number included in the instance name:

 QVector<QCheckBox> *checkBox_array;

  for(int r=0;r<user_input;r++)
  {
      ui->tableWidget->insertRow(r);        
      *checkBox%1.arg(r) = new QCheckBox;   //create an instance "checkBox1" here
      checkBox_array->pushBack(checkBox%1.arg(r))            
  }

or something like the following, which does not compile in its current state:

 for(int r=0;r<7;r++)
{
  ui->tableWidget->insertRow(r);


  checkBox_array->push_back();
  checkBox_array[r] = new QCheckBox;
  ui->tableWidget->setCellWidget(r,2,checkBox_array[r]);

}

is this possible? How can I work around this issue? All I need is to get the new widgets into the array without having to name them explicitly. Thanks in advance!

Thanks in advance.

Rachael
  • 1,965
  • 4
  • 29
  • 55
  • 1
    I never store variables for these. I just add them to the QTableWidget and connect the signals / slots when I create the objects. Qt will handle the deletion so no leaked memory. – drescherjm Sep 15 '14 at 17:16
  • What does this mean : *checkBox%1 ? – galinette Sep 15 '14 at 17:17
  • which doesn't help me....I have an n-dimensional QTableWidget to populate, as you read. – Rachael Sep 15 '14 at 17:18
  • I have had the same still no need to create any variables you need to make use of the QTableWidget to get and set the values of the checkboxes. – drescherjm Sep 15 '14 at 17:20
  • @galinette, that was a makeshift attempt to replace part of the string with an argument value, as is commonly done for QStrings (ex: QString game%1.arg(s) is "games") – Rachael Sep 15 '14 at 17:22
  • @drescherjm, could you please provide an example? I need a checkbox for each widget, and I need to be able to retrieve the vablue of that checkbox widget for each row. thank you – Rachael Sep 15 '14 at 17:23
  • @Chernobyl did exactly as I was thinking. – drescherjm Sep 15 '14 at 18:06

1 Answers1

3

Try something like this:

for(int r=0;r<7;r++)
{
 ui->tableWidget->insertRow(r);
 ui->tableWidget->setCellWidget(r,2,new QCheckBox(QString("checkBox%1").arg(r)));
}

It creates some widgets.

When you want change something in this widget or get data then use cellWidget() method, but don't forget cast it! For example:

for(int r=0;r<7;r++)
{
 QCheckBox* curBox = qobject_cast<QCheckBox*>(ui->tableWidget->cellWidget(r,2));
 if(curBox)
 {
    qDebug() << curBox->text() << curBox->isChecked();
    curBox->setText("This is new Text");
 }
 else
     qDebug() << "fail";
}
Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • Correct. Not every widget needs a C++ name. – MSalters Sep 15 '14 at 17:57
  • @Chernobyl (and everyone): thank you for the example. It makes perfect sense. Also thanks for including an qobject_cast, which I also ended up needing. Thanks again. – Rachael Sep 16 '14 at 13:36