framelesshelper icon indicating copy to clipboard operation
framelesshelper copied to clipboard

Utilities::mapOriginPointToWindow is wrong in some cases

Open SineStriker opened this issue 2 years ago • 5 comments

// utilities.cpp
QPointF Utilities::mapOriginPointToWindow(const QObject *object) {
    Q_ASSERT(object);
    if (!object) {
        return {};
    }
    if (!object->isWidgetType() && !object->inherits("QQuickItem")) {
        qWarning() << object << "is not a QWidget or a QQuickItem.";
        return {};
    }
    QPointF point = {object->property("x").toReal(), object->property("y").toReal()};
    for (QObject *parent = object->parent(); parent; parent = parent->parent()) {
        point += {parent->property("x").toReal(), parent->property("y").toReal()};
        if (parent->isWindowType()) { // If obj is a dialog which has a parent window, then the last addition is redundant
            break;
        }
    }
    return point;
}

This function does not apply to dialogs. When a frameless window's WindowFlags contains Qt::Dialog and it has parent widget, it's QWidget::isWindowType returns false but looks like a window however. What's really strange is that it's QWidget::pos() returns its absolute coordinates in the screen instead of coordinates relative to the parent widget, in this case, this function obviously adds the coordinates of its parent widget unnecessarily. So why not directly call QWidget::mapToGlobal? I'm using Qt 5.15.2.

SineStriker avatar Apr 14 '22 09:04 SineStriker

好的,我研究下。 你那边必须要使用Qt::Dialog吗,能不能用普通的widget代替?

wangwenx190 avatar Apr 14 '22 09:04 wangwenx190

有时候需要一个嵌入的Dialog,虽然也能实现阻塞,但这样就有两个窗口,有些情况下就显得麻烦。反正我也不用quick,暂时用QWidget::mapToGlobal解决了。

SineStriker avatar Apr 14 '22 11:04 SineStriker

代码里没有直接使用QWidget::mapToGlobal是因为想避免直接依赖QWidget模块,不过你这样改也可以,从原理上来讲是没错的。你自己用的话就那么改吧,不会导致什么问题

wangwenx190 avatar Apr 16 '22 09:04 wangwenx190

可以,但是这个问题可以注意一下。

SineStriker avatar Apr 17 '22 07:04 SineStriker

Duplicates #83

wangwenx190 avatar Apr 23 '22 07:04 wangwenx190