问题描述
我正在尝试创建一个带有无框窗口和圆角的简单 Qt UI.从具有 QtQuick 2 Application 模板的新项目开始,我的代码如下所示:
I am trying to create a simple Qt UI with a frameless window and rounded corners. Starting from a new project with the QtQuick 2 Application template, my code looks like this:
main.cpp
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/qtquick-test/main.qml"));
viewer.setFlags(Qt::FramelessWindowHint);
viewer.showExpanded();
return app.exec();
}
main.qml
import QtQuick 2.0
Rectangle {
width: 360
height: 360
radius: 10
color: "red"
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
}
结果如下:
我不想做的是通过使主窗口透明来摆脱白色角落.但是,据我所知,Qt5 中没有办法做到这一点,因为我们没有样式表等,而且我没有使用 QtWidget.我应该使用 QtWidget 吗?
What I wan't to do is get rid of the white corners, by making the main window transparent. However, as far as I can tell there is no way in Qt5 to do this, because we don't have stylesheets, etc and I am not using a QtWidget. Should I use a QtWidget?
顺便说一句,我是 Qt 和 Qt5 的新手.
Btw, I'm new to Qt and Qt5.
推荐答案
这适用于我在 Windows 8 和 Ubuntu 12.04 下.
This works for me under Windows 8 and Ubuntu 12.04.
import QtQuick 2.3
import QtQuick.Window 2.2
Window {
width: 300
height: 300
flags: Qt.FramelessWindowHint | Qt.Window
color: "transparent"
Rectangle {
color: "brown"
anchors.fill: parent
anchors.margins: 10
}
}
这篇关于Qt5&QtQuick2 - 透明主窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!