QML设置圆角窗体

先看一个失败的案例,那黑色的就是主窗体,我已经找遍全网但依然是这样子。

在这里插入图片描述

下面贴上失败的代码

c++代码

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickWindow>
int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
 
    QGuiApplication app(argc, argv);
    
    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
                         if (!obj && url == objUrl)
                             QCoreApplication::exit(-1);
                     }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

QML代码

import QtQuick 2.13
import QtQuick.Window 2.13

Window {
    visible: true
    width: 640
    height: 480
    flags: Qt.FramelessWindowHint
    title: qsTr("Hello World")
    color: "transparent"
    Rectangle{
        anchors.fill:parent
        anchors.margins: 30
        radius:30
        opacity:1
        color: "white"
    }
}

后来在浩瀚的搜索结果里翻到第10页左右的时候找到了一篇帖子中提到了一个属性,在main.cpp内加上这行代码以后就OK了。

//QQuickWindow::setSceneGraphBackend(QSGRendererInterface::Software);

最后贴上一张OK的图片

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42485732/article/details/126438285
今日推荐