我有一个我创建的自定义QQuickItem
,我想创建一个圆角窗口。所以我实现了 aQQuickPaintedItem
并导出到QML
. 问题是该项目的子项正在通过该项目的边界矩形扩展,该矩形是一个矩形,而不是我想要的圆角矩形。这是它的外观:
这是我的代码:
main.qml
import QtQuick 2.7
import QtQuick.Window 2.2
import mycustomlib 1.0
Window {
id: wnd
width: 300
height: 280
visible: true
flags: Qt.FramelessWindowHint
color: "transparent"
x: 250
y: 250
MyCustomWindow {
id: playerFrame
anchors.fill: parent
radius: 25
Rectangle {
color: "beige"
anchors.margins: 5
anchors.fill: parent
}
}
}
主文件
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QUrl>
#include "mycustomwindow.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<MyCustomWindow>("mycustomlib", 1, 0, "MyCustomWindow");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
我的自定义窗口.h
#ifndef MYCUSTOMWINDOW_H
#define MYCUSTOMWINDOW_H
#include <QQuickPaintedItem>
class MyCustomWindow : public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY(int radius READ radius WRITE setRadius NOTIFY radiusChanged)
public:
MyCustomWindow(QQuickItem *parent = 0);
int radius() const;
void setRadius(int radius);
signals:
void radiusChanged();
protected:
virtual void paint(QPainter *pPainter) Q_DECL_OVERRIDE;
private:
int m_radius;
};
#endif // MYCUSTOMWINDOW_H
我的自定义窗口.cpp
#include "mycustomwindow.h"
#include <QPainter>
MyCustomWindow::MyCustomWindow(QQuickItem *parent) :
QQuickPaintedItem(parent),
m_radius(0)
{
setAcceptedMouseButtons(Qt::AllButtons);
}
void MyCustomWindow::paint(QPainter *pPainter)
{
const QRect myRect(0, 0, width() - 1, height() - 1);
pPainter->fillRect(myRect, Qt::transparent);
pPainter->drawRoundedRect(myRect, m_radius, m_radius);
}
int MyCustomWindow::radius() const
{
return m_radius;
}
void MyCustomWindow::setRadius(int radius)
{
m_radius = radius;
emit radiusChanged();
}
我想要的是 childRectangle
被我的自定义形状剪裁(在这种情况下是圆角矩形。像这样的东西:
是否有可能实现这样的目标QML
?