QGraphicsScene中嵌入QWidget点击时childAt获得的是QWidget,而不是设置的控件
时间: 2024-03-03 07:51:44 浏览: 25
如果您使用 QGraphicsScene 中的 QGraphicsProxyWidget 将 QWidget 添加到场景中,则在调用 childAt() 方法时,将返回 QGraphicsProxyWidget,而不是 QWidget。因为 QGraphicsProxyWidget 是 QGraphicsItem 的子类,而 QGraphicsItem 是 QGraphicsScene 中的基本元素。如果您需要获取该控件,可以使用 QGraphicsProxyWidget 的 widget() 方法获取QWidget 对象。例如:
```python
# proxy 是 QGraphicsProxyWidget 对象
proxy = scene.addWidget(widget)
# 获取控件
widget = proxy.widget()
# 获取控件位置
pos = proxy.pos()
```
相关问题
QGraphicsScene中嵌入QWidget,QWidget又嵌入QLabel,如何获得鼠标点击下的QLabel
可以通过在QLabel上安装事件过滤器来实现获取鼠标点击下的QLabel。
首先,在QGraphicsScene中嵌入QWidget,同时在该QWidget中嵌入QLabel。然后,为QWidget安装事件过滤器,重写eventFilter方法,在该方法中判断事件是否为鼠标点击事件。如果是,则获取事件的位置,转换为在QLabel中的位置,然后通过childAt方法获取该位置下的QLabel,即可得到鼠标点击下的QLabel。
以下是代码示例:
```python
class MyWidget(QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
self.label = QLabel(self)
self.label.setText("Click me!")
# 安装事件过滤器
self.installEventFilter(self)
def eventFilter(self, obj, event):
if event.type() == QEvent.MouseButtonPress:
pos = event.pos()
label_pos = self.label.pos()
label_rect = self.label.rect()
# 判断事件是否发生在QLabel中
if label_rect.contains(pos - label_pos):
print("QLabel clicked")
return True
return super(MyWidget, self).eventFilter(obj, event)
```
在QGraphicsScene中添加MyWidget即可。
QGraphicsScene中嵌入QWidget,QWidget又嵌入QLabel,如何获得鼠标点击下的QLabel C++
可以通过在QLabel上安装事件过滤器来实现获取鼠标点击下的QLabel。
首先,在QGraphicsScene中嵌入QWidget,同时在该QWidget中嵌入QLabel。然后,为QWidget安装事件过滤器,重写eventFilter方法,在该方法中判断事件是否为鼠标点击事件。如果是,则获取事件的位置,转换为在QLabel中的位置,然后通过childAt方法获取该位置下的QLabel,即可得到鼠标点击下的QLabel。
以下是代码示例:
```c++
class MyWidget : public QWidget
{
public:
MyWidget(QWidget* parent = nullptr) : QWidget(parent)
{
label = new QLabel(this);
label->setText("Click me!");
// 安装事件过滤器
this->installEventFilter(this);
}
protected:
bool eventFilter(QObject* obj, QEvent* event) override
{
if (event->type() == QEvent::MouseButtonPress)
{
auto mouseEvent = static_cast<QMouseEvent*>(event);
QPoint pos = mouseEvent->pos();
QPoint labelPos = label->pos();
QRect labelRect = label->rect();
// 判断事件是否发生在QLabel中
if (labelRect.contains(pos - labelPos))
{
qDebug() << "QLabel clicked";
return true;
}
}
return QWidget::eventFilter(obj, event);
}
private:
QLabel* label;
};
```
在QGraphicsScene中添加MyWidget即可。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)