QTableView:选择单元格时,如何使选择的第一个单元格成为当前索引?

发布于 2024-10-28 03:46:15 字数 661 浏览 5 评论 0原文

我有一个继承 QTableView 的简单类,我想要以下行为:当用户选择几个单元格时,我希望将选定的第一个单元格设置为当前索引。

因此,例如,如果我从 (0, 0) 到 (2, 2) 选择,当我开始输入时,文本将显示在 (0, 0),而不是 (2, 2),这似乎是默认值。

我尝试使用以下内容覆盖 setSelection 函数:

void SampleTable::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command)
{
    if((command & QItemSelectionModel::Current) != 0)
    {
        QModelIndex curr = indexAt(rect.topLeft());
        selectionModel()->select(curr, QItemSelectionModel::Current);

        command ^= QItemSelectionModel::Current;
    }
    QTableView::setSelection(rect, command);
}

但无济于事。这似乎与鼠标事件有关,但我无法在源代码中找到问题所在,我希望有一种更简单的方法。

I have a simple class which inherits QTableView and I want the following behavior: when the user selects a few cells, I want the first cell selected to be set as the current index.

So for example if I select from (0, 0) towards (2, 2), when I start typing the text would show up in (0, 0), not (2, 2) which seems to be the default.

I have tried overriding the setSelection function with the following:

void SampleTable::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command)
{
    if((command & QItemSelectionModel::Current) != 0)
    {
        QModelIndex curr = indexAt(rect.topLeft());
        selectionModel()->select(curr, QItemSelectionModel::Current);

        command ^= QItemSelectionModel::Current;
    }
    QTableView::setSelection(rect, command);
}

but to no avail. It seems to have something to do with the mouse events, but I can't quite locate the problem in the source code and I'm hoping there's an easier way anyway.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

囚我心虐我身 2024-11-04 03:46:15

你想达到什么目的?如果您希望用户仅编辑/选择单个单元格,请使用 setSelectionBehaviour 来强制执行此操作。否则,您可以尝试 chinfoo 的想法,但确保以用户能够理解的方式传达行为(即他能够看到他的编辑将更改第一个单元格/行)。

What are you trying to achieve? If you'd like the user to edit/select a single cell only, use setSelectionBehaviour to force this. Otherwise you could try chinfoo's idea but make sure to communicate the behavior in a way the user is able to understand it (i.e. he's able to see that his edit will change the first cell/row).

苏辞 2024-11-04 03:46:15

我找出了问题所在以及如何解决它,但情况并不好。问题出在 QAbstractItemView 的鼠标移动事件中。经过多次调试和搜索源代码后,我在 qabstractitemview.cpp 中发现了这一点:

void QAbstractItemView::mouseMoveEvent(QMouseEvent *event)
...
if (index.isValid()
        && (index != d->selectionModel->currentIndex())
        && d->isIndexEnabled(index))
    d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
}

我通过给我的类提供一个 QModelIndex 成员来修复它,该成员存储左上角 QModelIndex 的最后一个位置(在我的 setSelection 的重写版本中设置,如上面所示),然后我用以下内容覆盖 mouseMoveEvent:


void SampleTable::mouseMoveEvent(QMouseEvent *event)
{
    QTableView::mouseMoveEvent(event);
    if (state() == ExpandingState || state() == CollapsingState || state() == DraggingState || state() == EditingState)
            return;
    if ((event->buttons() & Qt::LeftButton)) {
        if (m_topLeft.isValid())
        {
            selectionModel()->setCurrentIndex(m_topLeft, QItemSelectionModel::NoUpdate);
        }
    }
}

Not a pretty solution, but it works.

I figured out the problem and how to fix it, but it's not pretty. The problem is in the mouse moved event for a QAbstractItemView. After much debugging and searching through the source code I found this in qabstractitemview.cpp:

void QAbstractItemView::mouseMoveEvent(QMouseEvent *event)
...
if (index.isValid()
        && (index != d->selectionModel->currentIndex())
        && d->isIndexEnabled(index))
    d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
}

I fixed it by giving my class a QModelIndex member which stores the last location of the top left QModelIndex (set in my overriden version of setSelection like above) and then I overrode the mouseMoveEvent with this:


void SampleTable::mouseMoveEvent(QMouseEvent *event)
{
    QTableView::mouseMoveEvent(event);
    if (state() == ExpandingState || state() == CollapsingState || state() == DraggingState || state() == EditingState)
            return;
    if ((event->buttons() & Qt::LeftButton)) {
        if (m_topLeft.isValid())
        {
            selectionModel()->setCurrentIndex(m_topLeft, QItemSelectionModel::NoUpdate);
        }
    }
}


Not a pretty solution, but it works.

离笑几人歌 2024-11-04 03:46:15

QtableWidget 类有一个信号 itemSelectionChanged(),将其连接到您的自定义插槽。在该槽中,使用 selectedIndexes() 获取所有索引,然后使用 setCurrentIndex() 设置要作为当前索引的单元格。

The QtableWidget class has a signal itemSelectionChanged(), connect it to your custom slot. In that slot, use selectedIndexes() to get all indexes, then use setCurrentIndex() to set the cell which you want to be the current index.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文