Regarding QTableWidget



  • Hello
    i have a QtableWidget, my question is , how to get the entire row values, present in QtableWidget, when once clicked on the selected row.


  • Moderators

    @Pradeep-Kumar
    Iterate using columnCount and use QTableWidget::item to get the item. You already have the row index.



  • @p3c0

    using the QModelIndex we will get the items once we select the respective row in QTableWidget or QTableView.



  • Here is the sample code to get the values from QTableView, when clicked on to the particular row.

    connect(m_tableview,SIGNAL(clicked(QModelIndex)),this,SLOT(gettableValue(QModelIndex)),Qt::UniqueConnection);

    We have to use connect statement as shown above and the respective slots in the below code

    void TableViewExample::gettableValue(QModelIndex modelIndex)
    {
    int rowSelected = 0;
    int columnSelected = 0;

    rowSelected = modelIndex.row();
    
    QString firstData = modelIndex.sibling(rowSelected,columnSelected).data().toString();
    QString secondData = modelIndex.sibling(rowSelected,columnSelected+1).data().toString();
    
    qDebug () << "firstData" << firstData << endl;
    qDebug () << "secondData" << secondData << endl;
    

    }



  • @Pradeep-Kumar
    may be this?

            QString data = index.data().toString();


  • @Ratzz
    Thanks man

    Hi was trying to inform the above code which i provided works fine and which will be helpfull also. So i provided the code, which works for QTableView and QTableWidget. To get the data when clicked the respective row.
    Cheers.



  • You may need to create index of 2nd column

    void MainWindow::on_tableWidget_doubleClicked(const QModelIndex &index)
    {
         QString firstData = index.data().toString();
    
         QModelIndex colIndex= ui->tableWidget->model()->index(index.row(), index.column()+1,QModelIndex());
         QString secondData = colIndex.data().toString();
    
         qDebug() <<"firstData " <<firstData ;
         qDebug() <<"secondData " <<secondData;
    }


  • @Ratzz
    The solution helped.
    Thanks.

    I was trying to tell this also works.

    void TableWidget::getTableWidgetValues(QModelIndex modelIndex)
    {
    int row = 0;
    int column =0;

    row = modelIndex.row();
    
    QString firstData = modelIndex.sibling(row,column).data().toString();
    QString secondData = modelIndex.sibling(row,column+1).data().toString();
    
    qDebug () << "firstData" << firstData << endl;
    qDebug () << "secondData" << secondData << endl;
    

    }



  • I taught you are finding a way to get values!



  • I got the solution , so i taught of providing the code.
    Thanks for your kind reply.

    I tried the code provided by you @Ratzz also, which worked.

    Thanks,



  • The above code which i provided , will work for both QTableView and QTableWidget.


Log in to reply
 

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