pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

當數據添加到當前位置上方時停止 QTableView 滾動

Stop QTableView from scrolling as data is added above current position(當數據添加到當前位置上方時停止 QTableView 滾動)
本文介紹了當數據添加到當前位置上方時停止 QTableView 滾動的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個簡單的 QTableView,帶有一個 QSortFilterProxyModel 和一個從 QAbstractTableModel 繼承的自定義 TableModel 子類的源模型.該模型使用附加行動態更新.

I have a simple QTableView with a QSortFilterProxyModel and a source model of a custom TableModel subclass that inherits from QAbstractTableModel. The model is dynamically updated with additional rows.

我的問題是:如果我按列對表格進行排序,然后滾動到特定行,然后在該行上方添加更多行,則會將該行向下推.數據輸入的速度足夠快,以至于很難在光標下方的行發生變化的情況下單擊行進行編輯.

My problem is this: If I sort the table on a column, then scroll to a specific row, and then more rows are added above this row it pushes the row down. Data is coming in fast enough that it makes it difficult to click on rows to edit them without the row changing underneath my cursor.

有沒有辦法阻止表格滾動并保持表格相對于選定行的位置?

Is there a way to stop the table from scrolling and maintain the position of the table relative to say a selected row?

推薦答案

QTableView::rowViewportPosition() 可用于獲取當前視口位置,如果之前插入了某些內容,則必須更正該位置當前索引.

QTableView::rowViewportPosition() can be used to get the current view port position which has to be corrected if something is inserted before current index.

可以使用信號處理程序在插入行之前和之后檢索它.

It can be retrieved before and after insertion of a row using signal handlers.

因此,插入后可以在信號處理程序中相應地調整滾動.這是通過更改垂直滾動條的值來完成的.(垂直滾動模式更改為QTableView::ScrollPerPixel 以確保正確的垂直調整.)

Thus, the scrolling can be adjusted accordingly in the signal handler after the insertion. This is done changing the value of the vertical scrollbar. (The vertical scroll mode is changed to QTableView::ScrollPerPixel to ensure correct vertical adjustment.)

一個最小的代碼示例:

#include <iostream>

#include <QApplication>
#include <QMainWindow>
#include <QScrollBar>
#include <QStandardItemModel>
#include <QTableView>
#include <QTimer>

enum { NCols = 2 }; // number of columns
enum { Interval = 1000 }; // interval of auto action
enum { NRep = 5 }; // how often selected auto action is repeated

// fills a table model with sample data
void populate(QStandardItemModel &tblModel, bool prepend)
{
  int row = tblModel.rowCount();
  if (prepend) tblModel.insertRow(0);
  for (int col = 0; col < NCols; ++col) {
    QStandardItem *pItem = new QStandardItem(QString("row %0, col %1").arg(row).arg(col));
    tblModel.setItem(prepend ? 0 : row, col, pItem);
  }
}

// does some auto action
void timeout(QTimer &timer, QStandardItemModel &tblModel)
{
  static int step = 0;
  ++step;
  std::cout << "step: " << step << std::endl;
  switch (step / NRep % 3) {
    case 0: break; // pause
    case 1: populate(tblModel, false); break; // append
    case 2: populate(tblModel, true); break; // prepend
  }
}

// managing the non-scrolling when something is inserted.
struct NoScrCtxt {
  QTableView &tblView;
  int y;
  NoScrCtxt(QTableView &tblView_): tblView(tblView_) { }

  void rowsAboutToBeInserted()
  {
    y = tblView.rowViewportPosition(tblView.currentIndex().row());
  }

  void rowsInserted()
  {
    int yNew = tblView.rowViewportPosition(tblView.currentIndex().row());
    if (y != yNew) {
      if (QScrollBar *pScrBar = tblView.verticalScrollBar()) {
        pScrBar->setValue(pScrBar->value() + yNew - y);
      }
    }
  }
};

int main(int argc, char **argv)
{
  QApplication app(argc, argv);
  // build some GUI
  QMainWindow win;
  QStandardItemModel tblModel(0, NCols);
  for (int i = 0; i < 10; ++i) populate(tblModel, false);
  QTableView tblView;
  tblView.setVerticalScrollMode(QTableView::ScrollPerPixel);
  tblView.setModel(&tblModel);
  win.setCentralWidget(&tblView);
  win.show();
  // setup a "no-scroll manager"
  NoScrCtxt ctxt(tblView);
  QObject::connect(&tblModel, &QStandardItemModel::rowsAboutToBeInserted,
    [&ctxt](const QModelIndex&, int, int) { ctxt.rowsAboutToBeInserted(); });
  QObject::connect(&tblModel, &QStandardItemModel::rowsInserted,
    [&ctxt](const QModelIndex&, int, int) { ctxt.rowsInserted(); });
  // initiate some auto action
  QTimer timer;
  timer.setInterval(Interval); // ms
  QObject::connect(&timer, &QTimer::timeout,
    [&timer, &tblModel]() { timeout(timer, tblModel); });
  timer.start();
  // exec. application
  return app.exec();
}

