[SOLVED]not getting the right information from QTableView



  • hi everyone when i execute this code
    @void ExportData::on_agregar_clicked()
    {
    QModelIndexList rows_selected = this->ui->table_busqueda->selectionModel ()->selectedRows ();

    foreach (QModelIndex index, rows_selected){
        int row = this->ui->table_agregados->rowCount ();
        this->ui->table_agregados->insertRow (row);
        QTableWidgetItem *copia = new QTableWidgetItem (this->ui->table_busqueda->model ()->data (index).toString ());
        this->ui->table_agregados->setItem (row, index.column (), copia);
        QPoint punto_precio = QPoint (index.row (), 1+index.column ());
        QModelIndex i_precio = QModelIndex (this->ui->table_busqueda->indexAt (punto_precio));
        QTableWidgetItem *precio = new QTableWidgetItem (this->ui->table_busqueda->model ()->data (i_precio).toString ());
        qDebug () << this->ui->table_busqueda->model ()->data (this->ui->table_busqueda->indexAt (QPoint (index.row (), index.column ()+1))).toInt ();
        qDebug () << this->ui->table_busqueda->indexAt (punto_precio).column ();
        this->ui->table_agregados->setItem (row, index.column ()+1, precio);
    }
    

    }@

    both QTableWidgetItems have the same data because the index is the same even though i change the column ()+1....seems that when i execute indexAt i recieve the index (row, 0) when i should be receiving (row, 1)...and i dont know how to solve it.
    The idea is just to copy field1, field2 from the selected row to anothe table i have in the same widget...

    apologies for my english...

    greetings!

    SOLUTION: the problem was at IndexAt () read the documentation but still dont know why...anyways changed to:

    @void ExportData::on_agregar_clicked()
    {
    QModelIndexList rows_selected = this->ui->table_busqueda->selectionModel ()->selectedRows ();
    foreach (QModelIndex index, rows_selected){
    int row = this->ui->table_agregados->rowCount ();
    this->ui->table_agregados->insertRow (row);
    QTableWidgetItem *copia = new QTableWidgetItem (this->ui->table_busqueda->model ()->data (index).toString ());
    this->ui->table_agregados->setItem (row, index.column (), copia);
    QTableWidgetItem *precio = new QTableWidgetItem (this->ui->table_busqueda->model ()->data (this->ui->table_busqueda->model ()->index (index.row (), index.column ()+1)).toString ());
    this->ui->table_agregados->setItem (row, index.column ()+1, precio);
    double precio_caja = precio->text ().toDouble () * 6;
    QString precio_string;
    precio_string.setNum (precio_caja, 'g', 6);
    this->ui->table_agregados->setItem (row, index.column ()+2, new QTableWidgetItem (precio_string));
    }
    }@

    and works just fine! hope it serves someone


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.