PDA

View Full Version : How to always display latest element in QTableView while model get filled.



schall_l
24th July 2009, 20:44
Hi,

I would like to know how I can force a QTableView to always display the latest row of it model when I do insert my elements at the end of the model.

This is how I do insert my elements in the model:


bool
CommunicationModel::addFrame(const Can_Frame &frame) {
beginInsertRows(QModelIndex(), m_communication.count(), m_communication.count());
m_communication.append(frame);
endInsertRows();
return true;
}


class CommunicationModel : public QAbstractTableModel
{
Q_OBJECT
public:
static CommunicationModel* instance(QWidget* parent = 0);

int rowCount( const QModelIndex &index) const;
int columnCount( const QModelIndex &index) const;

QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
Qt::ItemFlags flags( const QModelIndex& ) const ;
bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex());
bool removeRows(int position, int rows, const QModelIndex &index=QModelIndex());
bool addFrame(const Can_Frame &frame);
void clear();

void sort (int column, Qt::SortOrder order = Qt::AscendingOrder );
private:
CommunicationModel(QObject *parent = 0);
QList<Can_Frame> m_communication; // Communication log
};

When doing this I am seeing a scroll bar appearing as soon as the number of rows in the model is becoming higher than the number of rows that can be displayed on the screen, but the scrollbar remains at the top, becoming smaller while the model get filled. What I would like to have is that the scroll bar of the QTableView is remaining at the bottom while I am filling the model so that I can always see the latest row filled in the model.

wysota
24th July 2009, 21:58
Use QAbstractItemView::scrollTo() or QScrollBar::setValue().

schall_l
24th July 2009, 22:09
Thank you,

this worked for me:


CommunicationView::CommunicationView( QWidget* parent ) :
PrintableTableView(parent)
{
m_model = CommunicationModel::instance(this);
setModel(m_model);

connect(m_model,SIGNAL(rowsInserted(const QModelIndex&, int, int)), this, SLOT(scrollToBottom()) );