我在 Windows 10、VS2013、Qt 5.7 中編譯并測試了這個:

I compiled and tested this in Windows 10, VS2013, Qt 5.7:

這篇關于當數據添加到當前位置上方時停止 QTableView 滾動的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 自动钻孔机-全自动数控钻孔机生产厂家-多米(广东)智能装备有限公司 | 轴流风机-鼓风机-离心风机-散热风扇-罩极电机,生产厂家-首肯电子 | 酒吧霸屏软件_酒吧霸屏系统,酒吧微上墙,夜场霸屏软件,酒吧点歌软件,酒吧互动游戏,酒吧大屏幕软件系统下载 | FAG轴承,苏州FAG轴承,德国FAG轴承-恩梯必传动设备(苏州)有限公司 | 储气罐,真空罐,缓冲罐,隔膜气压罐厂家批发价格,空压机储气罐规格型号-上海申容压力容器集团有限公司 | 电子天平-华志电子天平厂家| 空气能暖气片,暖气片厂家,山东暖气片,临沂暖气片-临沂永超暖通设备有限公司 | 智慧物联网行业一站式解决方案提供商-北京东成基业 | 水压力传感器_数字压力传感器|佛山一众传感仪器有限公司|首页 | sus630/303cu不锈钢棒,440C/430F/17-4ph不锈钢研磨棒-江苏德镍金属科技有限公司 | 钛合金标准件-钛合金螺丝-钛管件-钛合金棒-钛合金板-钛合金锻件-宝鸡远航钛业有限公司 | 金联宇电缆|广东金联宇电缆厂家_广东金联宇电缆实业有限公司 | 自清洗过滤器,浅层砂过滤器,叠片过滤器厂家-新乡市宇清净化 | 焊接烟尘净化器__焊烟除尘设备_打磨工作台_喷漆废气治理设备 -催化燃烧设备 _天津路博蓝天环保科技有限公司 | 富森高压水枪-柴油驱动-养殖场高压清洗机-山东龙腾环保科技有限公司 | 电动高压冲洗车_价格-江苏速利达机车有限公司 | 网带通过式抛丸机,,网带式打砂机,吊钩式,抛丸机,中山抛丸机生产厂家,江门抛丸机,佛山吊钩式,东莞抛丸机,中山市泰达自动化设备有限公司 | 厂房出售_厂房仓库出租_写字楼招租_土地出售-中苣招商网-中苣招商网 | 高温热泵烘干机,高温烘干热泵,热水设备机组_正旭热泵 | 耐力板-PC阳光板-PC板-PC耐力板 - 嘉兴赢创实业有限公司 | 气动|电动调节阀|球阀|蝶阀-自力式调节阀-上海渠工阀门管道工程有限公司 | 减速机三参数组合探头|TSM803|壁挂式氧化锆分析仪探头-安徽鹏宸电气有限公司 | 机构创新组合设计实验台_液压实验台_气动实训台-戴育教仪厂 | 儋州在线-儋州招聘找工作、找房子、找对象,儋州综合生活信息门户! | 诸城网站建设-网络推广-网站优化-阿里巴巴托管-诸城恒泰互联 | 包装盒厂家_纸盒印刷_礼品盒定制-济南恒印包装有限公司 | 浙江美尔凯特智能厨卫股份有限公司| 数年网路-免费在线工具您的在线工具箱-shuyear.com | 示波器高压差分探头-国产电流探头厂家-南京桑润斯电子科技有限公司 | 全自动包衣机-无菌分装隔离器-浙江迦南科技股份有限公司 | 武汉刮刮奖_刮刮卡印刷厂_为企业提供门票印刷_武汉合格证印刷_现金劵代金券印刷制作 - 武汉泽雅印刷有限公司 | 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | 电磁铁_推拉电磁铁_机械手电磁吸盘电磁铁厂家-广州思德隆电子公司 | 集装袋吨袋生产厂家-噸袋廠傢-塑料编织袋-纸塑复合袋-二手吨袋-太空袋-曹县建烨包装 | 合肥地磅_合肥数控切割机_安徽地磅厂家_合肥世佳电工设备有限公司 | 橡胶膜片,夹布膜片,橡胶隔膜密封,泵阀设备密封膜片-衡水汉丰橡塑科技公司网站 | 量子管通环-自清洗过滤器-全自动反冲洗过滤器-北京罗伦过滤技术集团有限公司 | ICP备案查询_APP备案查询_小程序备案查询 - 备案巴巴 | 金库门,金库房,金库门厂家,金库门价格-河北特旺柜业有限公司 | 耐高温风管_耐高温软管_食品级软管_吸尘管_钢丝软管_卫生级软管_塑料波纹管-东莞市鑫翔宇软管有限公司 | 氧化锆纤维_1800度高温退火炉_1800度高温烧结炉-南京理工宇龙新材料股份有限公司 |