Tianxiang Xiong
Tianxiang Xiong

Reputation: 4081

Modifying QWidget before putting it in QTableWidget

In my QT UI, I need to create a QTableWidget in which the user can add a double to each cell.

The table has four columns: the first column is a value from 0-255, the second column to fourth columns are RGB values respectively, each from 0-1.0.

void MainWindow::InitializeColorTable(){
        QTableWidget *tableColor = ui->tableColor;
        tableColor->setRowCount(4);
        tableColor->setColumnCount(4);

    tableColor->setHorizontalHeaderLabels(QStringList() << 
        tr("Value") << tr("R") << tr("G") << tr("B"));
    for (int row = 0; row < tableColor->rowCount(); ++row){
        tableColor->setCellWidget(row, 0, new QDoubleSpinBox(tableColor));
        tableColor->setCellWidget(row, 1, new QDoubleSpinBox(tableColor));
        tableColor->setCellWidget(row, 2, new QDoubleSpinBox(tableColor));
        tableColor->setCellWidget(row, 3, new QDoubleSpinBox(tableColor));
    }
}

I want to modify the properties of my QDoubleSpinBox objects, such as set initial values and defining ranges. However, I'm not sure how to do this. If I create QDoubleSpinBox objects like

QDoubleSpinBox *box = new QDoubleSpinBox;
box->setValue(0);
box->setRange(0, 255);
tableColor->setCellWidget(row, 0, box);

in my InitializeColorTable function, the box variable goes out of scope when the function returns. What is a good way to solve this problem?

Upvotes: 0

Views: 287

Answers (1)

AnatolyS
AnatolyS

Reputation: 4319

Why do you decide that the box (QDoubleSpinBox *box = new QDoubleSpinBox;) goes our of scope? This is dynamic object which will become child of tableColor after setCellWidget (only pointer for this object goes out of scope). So do not warry.

Upvotes: 1

Related Questions