本文介绍了如何在QTreeWidget中找到所选项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个继承QTreeWidget的类。如何找到当前选定的行?
通常我以这种方式连接信号到插槽:
I have a class that inherits QTreeWidget. How can I find the currently selected row?Usually I connect signals to slots this way:
connect(myButton, SIGNAL(triggered(bool)), this, SLOT(myClick()));
但是,我找不到类似 QTreeWidget-> QTreeWidgetItem
。
我发现的唯一方法是重新定义的QTreeWidget类的mousePressEvent像这样:
However, I can't find anything similar for QTreeWidget->QTreeWidgetItem
.The only way I found is to redefine the mousePressEvent of the QTreeWidget class like this:
void MyQTreeWidget::mousePressEvent(QMouseEvent *e){
QTreeView::mousePressEvent(e);
const QModelIndex index = indexAt(e->pos());
if (!index.isValid())
{
const Qt::KeyboardModifiers modifiers = QApplication::keyboardModifiers();
if (!(modifiers & Qt::ShiftModifier) && !(modifiers & Qt::ControlModifier))
clearSelection();
}
}
我没有尝试。
推荐答案
使用itemClicked()信号会错过任何使用键盘。我假设这是一件坏事在你的情况下。
Using the itemClicked() signal will miss any selection changes made using the keyboard. I'm assuming that's a bad thing in your case.
这篇关于如何在QTreeWidget中找到所选项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!