2016-08-02 57 views
0

我使用C++,Qt 4.8.7,Visual Studio 2013,Windows 7. 我的GUI應用程序包含QTableView。我添加了插槽,處理來自我的模型的信號beginResetModel()reset()。通常會調用這些插槽,但垂直滾動仍會始終滾動到整個列表的頂部。我該如何解決它?我需要恢復當前位置。請注意,在復位期間,不會刪除行。因此,可以更改舊行內容和/或添加新行。在QTableView中恢復垂直卷軸

//before reset 
current_top_row_number_ = table_view_->rowAt(0); 

//after reset 
table_view_->scrollTo(log_model_->index(current_top_row_number_, 0), QAbstractItemView::PositionAtTop); 

回答

0

嘗試排隊呼叫到scrollTo,例如,

class MyWidget : public MyWidget { 
    Q_OBJECT 
    int current_top_row_number_; 
    QTableView table_view_; 
    Q_SLOT void onResetModel() { 
    table_view_->scrollTo(log_model_->index(current_top_row_number_, 0), 
          QAbstractItemView::PositionAtTop); 
    } 
    ... 
public: 
    MyWidget(QWidget * parent = nullptr) : QWidget{parent} {  
    connect(table_view_, SIGNAL(modelReset()), SLOT(onResetModel(), Qt::QueuedConnection); 
    } 
};