Important: Please read the Qt Code of Conduct -

QTablewidget ItemAt->text() ????

  • Hi everyone :D
    so.. i've written some kind of table-handling program there is [ add, remove, clear, move up-down, edit ] buttons.
    everything works except "edit" (load the text of the selected item to string):
    QString varaible1;
    QString varaible2;
    QString varaible3;
    I thougt that maybe the "SelectedItems()[ 0 ]" is the problem... I have SelectionBehaviour = SelectRows so this currently one single row and that method works fine in the move up-down slots:
    @if (ui->tableWidget->selectedItems().count()>1)
    for (int i=0; i<=5; i++)
    currentitem = ui->tableWidget->takeItem(ui->tableWidget->selectedItems()[0]->row(), i);
    itemabove = ui->tableWidget->takeItem(ui->tableWidget->selectedItems()[0]->row()-1, i);


    After i checked it with a debug, if i click on the Edit button, all variables from 1-3 gets the value of variable1 :S

    [EDIT: code fixup for [ 0 ] in text, Volker]

  • The problem is itemAt(). This method wants a coordinate in mouse pixels, not the row and column of a widget in the item.

    Either use the items of selectedItems() directly or use method item() of the table view. The latter takes a row and column as argument.

    The same goes for the takeItem().

    See the "QTableWidget": class reference for further details and an explanation of the arguments that the various methods take.

    PS: the first code snippet will not compile.

Log in to reply