如何在QTreeWidget中找到所选项?

时间:2022-09-08 19:32:12

I have a class that inherits QTreeWidget. How can I find the currently selected row? Usually I connect signals to slots this way:

我有一个继承QTreeWidget的类。如何找到当前选定的行?通常我会以这种方式将信号连接到插槽:

connect(myButton, SIGNAL(triggered(bool)), this, SLOT(myClick()));

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:

但是,我找不到任何与QTreeWidget-> QTreeWidgetItem类似的东西。我找到的唯一方法是重新定义QTreeWidget类的mousePressEvent,如下所示:

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

I didn't try it yet. Is the only solution or is there any easier way?

我还没试过。是唯一的解决方案还是有更简单的方法?

5 个解决方案

#1


4  

Using the itemClicked() signal will miss any selection changes made using the keyboard. I'm assuming that's a bad thing in your case.

使用itemClicked()信号将错过使用键盘进行的任何选择更改。我认为在你的情况下这是件坏事。

#2


11  

Dusty is almost correct. But the itemSelectionChanged signal will not tell you which item is selected.

尘土飞扬几乎是正确的。但是itemSelectionChanged信号不会告诉您选择了哪个项目。

QList<QTreeWidgetItem *> QTreeWidget::selectedItems() const

will give you the selected item(s).

会给你所选的项目。

So, connect a slot to the itemSelectionChanged signal, then call selectedItems() on the tree widget to get the selected item(s).

因此,将一个槽连接到itemSelectionChanged信号,然后在树窗口小部件上调用selectedItems()以获取所选项目。

#3


2  

you can simply use this :

你可以简单地使用这个:

QString word = treeWidget->currentItem()->text(treeWidget->currentColumn());

to get your text in the variable word.

在变量字中获取文本。

#4


1  

According to the documentation here it appears that you should connect the QTreeWidget itemSelectionChanged() signal to a slot in your class. That will tell you which QTreeWidgetItem was selected which is what I believe you want.

根据此处的文档,您似乎应该将QTreeWidget itemSelectionChanged()信号连接到类中的插槽。这将告诉你选择了哪个QTreeWidgetItem,这是我认为你想要的。

#5


0  

ooops, I've solved simply using this:

ooops,我已经解决了这个问题:

connect(this,SIGNAL(itemClicked(QTreeWidgetItem*, int)), SLOT(mySlot()));

however thanks for replies :D

但感谢您的回复:D

#1


4  

Using the itemClicked() signal will miss any selection changes made using the keyboard. I'm assuming that's a bad thing in your case.

使用itemClicked()信号将错过使用键盘进行的任何选择更改。我认为在你的情况下这是件坏事。

#2


11  

Dusty is almost correct. But the itemSelectionChanged signal will not tell you which item is selected.

尘土飞扬几乎是正确的。但是itemSelectionChanged信号不会告诉您选择了哪个项目。

QList<QTreeWidgetItem *> QTreeWidget::selectedItems() const

will give you the selected item(s).

会给你所选的项目。

So, connect a slot to the itemSelectionChanged signal, then call selectedItems() on the tree widget to get the selected item(s).

因此,将一个槽连接到itemSelectionChanged信号,然后在树窗口小部件上调用selectedItems()以获取所选项目。

#3


2  

you can simply use this :

你可以简单地使用这个:

QString word = treeWidget->currentItem()->text(treeWidget->currentColumn());

to get your text in the variable word.

在变量字中获取文本。

#4


1  

According to the documentation here it appears that you should connect the QTreeWidget itemSelectionChanged() signal to a slot in your class. That will tell you which QTreeWidgetItem was selected which is what I believe you want.

根据此处的文档,您似乎应该将QTreeWidget itemSelectionChanged()信号连接到类中的插槽。这将告诉你选择了哪个QTreeWidgetItem,这是我认为你想要的。

#5


0  

ooops, I've solved simply using this:

ooops,我已经解决了这个问题:

connect(this,SIGNAL(itemClicked(QTreeWidgetItem*, int)), SLOT(mySlot()));

however thanks for replies :D

但感谢您的回复:D