Qtablewidget and QModelIndex

  • hello,

        QModelIndex index = ui->tableWidget->currentIndex();
        QString itemText = index.data(Qt::DisplayRole).toString();

    when i click an item on the tablewidget i want to show it in the tablewidget_show but it is not work how can i do this ?

  • 0_1542371008687_1e4b90af-fce4-4bc4-8004-2a240fc3129a-image.png

  • Lifetime Qt Champion

    What does debugger show?
    You could test with ui->tableWidget_show->setHorizontalHeaderLabels(QStringList()<<"Test");
    and see if that works. then you know you dont get right text.

  • if i do this it shows that but i dont want this i want to get selected item from tablewidget


  • Every time you want that "when you do something then something else happens" it means that you need a connect

    connect(ui->tableWidget->selectionModel(),&QItemSelectionModel::currentIndexChanged,[=](const QModelIndex& idx)->void{ui->tableWidget_show->model->setHeaderData(0,Qt::Horizontal,idx.data());});

  • thank you for help, i will keep in mind what you said

Log in to reply