Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Adding qtablewidget items to vector



  • Hello. I am trying to add the items of the last row only of my qtablewidget into a vector<int>. Thanks!



  • @Kiboshi awesome !

    You can post here if you have an issue with that



  • Below is my approach. But it does not get the last row of my qtablewidget. "Input_table" is the name of my qtablewidget.

    std::vector<int> tableoperatations::getdestination()
    {
    std::vector <int> destination;
    int row = this->input_table->rowCount()-1;
    for(int i=0;i<=this->input_table->columnCount();i++)
    {
    destination.push_back(this->input_table->item(row, i)->text().toInt());
    }
    return destination;
    }


  • Lifetime Qt Champion

    Hi,

    What row does it get ?



  • @Kiboshi said in Adding qtablewidget items to vector:

    for(int i=0;i<=this->input_table->columnCount();i++)
    destination.push_back(this->input_table->item(row, i)->text().toInt());

    I'm not saying it's your issue, but this does not look good. Does item(row, i) not error? i <= this->input_table->columnCount(), that <=?



  • This post is deleted!


  • Currently I am facing a problem where my destination vector is inacessible when I debug the program. However my "source" vector has successfully obtain only the last column.

    Below is my code:

    tableoperatations::tableoperatations(QWidget *parent, int a, int b) : QDialog(parent)
    {
    input_table = new QTableWidget;
    input_table->setRowCount(a+1);
    input_table->setColumnCount(b+1);
    for(int i=0;i<b;++i){
    input_table->setHorizontalHeaderItem(i, new QTableWidgetItem);
    Q_ASSUME(input_table->model()->setHeaderData(i,Qt::Horizontal,QStringLiteral("D%1").arg(i+1)));
    }
    for(int i=0;i<a;++i){
    input_table->setVerticalHeaderItem(i, new QTableWidgetItem);
    Q_ASSUME(input_table->model()->setHeaderData(i,Qt::Vertical,QStringLiteral("S%1").arg(i+1)));
    }
    for (int i = 0; i < input_table->rowCount(); i++){
    for (int j = 0; j < input_table->columnCount(); j++) {
    QTableWidgetItem *item = new
    QTableWidgetItem(tr("-"));
    input_table->setItem(i, j, item);
    }
    }
    input_table->setVerticalHeaderItem(a, new QTableWidgetItem);
    input_table->model()->setHeaderData(a,Qt::Vertical,QStringLiteral("Demand"));
    input_table->setHorizontalHeaderItem(b, new QTableWidgetItem);
    input_table->model()->setHeaderData(b,Qt::Horizontal,QStringLiteral("Supply"));
    input_table->removeCellWidget(a,b);
    calc_button = new QPushButton("Calculate", this);
    connect(calc_button, SIGNAL (clicked()),this, SLOT (calculate()));
    result=new QTextEdit("");
    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(input_table);
    layout->addWidget(calc_button);
    layout->addWidget(result);
    setLayout(layout);
    }

    std::vector<int> tableoperatations::getdestination()
    {
    std::vector <int> destination;
    int row = this->input_table->rowCount();
    for(int i=0;i<this->input_table->columnCount();i++)
    {
    destination.push_back(this->input_table->item(i, row)->text().toInt());
    }
    return destination;
    }

    std::vector<int> tableoperatations::getsource()
    {
    std::vector <int> source;
    int column = this->input_table->columnCount()-1;
    for(int j=0;j<=this->input_table->rowCount();j++)
    {
    source.push_back(this->input_table->item(j, column)->text().toInt());
    }
    return source;
    }



  • @Kiboshi said in Adding qtablewidget items to vector:

    int row = this->input_table->rowCount();
    for(int i=0;i<this->input_table->columnCount();i++)
    {
    destination.push_back(this->input_table->item(i, row)->text().toInt());

    Now row is out of range.

    for(int j=0;j<=this->input_table->rowCount();j++)
    {
    source.push_back(this->input_table->item(j, column)->text().toInt());

    Same for j.

    getdestination() uses a fixed row and varies the column. getsource() uses a varying row and a fixed column. Why is that?

    getdestination() uses row = rowCount() to access input_table->item(i, row), i.e. as a column index. Why is that?

    You really need to sort out your column & row indexes.



  • My bad. Now i have fixed it but my "destination" vector is not accessible still. Any help would be appreciated. Thanks

    tableoperatations::tableoperatations(QWidget *parent, int a, int b) : QDialog(parent)
    {
    input_table = new QTableWidget;
    input_table->setRowCount(a+1);
    input_table->setColumnCount(b+1);
    for(int i=0;i<b;++i){
    input_table->setHorizontalHeaderItem(i, new QTableWidgetItem);
    Q_ASSUME(input_table->model()->setHeaderData(i,Qt::Horizontal,QStringLiteral("D%1").arg(i+1)));
    }
    for(int i=0;i<a;++i){
    input_table->setVerticalHeaderItem(i, new QTableWidgetItem);
    Q_ASSUME(input_table->model()->setHeaderData(i,Qt::Vertical,QStringLiteral("S%1").arg(i+1)));
    }
    for (int i = 0; i < input_table->rowCount(); i++){
    for (int j = 0; j < input_table->columnCount(); j++) {
    QTableWidgetItem *item = new
    QTableWidgetItem(tr("-"));
    input_table->setItem(i, j, item);
    }
    }
    input_table->setVerticalHeaderItem(a, new QTableWidgetItem);
    input_table->model()->setHeaderData(a,Qt::Vertical,QStringLiteral("Demand"));
    input_table->setHorizontalHeaderItem(b, new QTableWidgetItem);
    input_table->model()->setHeaderData(b,Qt::Horizontal,QStringLiteral("Supply"));
    input_table->removeCellWidget(a,b);
    calc_button = new QPushButton("Calculate", this);
    connect(calc_button, SIGNAL (clicked()),this, SLOT (calculate()));
    result=new QTextEdit("");
    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(input_table);
    layout->addWidget(calc_button);
    layout->addWidget(result);
    setLayout(layout);
    }

    std::vector<std::vector<int>> tableoperatations::getcost()
    {
    std::vector<std::vector <int>> cost;
    for(int i=0;i<this->input_table->rowCount()-1;i++)
    {
    std::vector <int> row;
    for(int j=0;j<this->input_table->columnCount()-1;j++)
    {
    row.push_back(this->input_table->item(i, j)->text().toInt());
    }
    cost.push_back(row);
    }
    return cost;
    }

    std::vector<int> tableoperatations::getdestination()
    {
    std::vector <int> destination;
    int row = this->input_table->rowCount();
    for(int i=0;i<this->input_table->columnCount();i++)
    {
    destination.push_back(this->input_table->item(row,i)->text().toInt());
    }
    return destination;
    }

    std::vector<int> tableoperatations::getsource()
    {
    std::vector <int> source;
    int column = this->input_table->columnCount()-1;
    for(int j=0;j<=this->input_table->rowCount();j++)
    {
    source.push_back(this->input_table->item(j, column)->text().toInt());
    }
    return source;
    }



  • @Kiboshi
    You haven't fixed all the points, can't keep saying the same things. Get your loop limits correct. There's no point keep pasting whole code without addressing the issues.



  • @JonB In getdestination(), Im trying to get all the user input in the last row of my table. Meanwhile in getsource(), Im trying to get all the user input in the last column of the table.
    Screen Shot 2019-12-21 at 02.26.57.png

    In this case, vector destination would be[2,3,4,0] and vector source would be[2,5,3,0]



  • I was able to solve this problem. Thanks @SGaist @JonB @LeLev for your effort.



  • @Kiboshi

    int row = this->input_table->rowCount();
    for(int j=0;j<=this->input_table->rowCount();j++)

    I trust you altered at least the above two lines!


Log in to reply