[SOLVED]updating qtablewidget removes selection



  • Hello All,

    I'm sure I'm doing something wrong here. I've a qtablewidget table populated from a sqlite database. I've created a loop to update the gui every second(using signal/slot) with new info from the sql database.

    Update slot looks like this. Basically I'm recreating the entire table every second I think. I couldn't find a better way.

    @ui->MyWidget->insertRow(r);
    ui->MyWidget->setItem(r,0,new QTableWidgetItem(query.value(0).toString()));@

    Anyway, when the user clicks on a cell whole row is selected by design but everytime the GUI is updated the selection is lost. What is the correct way to deal with this situation? I'd appreciate any help.



  • Hi darwwin.
    First solution which comes to my mind, makes minimal changes to your code:
    @QModelIndex tempInd = ui->MyWidget->currentIndex();
    //update data
    ui->MyWidget->setCurrentIndex(tempInd);@
    But another question is why you need refresh it so often and what kind of changes can happen, can row count change?



  • By the way, try to use a QTableView instead QTableWidget.



  • Hi qxoz,

    Thanks for answering. I'm updating the sql database from another thread and sometimes from another application so the sql data needs to be updated fairly regularly. We are talking about 20-30 rows for each table at most so I don't think it's a big deal. But just wanted to be sure if that was the right way to update the GUI.

    I've used a similar albeit more hackish approach to the selection problem. Now the selection is kept during updates however it seems selection does not equal to a click.

    When I select a row on the table with a mouse click "clicked" signal is emitted(that does another bunch of stuff) however, when I select the row programmatically that signal is not emitted.

    I've tried the following:

    @ui->MyWidget->selectRow(row);
    ui->MyWidget->setCurrentIndex(tempInd);
    ui->MyWidget->selectionModel()->select(tempInd,QItemSelectionModel::ClearAndSelect|QItemSelectionModel::Rows);@

    Is there a way to emit a signal on selection? Or programmatically click a row?

    I've tried these but they don't work:

    @connect(ui->MyWidget, SIGNAL(activated(const QModelIndex&)),
    this, SLOT(MyFunction()));
    connect(
    ui->MyWidget->selectionModel(),
    SIGNAL(itemSelectionChanged()),
    this, SLOT(MyFunction()));
    connect(
    ui->MyWidget->selectionModel(),
    SIGNAL(SelectionChanged()),
    this, SLOT(MyFunction()));
    @



  • Try:
    @connect(ui->MyWidget->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),this, SLOT(on_currentRowChanged(QModelIndex,QModelIndex)));@



  • [quote author="qxoz" date="1374130241"]Try:
    @connect(ui->MyWidget->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),this, SLOT(on_currentRowChanged(QModelIndex,QModelIndex)));@[/quote]

    Thanks a lot qxoz. This one didn't work but I managed to solve the problem by rearranging some stuff.


Log in to reply
 

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