Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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");
        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.


  • Moderators

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

    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]
    


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

    Hi,

    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