Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]updating qtablewidget removes selection
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]updating qtablewidget removes selection

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    darwwin
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qxoz
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          darwwin
          wrote on last edited by
          #4

          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()));
          @

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qxoz
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • D Offline
              D Offline
              darwwin
              wrote on last edited by
              #6

              [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.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved