简体   繁体   English

Qt5检测另一个小部件

[英]Qt5 detecting widgets under an other one

I am trying to detect if two widgets are overlapping without any success. 我试图检测两个小部件是否重叠而没有任何成功。 Here is a minimal example: 这是一个最小的例子:

mainwindow.h mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void hover();
};

#endif // MAINWINDOW_H

mainwindow.cpp mainwindow.cpp

#include "mainwindow.h"
#include "hover.h"
#include <QPushButton>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    QPushButton* pb = new QPushButton("Button", this);
    setCentralWidget(pb);
    connect(pb, SIGNAL(clicked(bool)), this, SLOT(hover()));
}

MainWindow::~MainWindow() {}

void MainWindow::hover()
{
    Hover* h = new Hover(centralWidget());
    h->show();
}

hover.h hover.h

#ifndef HOVER_H
#define HOVER_H

#include <QDialog>

class QMoveEvent;

class Hover : public QDialog
{
public:
    Hover(QWidget* w, Qt::WindowFlags f = Qt::WindowFlags());

protected:
    void moveEvent(QMoveEvent* event);

private:
    QWidget* w;
};

#endif // HOVER_H

hover.cpp hover.cpp

Hover::Hover(QWidget* w, Qt::WindowFlags f) : QDialog(nullptr, f), w(w)
{
    setAttribute(Qt::WA_DeleteOnClose);
}

void Hover::moveEvent(QMoveEvent* event)
{
    if(qApp->widgetAt(event->pos()) == w)
    {
        qDebug() << "YES"; // Never triggered.
    }
}

As you can see above, I re implemented the QDialog::moveEvent of the Hover class in order to get notified when the widgets is dragged onto the MainWindow 's QPushButton . 如上所示,我重新实现了Hover类的QDialog::moveEvent ,以便在将小部件拖到MainWindowQPushButton上时得到通知。 However, when I move the dialog over the button, it does not work. 但是,当我在按钮上移动对话框时,它不起作用。

Does anyone have a solution? 有没有人有办法解决吗?

Thanks for your answers. 谢谢你的回答。

Edit 编辑

I played around with eyllanesc's code. 我玩了eyllanesc的代码。 It works well for the above sample. 它适用于上述样本。 However I noticed something weird when using QPropertyAnimation . 但是,当我使用QPropertyAnimation时,我发现了一些奇怪的东西。 In fact, the global coordinates of the QPushButton seem to be wrong. 实际上, QPushButton的全局坐标似乎是错误的。 The x coordinate of the QPushButton is slightly offset to the right. QPushButtonx坐标略微向右偏移。 Just replace the code of the MainWindow by the following one: 只需用以下代码替换MainWindow的代码:

mainwindow.h mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
class QPushButton;
class Hover;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void hover();

private:
    QPushButton* pb;
    Hover* h;
};

#endif // MAINWINDOW_H

mainwindow.cpp mainwindow.cpp

#include "mainwindow.h"
#include "hover.h"
#include <QPushButton>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    pb = new QPushButton("Button", this);
    connect(pb, SIGNAL(clicked(bool)), this, SLOT(hover()));
}

MainWindow::~MainWindow() {}

void MainWindow::hover()
{
    QPropertyAnimation* a = new QPropertyAnimation(pb, "geometry");
    a->setDuration(1000);
    a->setStartValue(QRect(0, 0, pb->width(), pb->height()));
    a->setEndValue(QRect(50, 50, pb->width(), pb->height()));
    a->start();
    h = new Hover(pb);
    h->setGeometry(300, 300, 50, 100);
    h->show();
}

Does that happen to you as well? 这也发生在你身上吗? If so, does anyone have an explanation? 如果是这样,有没有人有解释?

Thanks again for your answers. 再次感谢您的回答。

Instead of using the widgetAt() function we can use the intersection of its areas (according to docs widgetAt() is slow) 我们可以使用它的区域的交集(根据docs widgetAt()很慢)而不是使用widgetAt()函数

For this we use the global position through the function mapToGlobal() and pos() , and the function size() , after that we check if there is an intersection 为此,我们通过函数mapToGlobal()pos()以及函数size()使用全局位置,之后我们检查是否存在交集

void Hover::moveEvent(QMoveEvent*)
{
    if(!visibleRegion().isNull() && w)
    {
       QRect RectW(w->mapToGlobal(QPoint(QPoint(0,0))), w->size()) ;
       QRect RectHover = QRect(pos(), size());
       if(!RectW.intersected(RectHover).isEmpty())
           qDebug()<<"yes";
       else {
           qDebug()<<"no";
       }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM