简体   繁体   English

Qt非矩形窗

[英]Qt non rectangular window

I am trying to create a non rectangular window with Qt. 我正在尝试使用Qt创建一个非矩形窗口。

Like this: 像这样:

           ______________________
          |                      |
    ______|                      |
  /                              |
 /                               |
|                                |
|                                |
 \                               |
  \_______                       |
          |                      |
          |                      |
          |                      |
          |______________________|

With the transparent parts actually being click through to the non-Qt window below. 透明部分实际被点击进入下面的非Qt窗口。

The only way I have found to get this done is: window->setMask(complex_region); 我发现做到这一点的唯一方法是: window->setMask(complex_region);

But that seems pretty hacky, especially since I now need to know the sizes of window parts both in the C++ and qml. 但这似乎很棘手,特别是因为我现在需要了解C ++和qml中窗口部件的大小。

Ideally I would like to tell Qt to just not render fully transparent stuff. 理想情况下,我想告诉Qt不要渲染完全透明的内容。

You can create a main widget and with qpainter you draw the design. 您可以创建一个主窗口小部件,并使用qpainter绘制设计。 After all others widgets are inside the main widget. 毕竟所有其他小部件都位于主小部件内。 Finally, you add transparancy for the main widget and you have what you want. 最后,为主窗口小部件添加透明性,您将拥有所需的内容。

Just setting the color of the Window to transparent and set flags: Qt.FramelessWindowHint works for me on macOS : 只需将Windowcolor设置为transparent并设置flags: Qt.FramelessWindowHint在macOS上对我有效:

非矩形QML窗口

If your main concern is, that you want to set the mask from QML, you can create a QObject that is accessible from QML to tunnel to the methods setMask and mask of the QWindow . 如果您主要要设置QML的遮罩,则可以创建一个QObject ,该QObject可从QML访问,以隧穿到QWindow setMaskmask方法。

I have decided I will create this Object from within QML. 我决定我将从QML中创建此对象。 The design might be improved, but it works, and shall outline the idea. 该设计可能会得到改进,但是会起作用,并且会概述该想法。

exposemask.h 暴露掩码

#ifndef EXPOSEMASK_H
#define EXPOSEMASK_H

#include <QObject>
#include <QRegion>
#include <QWindow>

class ExposeMask : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QRegion mask READ mask WRITE setMask)

public:
    ExposeMask(QObject* parent = nullptr);

    Q_INVOKABLE void setWindow(QWindow* win);
    // Little helper to get a region from a image in QML.
    Q_INVOKABLE QRegion regionFromImage(QString path);

    QRegion mask() const;

    void setMask(const QRegion &region);

private:
    QWindow* m_window;

};

#endif // EXPOSEMASK_H

exposemask.cpp Exposuremask.cpp

#include "exposemask.h"
#include <QPixmap>
#include <QBitmap>
#include <QDebug>

ExposeMask::ExposeMask(QObject* parent)
        : QObject(parent)
        , m_window(nullptr)
{
}

QRegion ExposeMask::regionFromImage(QString path)
{
        QPixmap pix(path);
        return pix.mask();
}

QRegion ExposeMask::mask() const
{
    if (m_window != nullptr)
    {
        return m_window->mask();
    }
    else
    {
        qDebug() << "No Window Set";
    }
}

void ExposeMask::setMask(const QRegion &region)
{
    m_window->setMask(region);
}

void ExposeMask::setWindow(QWindow* win) { m_window = win; }

In main.cpp -> int main([...]) main.cpp中-> int main([...])

qmlRegisterType<ExposeMask>("ExposeMask", 1, 0, "ExposeMask");

In main.qml main.qml中

ExposeMask {
    // I need to do it "onCompleted" for I need to set the window first...
    // You can change this by having the region as a member, exposing the window as property e.t.c
    Component.onCompleted: {
        setWindow(appwindow)
        mask = regionFromImage("mask.png")
    }
}

For handling the shapes it might be easier, if you have multiple windows with the right shape. 如果您有多个形状正确的窗口,则处理形状可能会更容易。 So in the case of your drawing I would use two windows, where one is rectangular, and only the protuberance needs to be masked. 因此,在您的图形中,我将使用两个窗口,其中一个是矩形的,并且仅需要遮盖突起。 So individual changes of their sizes are easier to calculate. 因此,其大小的单个变化更易于计算。

Also, if the calculations become complex, consider adding well suited helper functions to the exposed object in C++ 另外,如果计算变得复杂,请考虑在C ++中向暴露对象添加合适的帮助函数。

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

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