Search code examples
qtcellqtablewidgetqmapqtablewidgetitem

Get The Row,Column values from a cell in QT


I have a problem, i want to return the selected Rows values, and the columns separately, i found a method to return both of them using the function cell(row, column), but i want to get them separately

Here is my code :

QTableWidgetItem *c = new QTableWidgetItem();
QMap<QString,int> lists;
for(i=0;i<range.rowCount();++i){
    for(int j=0;j<range.columnCount();++j){
        c=item(i,j);// here i can return the Rows, Columns Data
        QMessageBox::information(this,"",c->text());            
    }
}

As you can see this code is working, but i just want to return the Rows and the Columns separately so i can put them in my QMap<QString,int> list.

And the purpose of all this is to try to draw a piechart from the selected rows and columns

So Any help please


Solution

  • Here is what I understood from the comments, feel free to correct me and I'll update my answer if necessary.

    enter image description here

    COL1 | COL2

    NAME | VALUE

    So when you select a cell, you actually care about the whole row, a.k.a the name of the row and the value associated. If this is the case, it would make more sense to only allow the user to select whole rows, instead of cells. setSelectionBehavior(QAbstractItemView::SelectRows); should do the trick.

    Provided that the name of the dataset is always in column 1, and the value in column 2, you should update your code with the snippet:

    QTableWidgetItem *c; //Deleted memory leak in your code.
    QMap<QString,double> myMap; //Don't name it a list if it is explicitly a map.
    for(i=0;i<range.rowCount();++i){
        QString dataName = item(i,0)->text();
        int     dataValue;
        for(int j=1;j<range.columnCount();++j){
            c=item(i,j);// here i can return the Rows, Columns Data
            dataValue += c->text().toDouble(); 
            //If you always have 2 columns only, dataValue will be the value you are looking for. 
            //If you can have more than 2 columns, dataValue will be the sum of all the cells located after the column 0, on the same row.
            //Change this depending on how you want to treat those values.
            QMessageBox::information(this,dataName,c->text());            
        }
        myMap[dataName]=dataValue;
    }
    

    EDIT for QPieSeries, following this example:

    QPieSeries *series = new QPieSeries();
    QMap<QString,double>::iterator it = myMap.begin();
    QMap<QString,double>::iterator end = myMap.end();
    for(; it!=end; ++it){
        series->append(it->key(), it->value());
    }
    
    QPieSlice *slice = series->slices().at(1);
    slice->setExploded();
    slice->setLabelVisible();
    slice->setPen(QPen(Qt::darkGreen, 2));
    slice->setBrush(Qt::green);
    
    QChart *chart = new QChart();
    chart->addSeries(series);
    chart->setTitle("My Data");
    chart->legend()->hide();
    
    QChartView *chartView = new QChartView(chart);
    chartView->setRenderHint(QPainter::Antialiasing);
    
    /*change with your window here*/
    yourWindow.setCentralWidget(chartView);