本文介绍了如何限制在QTableWidget中的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何限制在QTableWidget中选择的行/列?我需要强制用户使用连续的选择(已完成)才能精确选择两列和任意数量的行。



谢谢!

解决方案

您可能需要做以下两件事之一:


  1. 您必须将 QItemSelectionModel 子类化,并实现添加和删除所选 QModelIndex es的函数,

    您可以通过自定义实现来捕获 QItemSelectionModel 发出的信号,例如:



    connect(tableWidget-> selectionModel(),
    SIGNAL(selectionChanged(QItemSelection& QItemSelection&)),
    selectionHandler,
    SLOT(updateSelection(QItemSelection& QItemSelection&)));


selectionHandler 是检查 QModelIndex 项目的行和列的对象 QItemSelection ,并移除您希望用户保留的行范围之外的所有索引,然后:

  selectionHandler-> ignoreSelectionUpdateSignal(); 
tableWidget-> selectionModel() - > select(QItemSelection&);
selectionHandler-> acceptSelectionUpdateSignal();

忽略 accept 您需要确保您不会进入无限循环处理 selectionChanged 信号。


How would I go about limiting the rows/columns selected in a QTableWidget? I need to force the user to use a contiguous selection (already done) to select exactly two columns and any amount of rows.

Thanks!

解决方案

You will probably have to do one of 2 things:

  1. You would have to subclass QItemSelectionModel and implement functions for adding and deleting selected QModelIndexes so that you only add items from 2 rows to it.
  2. You can do this by having a custom implementation for catching signals that QItemSelectionModel emits such as:

    connect(tableWidget->selectionModel(), SIGNAL(selectionChanged(QItemSelection &, QItemSelection &)), selectionHandler, SLOT(updateSelection(QItemSelection &, QItemSelection &)));

The selectionHandler is the object that checks the rows and columns of the QModelIndex items in the QItemSelection and remove all Indices that are outside the row range that you would like the user to keep and then:

selectionHandler->ignoreSelectionUpdateSignal();
tableWidget->selectionModel()->select(QItemSelection&);
selectionHandler->acceptSelectionUpdateSignal();

The ignore and accept you need to make sure that you don't get into an infinite loop processing selectionChanged signal.

这篇关于如何限制在QTableWidget中的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 22:00