Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QTableView:选择单元格时,如何使第一个选定的单元格成为当前索引?_C++_Qt_Indexing_Selection_Qtableview - Fatal编程技术网

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

C++ QTableView:选择单元格时,如何使第一个选定的单元格成为当前索引?,c++,qt,indexing,selection,qtableview,C++,Qt,Indexing,Selection,Qtableview,我有一个继承QTableView的简单类,我想要以下行为:当用户选择几个单元格时,我想要将选择的第一个单元格设置为当前索引 例如,如果我选择从(0,0)到(2,2),当我开始键入时,文本将显示在(0,0),而不是(2,2),这似乎是默认值 我已尝试用以下内容覆盖setSelection函数: void SampleTable::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command) { if

我有一个继承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);
}

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

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

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

我找到了问题以及如何解决它,但这并不漂亮。问题出在QAbstractItemView的鼠标移动事件中。在对源代码进行大量调试和搜索后,我在qabstractemview.cpp中找到了以下内容:

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

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


void SampleTable::mouseMoveEvent(QMouseEvent*事件)
{
QTableView::mouseMoveEvent(事件);
如果(状态()==展开状态| |状态()==折叠状态| |状态()==拖动状态| |状态()==编辑状态)
回来
if((事件->按钮()&Qt::LeftButton)){
if(m_topLeft.isValid())
{
selectionModel()->setCurrentIndex(m_左上角,QItemSelectionModel::NoUpdate);
}
}
}

这不是一个很好的解决方案,但它很有效。

我使用的是QTableView,而不是QTableWidget,因此信号不可用。
void QAbstractItemView::mouseMoveEvent(QMouseEvent *event)
...
if (index.isValid()
        && (index != d->selectionModel->currentIndex())
        && d->isIndexEnabled(index))
    d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
}

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);
        }
    }
}