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. QTableView setting last index as current index
Forum Updated to NodeBB v4.3 + New Features

QTableView setting last index as current index

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 2.7k Views 2 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
    davidesalvetti
    wrote on last edited by
    #1

    hi,

    I'm having problem setting as current index the last index of a query.
    This is my code:

        int rownum = 0;
        qry.exec("SELECT index FROM Table");
        while(qry.next())
        {
            rownum++;
        }
        model_Session.setQuery(qry);
        sessionProxyModel.setSourceModel(&model_Session);                       
    
        ui->SessionTable->setModel(&sessionProxyModel);
        qDebug() << "ROW" << rownum;                                       //row count gives me 331
    
       ui->SessionTable->setCurrentIndex(ui->SessionTable->model()->index(ui->SessionTable->model()->rowCount()-1,0));                                                                  //ui->SessionTable->model()->rowCount() gives me 255
    

    ui->SessionTable->model()->rowCount() should give me an int so I don't know why it stops at only 255;

    It worked perfectly until I reached 256 rows in my database.
    Also giving:

     ui->SessionTable->setCurrentIndex(ui->SessionTable->model()->index(rownum-1,0));
    

    doesn't work.

    Anyone knows how to set last index of my query as current index? Any workaround?

    Thanks in advance.

    raven-worxR 1 Reply Last reply
    0
    • D davidesalvetti

      hi,

      I'm having problem setting as current index the last index of a query.
      This is my code:

          int rownum = 0;
          qry.exec("SELECT index FROM Table");
          while(qry.next())
          {
              rownum++;
          }
          model_Session.setQuery(qry);
          sessionProxyModel.setSourceModel(&model_Session);                       
      
          ui->SessionTable->setModel(&sessionProxyModel);
          qDebug() << "ROW" << rownum;                                       //row count gives me 331
      
         ui->SessionTable->setCurrentIndex(ui->SessionTable->model()->index(ui->SessionTable->model()->rowCount()-1,0));                                                                  //ui->SessionTable->model()->rowCount() gives me 255
      

      ui->SessionTable->model()->rowCount() should give me an int so I don't know why it stops at only 255;

      It worked perfectly until I reached 256 rows in my database.
      Also giving:

       ui->SessionTable->setCurrentIndex(ui->SessionTable->model()->index(rownum-1,0));
      

      doesn't work.

      Anyone knows how to set last index of my query as current index? Any workaround?

      Thanks in advance.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @davidesalvetti
      255 sounds like you have some sort of incorrect data-type in your implementation.
      Did you reimplement a model?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      D 2 Replies Last reply
      1
      • raven-worxR raven-worx

        @davidesalvetti
        255 sounds like you have some sort of incorrect data-type in your implementation.
        Did you reimplement a model?

        D Offline
        D Offline
        davidesalvetti
        wrote on last edited by davidesalvetti
        #3

        @raven-worx no, the models I'm using are declared like this:

        QSqlQueryModel model_Session;
        QSortFilterProxyModel sessionProxyModel;
        

        I've just missed that I used an item delegate taken from QT example:

        ui->SessionTable->setItemDelegate(new StarDelegate);

        but there are no declaration of uint8_t or declaration of row/column.

        stardelegate.cpp

        class StarDelegate : public QStyledItemDelegate
        {
            Q_OBJECT
        
        public:
            StarDelegate(QWidget *parent = 0) : QStyledItemDelegate(parent) {}
        
            void paint(QPainter *painter, const QStyleOptionViewItem &option,
                       const QModelIndex &index) const override;
            QSize sizeHint(const QStyleOptionViewItem &option,
                           const QModelIndex &index) const override;
        
        };
        

        starrating.cpp

        class StarRating
        {
        public:
            enum EditMode { Editable, ReadOnly };
        
            explicit StarRating(int starCount = 1, int maxStarCount = 5);
        
            void paint(QPainter *painter, const QRect &rect,
                       const QPalette &palette, EditMode mode) const;
            QSize sizeHint() const;
            int starCount() const { return myStarCount; }
            int maxStarCount() const { return myMaxStarCount; }
            void setStarCount(int starCount) { myStarCount = starCount; }
            void setMaxStarCount(int maxStarCount) { myMaxStarCount = maxStarCount; }
        
        private:
            QPolygonF starPolygon;
            int myStarCount;
            int myMaxStarCount;
        };
        //! [0]
        
        //! [1]
        Q_DECLARE_METATYPE(StarRating)
        //! [1]
        
        1 Reply Last reply
        0
        • raven-worxR raven-worx

          @davidesalvetti
          255 sounds like you have some sort of incorrect data-type in your implementation.
          Did you reimplement a model?

          D Offline
          D Offline
          davidesalvetti
          wrote on last edited by
          #4

          @raven-worx I didn't found a solution to my problem, but now it's time to search for one. I've done some researches and I found this post.

          It seems that QSqlQueryModel only loads the first 255 rows. But there must be a workaround to select the last row of my query that contains more than 255 rows. Does somebody have any ideas?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            QSqlQueryModel::canFetchMore is likely going to be of interest.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            D 1 Reply Last reply
            3
            • SGaistS SGaist

              Hi,

              QSqlQueryModel::canFetchMore is likely going to be of interest.

              D Offline
              D Offline
              davidesalvetti
              wrote on last edited by
              #6

              @SGaist Thank you! It was exactly what I needed.

              while (model.canFetchMore()) model.fetchMore();
              
              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