Important: Please read the Qt Code of Conduct -

QTableView setting last index as current index

  • 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");
        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:


    doesn't work.

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

    Thanks in advance.

  • Moderators

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

  • @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.


    class StarDelegate : public QStyledItemDelegate
        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;


    class StarRating
        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; }
        QPolygonF starPolygon;
        int myStarCount;
        int myMaxStarCount;
    //! [0]
    //! [1]
    //! [1]

  • @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?

  • Lifetime Qt Champion


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

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

    while (model.canFetchMore()) model.fetchMore();

Log in to reply