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;
    varaible1=TableWidget->ItemAt(TableWidget->SelectedItems()[0]->row(),0,).text();
    QString varaible2;
    varaible1=TableWidget->ItemAt(TableWidget->SelectedItems()[0]->row(),1,).text();
    QString varaible3;
    varaible1=TableWidget->ItemAt(TableWidget->SelectedItems()[0]->row(),2,).text();
    @
    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);
    ui->tableWidget->setItem(ui->tableWidget->selectedItems()[0]->row(),i,itemabove);
    ui->tableWidget->setItem(ui->tableWidget->selectedItems()[0]->row()-1,i,currentitem);
    }
    ui->tableWidget->setCurrentItem(currentitem);

    }@
    

    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":http://doc.qt.nokia.com/4.7/qtablewidget.html 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
 

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