[SOLVED] QTableWidget

  • Hello, I am trying to read a string from QTableWidget (current row) when button in table is clicked... So when I click PushButton in row 1, it should print string from one column...

    Problem is, when I press on the PushButtton, it does not take index of current row... index does not change at all... Buttons are not connected with QTableWidget at all...

    I ran application and printed current index... When I ran app and press button, current index is -1... It should inherit index of row in which button is located... I can click on any button and index won't change... I will change only if I click on any item in QTable...

    This code works, but does not do what I want...

    1. I must press on item first, to obtain current index and print string at all...
    2. If I click on item in row 1 and press button in row 3, it will show me string from row 1 (because index is set to row 1)...
      @int row = ui->tableWidget->currentRow();
      QString str = ui->tableWidget->item(row, 3)->text();
      qDebug() << str;@

    How to get current row of a clicked button??? It seems like button and other parts in that row are not connected... Even when I click to any item, all items in that row highlight, but not also button...

    @ int row = ui->tableWidget->rowCount();
    QTableWidgetItem *name = new QTableWidgetItem("Name");
    QTableWidgetItem *time = new QTableWidgetItem("Time");
    QTableWidgetItem *duration = new QTableWidgetItem("Duration");
    QTableWidgetItem *location = new QTableWidgetItem("Location");
    QTableWidgetItem *size = new QTableWidgetItem("Size");
    btn = new QPushButton("PushButton");
    ui->tableWidget->setCellWidget(row, 5, btn);
    ui->tableWidget->setItem(row, 0, name);
    ui->tableWidget->setItem(row, 1, time);
    ui->tableWidget->setItem(row, 2, duration);
    ui->tableWidget->setItem(row, 3, location);
    ui->tableWidget->setItem(row, 4, size);@


  • I found solution...

    @int row;
    int col;

    for(row=0; row < ui->tableWidget->rowCount(); row++){
      for(col=0; col < ui->tableWidget->columnCount(); col++){
          if(sender() == ui->tableWidget->cellWidget(row,col)){
           QString str = ui->tableWidget->item(row, 3)->text();
           qDebug() << str;


Log in to reply

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