开发者

Qt tablewidget add row pointer?

I have a program with ~12 tablewidgets, how can I set up a function to add rows to all of the 12 tables in one functi开发者_运维百科on?

i.e.

main{

    addRow(tablewidget1);
    addRow(tablewidget2);
    // etc
}

addRow(*tablewidget){

    int row = tablewidget->rowCount();

    tablewidget->insertRow(row);

    QTableWidgetItem *item0 = new QTableWidgetItem;
    item0->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
    tablewidget->setItem(row, 0, item0);

    QTableWidgetItem *item1 = new QTableWidgetItem;
    item0->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
    tablewidget->setItem(row, 1, item1);
}


If you have an array or a vector with all your widgets in, you could do

void main()
{
    std::vector<tablewidgets> widgets;

    // Some initialization code the widgets.

    addMyWidgets(widgets);
}

void addMyWidgets(std::vector<tablewidget>& widgets)
{
    for(std::vector<tablewidget>::iterator it = widgets.begin(), end = widgets.end();
        it != end;
        ++it)
    {
        addRow(*it);
    }
}


Needed to not use a reference pointer..

main{

addRow(tablewidget1);
addRow(tablewidget2);
// etc

}

addRow(QTableWidget*tablewidget){

int row = tablewidget->rowCount();

tablewidget->insertRow(row);

QTableWidgetItem *item0 = new QTableWidgetItem;
item0->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
tablewidget->setItem(row, 0, item0);

QTableWidgetItem *item1 = new QTableWidgetItem;
item0->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
tablewidget->setItem(row, 1, item1);

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