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. TableView not updated or refreshed unless column is resized and scroll bar move up and down few times
QtWS25 Last Chance

TableView not updated or refreshed unless column is resized and scroll bar move up and down few times

Scheduled Pinned Locked Moved Solved General and Desktop
proxy modelsort filterqsqltablemodel
6 Posts 2 Posters 956 Views
  • 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.
  • VikramSamyV Offline
    VikramSamyV Offline
    VikramSamy
    wrote on last edited by
    #1

    In my QT applications, I have database connection and I'm using Microsoft SQL 2014. I have
    tried to implement model view programming method and I can say it almost working with some issues that I need to fixed, below is the code snippets:-

    //create the SQL table model based using the database connections
     TableModel = new QSqlTableModel(this,dbcon3);
     TableModel->setTable(tablename);
     TableModel->setEditStrategy(QSqlTableModel::OnManualSubmit); //need check and choose 
     TableModel->select(); //populates the model with data from the table 
    
     TableModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
     TableModel->setHeaderData(1, Qt::Horizontal, QObject::tr("EventDateTime"));
     TableModel->setHeaderData(2, Qt::Horizontal, QObject::tr("EventInfo"));
     TableModel->setHeaderData(3, Qt::Horizontal, QObject::tr("EventMsg"));
     TableModel->setHeaderData(4, Qt::Horizontal,QObject::tr("StationName"));
     qDebug()<<TableModel->lastError().text();
    
       //start create instance of the south-north proxy model
       HistoryProxyModel = new MySortFilterProxyModel(this); 
       HistoryProxyModel->setDynamicSortFilter(false); 
       HistoryProxyModel->sort(1,Qt::DescendingOrder); // so that last data inserted is displayed at TOP
       HistoryProxyModel->setSourceModel(TableModel);
    
       //create the proxy view
         HistoryView = new QTableView();
         HistoryView->setModel(HistoryProxyModel);
         HistoryView->setAlternatingRowColors(true);
         HistoryView->setEditTriggers(QAbstractItemView::NoEditTriggers);
         HistoryView->AdjustToContents;
    

    As the above code flow, I first created a QSqlTableModel @TableModel using the database connections Using that TableModel as the source model, I then created another QSortfilterProxyModel , all the codes are mainly took from the QT examples and then use in my application. Next I create a TableView using that sortfilterproxy model.

    so the flow is as below:-

    Microsoft SQL Database <----> QSqlTableModel --->QSortFilterProxyModel--->TableView

    So I have a database connected to a QSqlTableModel, which in turn is connected to a QSortFilterProxyModel which in turn is connected to a QTableView.

    Meanwhile I have Data-processing object which generate a SIGNAL each time a new data packet is received which connects to SLOT that inserts the new data into the QSqlTableModel (TableModel) . Once a new data arrived, a signal is generated, then the slot function to write to the TableModel is called, and by using "setData" I write/insert the data into the TableModel.

    I write/insert data to the QSqlTableModel@TableModel as below (code snippets):-

    if(dbcon3.isOpen()) //database connection is open
        {
            int row = 0;
         
                 if(!(TableModel->insertRows(row,1)))
                 {
                      qDebug()<<TableModel->lastError();
                 }
                 else
                 {
                 //-------------------------------------------------------------------
      TableModel->setData( TableModel->index(row, 1), serverDateTime);
      emit TableModel->dataChanged(TableModel->index(row, 1), TableModel->index(row, 1));
    
      TableModel->setData( TableModel->index(row, 2), Trainbound);
      emit TableModel->dataChanged(TableModel->index(row, 2), TableModel->index(row, 2));
    
      TableModel->setData( TableModel->index(row, 3), TableMessagesNOT[index]);
      emit TableModel->dataChanged(TableModel->index(row, 3), TableModel->index(row, 3));
    
      TableModel->setData( TableModel->index(row, 4), ThisStationName);
      emit TableModel->dataChanged(TableModel->index(row, 4), TableModel->index(row, 4));
    
      TableModel->submitAll();
                 }
    
    

    The data is properly written into the SQL database accordingly as new data arrives, the database is always updated correctly based on the data inserted into the TableModel and I can verify this by checking the Microsoft SQL database.

    As mentioned before the data is properly updated to microsoft SQL database, all rows are properly inserted/written and all event captured accordingly. All the data that updated to the TableModel will then get filtered according to some parameters (DATE/ event type) before being displayed on the TableView, and for this I use the QSortFilterProxyModel in-between the TableModel and the View.

    The new data is inserted to the QSqltableModel (TableModel) only and I expect the database will be updated and the table view also will be updated accordingly based on the filtering parameters of the proxy model, as the view is connected to the proxy model.

    The issue here is, each time a new row is inserted into the TableModel, the SQL Database is updated correctly but the TableView becomes blank/cleared and if I adjust the column size or scroll the view, then only it gets updated correctly. I need to scroll the view, or I can say scroll the view bar few times, then the Table View gets updated according to the proxy model sort-filter parameters which was selected beforehand, and if any continues new data is inserted to the Table Model again on the next round, the VIEW will become blank/cleared again, and then I need manually clicked on the view columns or rows, e.g. if I just resize the column few times, then the view suddenly gets updated to the latest data.

    I think the proxy-model only updates itself to the Table View if I click and move the scroll bar or resize the column, other than that it not refreshing automatically when each time a new data is inserted into the TableModel. But once I just start to resized any of the column and then play with scroll bar going up down up down ,up down, the all data gets updated in the view correctly. I also try adding the data changed signal as I show in the code snippets above, but still same.. any help or guide on this issue??

      TableModel->setData( TableModel->index(row, 1), serverDateTime);
      emit TableModel->dataChanged(TableModel->index(row, 1), TableModel->index(row, 1));
    
    

    with the emit datachanged code added or without that code ,it is still the same.

    JonBJ 1 Reply Last reply
    0
    • JonBJ JonB

      @VikramSamy
      I don't know, but a couple of observations:

      • You are using a QSqlTableModel, so it should be emitting all the dataChanged() and rowsInnserted() signals itself. I would remove all your emits.

      • HistoryProxyModel->setDynamicSortFilter(false);: try setting this to true, any difference?

      • If you remove your proxy model temporarily does everything work?

      • Try starting from a Qt example with model + proxy model, does that work?

      VikramSamyV Offline
      VikramSamyV Offline
      VikramSamy
      wrote on last edited by VikramSamy
      #6

      @JonB said in TableView not updated or refreshed unless column is resized and scroll bar move up and down few times:

      @VikramSamy
      I don't know, but a couple of observations:

      • You are using a QSqlTableModel, so it should be emitting all the dataChanged() and rowsInnserted() signals itself. I would remove all your emits.
        YES REMOVED

      • HistoryProxyModel->setDynamicSortFilter(false);: try setting this to true, any difference?
        NO, I need to disable the-sorting mechanism in the proxy model and do the sorting in the SQL Table model itself before I initialize table-model as the source-model for the Qsortproxymodel

      • If you remove your proxy model temporarily does everything work?
        Ya the the VIEW is updated properly if proxy model is removed.

      • Try starting from a Qt example with model + proxy model, does that work? YES I DID

      After a long gap, i just started to check this issue again,
      the previous behavior of the Table View was due to the flow of my code and it is because descending order SORTING mechanism enabled in the proxy-model, this caused the behavior as in 1st post. T

      //this was the issue
      HistoryProxyModel->setDynamicSortFilter(true);        HistoryProxyModel->sort(1,Qt::DescendingOrder);
      

      As I wanted to show the last row as first row in the Table View, I enabled the DescendingOrder sorting in the proxy model first and then apply filtering, and this was the issue .

      but then I enabled the descending Order sorting on the SQL Table Model and Disabled it on the proxy model,
      now all works fine, The proxy model just do the filtering's only. i added the code below on my Qsql Table Model.

      //this was the solution - sort the Table model 
       TableModel->setSort(1,Qt::DescendingOrder);
      

      and so i removed the sorting codes at the proxy model .
      I removed the below code:-

      //removed from proxy codes
       //HistoryProxyModel->sort(1,Qt::DescendingOrder)
      

      and now so far all works fine....

      1 Reply Last reply
      0
      • VikramSamyV VikramSamy

        In my QT applications, I have database connection and I'm using Microsoft SQL 2014. I have
        tried to implement model view programming method and I can say it almost working with some issues that I need to fixed, below is the code snippets:-

        //create the SQL table model based using the database connections
         TableModel = new QSqlTableModel(this,dbcon3);
         TableModel->setTable(tablename);
         TableModel->setEditStrategy(QSqlTableModel::OnManualSubmit); //need check and choose 
         TableModel->select(); //populates the model with data from the table 
        
         TableModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
         TableModel->setHeaderData(1, Qt::Horizontal, QObject::tr("EventDateTime"));
         TableModel->setHeaderData(2, Qt::Horizontal, QObject::tr("EventInfo"));
         TableModel->setHeaderData(3, Qt::Horizontal, QObject::tr("EventMsg"));
         TableModel->setHeaderData(4, Qt::Horizontal,QObject::tr("StationName"));
         qDebug()<<TableModel->lastError().text();
        
           //start create instance of the south-north proxy model
           HistoryProxyModel = new MySortFilterProxyModel(this); 
           HistoryProxyModel->setDynamicSortFilter(false); 
           HistoryProxyModel->sort(1,Qt::DescendingOrder); // so that last data inserted is displayed at TOP
           HistoryProxyModel->setSourceModel(TableModel);
        
           //create the proxy view
             HistoryView = new QTableView();
             HistoryView->setModel(HistoryProxyModel);
             HistoryView->setAlternatingRowColors(true);
             HistoryView->setEditTriggers(QAbstractItemView::NoEditTriggers);
             HistoryView->AdjustToContents;
        

        As the above code flow, I first created a QSqlTableModel @TableModel using the database connections Using that TableModel as the source model, I then created another QSortfilterProxyModel , all the codes are mainly took from the QT examples and then use in my application. Next I create a TableView using that sortfilterproxy model.

        so the flow is as below:-

        Microsoft SQL Database <----> QSqlTableModel --->QSortFilterProxyModel--->TableView

        So I have a database connected to a QSqlTableModel, which in turn is connected to a QSortFilterProxyModel which in turn is connected to a QTableView.

        Meanwhile I have Data-processing object which generate a SIGNAL each time a new data packet is received which connects to SLOT that inserts the new data into the QSqlTableModel (TableModel) . Once a new data arrived, a signal is generated, then the slot function to write to the TableModel is called, and by using "setData" I write/insert the data into the TableModel.

        I write/insert data to the QSqlTableModel@TableModel as below (code snippets):-

        if(dbcon3.isOpen()) //database connection is open
            {
                int row = 0;
             
                     if(!(TableModel->insertRows(row,1)))
                     {
                          qDebug()<<TableModel->lastError();
                     }
                     else
                     {
                     //-------------------------------------------------------------------
          TableModel->setData( TableModel->index(row, 1), serverDateTime);
          emit TableModel->dataChanged(TableModel->index(row, 1), TableModel->index(row, 1));
        
          TableModel->setData( TableModel->index(row, 2), Trainbound);
          emit TableModel->dataChanged(TableModel->index(row, 2), TableModel->index(row, 2));
        
          TableModel->setData( TableModel->index(row, 3), TableMessagesNOT[index]);
          emit TableModel->dataChanged(TableModel->index(row, 3), TableModel->index(row, 3));
        
          TableModel->setData( TableModel->index(row, 4), ThisStationName);
          emit TableModel->dataChanged(TableModel->index(row, 4), TableModel->index(row, 4));
        
          TableModel->submitAll();
                     }
        
        

        The data is properly written into the SQL database accordingly as new data arrives, the database is always updated correctly based on the data inserted into the TableModel and I can verify this by checking the Microsoft SQL database.

        As mentioned before the data is properly updated to microsoft SQL database, all rows are properly inserted/written and all event captured accordingly. All the data that updated to the TableModel will then get filtered according to some parameters (DATE/ event type) before being displayed on the TableView, and for this I use the QSortFilterProxyModel in-between the TableModel and the View.

        The new data is inserted to the QSqltableModel (TableModel) only and I expect the database will be updated and the table view also will be updated accordingly based on the filtering parameters of the proxy model, as the view is connected to the proxy model.

        The issue here is, each time a new row is inserted into the TableModel, the SQL Database is updated correctly but the TableView becomes blank/cleared and if I adjust the column size or scroll the view, then only it gets updated correctly. I need to scroll the view, or I can say scroll the view bar few times, then the Table View gets updated according to the proxy model sort-filter parameters which was selected beforehand, and if any continues new data is inserted to the Table Model again on the next round, the VIEW will become blank/cleared again, and then I need manually clicked on the view columns or rows, e.g. if I just resize the column few times, then the view suddenly gets updated to the latest data.

        I think the proxy-model only updates itself to the Table View if I click and move the scroll bar or resize the column, other than that it not refreshing automatically when each time a new data is inserted into the TableModel. But once I just start to resized any of the column and then play with scroll bar going up down up down ,up down, the all data gets updated in the view correctly. I also try adding the data changed signal as I show in the code snippets above, but still same.. any help or guide on this issue??

          TableModel->setData( TableModel->index(row, 1), serverDateTime);
          emit TableModel->dataChanged(TableModel->index(row, 1), TableModel->index(row, 1));
        
        

        with the emit datachanged code added or without that code ,it is still the same.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @VikramSamy
        I don't know, but a couple of observations:

        • You are using a QSqlTableModel, so it should be emitting all the dataChanged() and rowsInnserted() signals itself. I would remove all your emits.

        • HistoryProxyModel->setDynamicSortFilter(false);: try setting this to true, any difference?

        • If you remove your proxy model temporarily does everything work?

        • Try starting from a Qt example with model + proxy model, does that work?

        VikramSamyV 2 Replies Last reply
        1
        • JonBJ JonB

          @VikramSamy
          I don't know, but a couple of observations:

          • You are using a QSqlTableModel, so it should be emitting all the dataChanged() and rowsInnserted() signals itself. I would remove all your emits.

          • HistoryProxyModel->setDynamicSortFilter(false);: try setting this to true, any difference?

          • If you remove your proxy model temporarily does everything work?

          • Try starting from a Qt example with model + proxy model, does that work?

          VikramSamyV Offline
          VikramSamyV Offline
          VikramSamy
          wrote on last edited by
          #3

          @JonB said in TableView not updated or refreshed unless column is resized and scroll bar move up and down few times:

          @VikramSamy
          I don't know, but a couple of observations:

          • You are using a QSqlTableModel, so it should be emitting all the dataChanged() and rowsInnserted() signals itself. I would remove all your emits.
            => yup, i just try to test, anyway I have removed all my emit s.

          • HistoryProxyModel->setDynamicSortFilter(false);: try setting this to true, any difference?
            => previously was true, i just set to false, so no difference.

          • If you remove your proxy model temporarily does everything work?
            i will try this ,but before trying, i just found out that after i delete my current SQL database table with more than 5000++ records, and start again with new empty Table, the implementation works fine now. The only difference now is the SQL table has only todays 'dated' record, and previously the old table contains 5000++ records on different dates and so on.

          so looks the below flow below works fine with a new empty database table:-
          Microsoft SQL Database <----> QSqlTableModel --->QSortFilterProxyModel--->TableView

          • Try starting from a Qt example with model + proxy model, does that work?
            => sure i look for some example and try, but as I mention, I just found out that the flow code implemented works as expected after I delete the database table and create a new empty Table.

          So anything to do with the large record size in the SQL Table??, basically the sortfilterproxymodel filters the data using DATE, (date from and date to), and also filter based on some event type.

          I guess for now il just try insert more and more data/records into the new SQL table and see my previous issue will happen again after the records count becomes high and have different dates.

          So far I didn't do any modifications to the code, except I removed the emit s as u mentioned, but previously also without emit s also was the same issue using the large Table.

          So the only difference now is the Table in the database is a new empty table and im just starting to insert new data and the view updates accordingly for now.

          JonBJ 1 Reply Last reply
          0
          • VikramSamyV VikramSamy

            @JonB said in TableView not updated or refreshed unless column is resized and scroll bar move up and down few times:

            @VikramSamy
            I don't know, but a couple of observations:

            • You are using a QSqlTableModel, so it should be emitting all the dataChanged() and rowsInnserted() signals itself. I would remove all your emits.
              => yup, i just try to test, anyway I have removed all my emit s.

            • HistoryProxyModel->setDynamicSortFilter(false);: try setting this to true, any difference?
              => previously was true, i just set to false, so no difference.

            • If you remove your proxy model temporarily does everything work?
              i will try this ,but before trying, i just found out that after i delete my current SQL database table with more than 5000++ records, and start again with new empty Table, the implementation works fine now. The only difference now is the SQL table has only todays 'dated' record, and previously the old table contains 5000++ records on different dates and so on.

            so looks the below flow below works fine with a new empty database table:-
            Microsoft SQL Database <----> QSqlTableModel --->QSortFilterProxyModel--->TableView

            • Try starting from a Qt example with model + proxy model, does that work?
              => sure i look for some example and try, but as I mention, I just found out that the flow code implemented works as expected after I delete the database table and create a new empty Table.

            So anything to do with the large record size in the SQL Table??, basically the sortfilterproxymodel filters the data using DATE, (date from and date to), and also filter based on some event type.

            I guess for now il just try insert more and more data/records into the new SQL table and see my previous issue will happen again after the records count becomes high and have different dates.

            So far I didn't do any modifications to the code, except I removed the emit s as u mentioned, but previously also without emit s also was the same issue using the large Table.

            So the only difference now is the Table in the database is a new empty table and im just starting to insert new data and the view updates accordingly for now.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #4

            @VikramSamy
            I'm not sure where exactly you are saying your issue is. Be aware that when you insert new rows the model emits rowsInserted() signal, not dataChanged(). Not that it should matter, as the table view listens for either and updates accordingly.

            If you think it might be a proxy model issue, Qt has QIdentityProxyModel. This is a "null" proxy, it does nothing other than pass everything through to/from the source model. Try that instead of your QSortFilterProxyModel, if that works you have some issue in the QSFPM.

            VikramSamyV 1 Reply Last reply
            0
            • JonBJ JonB

              @VikramSamy
              I'm not sure where exactly you are saying your issue is. Be aware that when you insert new rows the model emits rowsInserted() signal, not dataChanged(). Not that it should matter, as the table view listens for either and updates accordingly.

              If you think it might be a proxy model issue, Qt has QIdentityProxyModel. This is a "null" proxy, it does nothing other than pass everything through to/from the source model. Try that instead of your QSortFilterProxyModel, if that works you have some issue in the QSFPM.

              VikramSamyV Offline
              VikramSamyV Offline
              VikramSamy
              wrote on last edited by
              #5

              @JonB said in TableView not updated or refreshed unless column is resized and scroll bar move up and down few times:

              @VikramSamy
              I'm not sure where exactly you are saying your issue is. Be aware that when you insert new rows the model emits rowsInserted() signal, not dataChanged(). Not that it should matter, as the table view listens for either and updates accordingly.

              If you think it might be a proxy model issue, QT has QIdentityProxyModel. This is a "null" proxy, it does nothing other than pass everything through to/from the source model. Try that instead of your QSortFilterProxyModel, if that works you have some issue in the QSFPM.

              I meant the codes work fine after I just delete the sql database Table, and created a new table with empty records. so now when the data is inserted in to the SQLtable model, both the database and the View is updated accordingly, anyway im not sure also why, but currently the record/row count about 100++ records only, I just guess after insertions of many record reaching 1000++ then the behavior will change as describe is my 1st post...anyhow il just test and study again and update here with more clearer picture of my issue.

              1 Reply Last reply
              0
              • JonBJ JonB

                @VikramSamy
                I don't know, but a couple of observations:

                • You are using a QSqlTableModel, so it should be emitting all the dataChanged() and rowsInnserted() signals itself. I would remove all your emits.

                • HistoryProxyModel->setDynamicSortFilter(false);: try setting this to true, any difference?

                • If you remove your proxy model temporarily does everything work?

                • Try starting from a Qt example with model + proxy model, does that work?

                VikramSamyV Offline
                VikramSamyV Offline
                VikramSamy
                wrote on last edited by VikramSamy
                #6

                @JonB said in TableView not updated or refreshed unless column is resized and scroll bar move up and down few times:

                @VikramSamy
                I don't know, but a couple of observations:

                • You are using a QSqlTableModel, so it should be emitting all the dataChanged() and rowsInnserted() signals itself. I would remove all your emits.
                  YES REMOVED

                • HistoryProxyModel->setDynamicSortFilter(false);: try setting this to true, any difference?
                  NO, I need to disable the-sorting mechanism in the proxy model and do the sorting in the SQL Table model itself before I initialize table-model as the source-model for the Qsortproxymodel

                • If you remove your proxy model temporarily does everything work?
                  Ya the the VIEW is updated properly if proxy model is removed.

                • Try starting from a Qt example with model + proxy model, does that work? YES I DID

                After a long gap, i just started to check this issue again,
                the previous behavior of the Table View was due to the flow of my code and it is because descending order SORTING mechanism enabled in the proxy-model, this caused the behavior as in 1st post. T

                //this was the issue
                HistoryProxyModel->setDynamicSortFilter(true);        HistoryProxyModel->sort(1,Qt::DescendingOrder);
                

                As I wanted to show the last row as first row in the Table View, I enabled the DescendingOrder sorting in the proxy model first and then apply filtering, and this was the issue .

                but then I enabled the descending Order sorting on the SQL Table Model and Disabled it on the proxy model,
                now all works fine, The proxy model just do the filtering's only. i added the code below on my Qsql Table Model.

                //this was the solution - sort the Table model 
                 TableModel->setSort(1,Qt::DescendingOrder);
                

                and so i removed the sorting codes at the proxy model .
                I removed the below code:-

                //removed from proxy codes
                 //HistoryProxyModel->sort(1,Qt::DescendingOrder)
                

                and now so far all works fine....

                1 Reply Last reply
                0
                • VikramSamyV VikramSamy has marked this topic as solved on

                • Login

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