kar*_*lip 22 c++ qt transparent qml qt-quick
有没有办法让qml应用程序的窗口透明?
我正在寻找有关如何使用qml绘制简单形状的详细说明,同时使应用程序的窗口透明,以及背景.一个工作源代码演示将是非常棒的.
hid*_*bit 22
这是一个简单的例子:
main.cpp:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Run Code Online (Sandbox Code Playgroud)
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QDeclarativeView>
class MainWindow : public QDeclarativeView
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // MAINWINDOW_H
Run Code Online (Sandbox Code Playgroud)
mainwindow.cpp:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QDeclarativeView(parent)
{
// transparent background
setAttribute(Qt::WA_TranslucentBackground);
setStyleSheet("background:transparent;");
// no window decorations
setWindowFlags(Qt::FramelessWindowHint);
// set QML file
setSource(QUrl("main.qml"));
}
MainWindow::~MainWindow()
{
}
Run Code Online (Sandbox Code Playgroud)
main.qml
import QtQuick 1.0
Rectangle {
id: root
width: 250
height: 250
// completely transparent background
color: "#00FFFFFF"
border.color: "#F00"
border.width: 2
Rectangle {
id: ball
height: 50; width: 50
x: 100
color: "#990000FF"
radius: height / 2
}
SequentialAnimation {
running: true; loops: Animation.Infinite
NumberAnimation { target: ball; property: "y"; to: root.height - ball.height; duration: 1000; easing.type: Easing.OutBounce }
PauseAnimation { duration: 1000 }
NumberAnimation { target: ball; property: "y"; to: 0; duration: 700 }
PauseAnimation { duration: 1000 }
}
}
Run Code Online (Sandbox Code Playgroud)
transp-qml.pro
QT += core gui declarative
TARGET = transp-qml
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
OTHER_FILES += main.qml
Run Code Online (Sandbox Code Playgroud)
结果截图:

Tim*_*mmm 17
至少从Qt 5.3开始,你不需要像以前的答案那样精心设计:
Window {
flags: Qt.ToolTip | Qt.FramelessWindowHint | Qt.WA_TranslucentBackground
color: "#00000000"
Run Code Online (Sandbox Code Playgroud)
任务完成.(你可能想要改变ToolTip.我正在使用它,因为我正在制作工具提示.)
kar*_*lip 10
我终于找到了一种简单的方法来绘制几个红色/蓝色矩形,同时让窗口保持透明.

draw_rectangles.qml
import Qt 4.7
Item {
Rectangle {
opacity: 0.5
color: "red"
width: 100; height: 100
Rectangle {
color: "blue"
x: 50; y: 50; width: 100; height: 100
}
}
}
Run Code Online (Sandbox Code Playgroud)
win.cpp:
#include <QApplication>
#include <QDeclarativeView>
#include <QMainWindow>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow window;
QDeclarativeView* v = new QDeclarativeView;
window.setCentralWidget(v);
v->setSource(QUrl::fromLocalFile(("draw_rectangles.qml")));
window.setStyleSheet("background:transparent;");
window.setAttribute(Qt::WA_TranslucentBackground);
window.setWindowFlags(Qt::FramelessWindowHint);
window.show();
return app.exec();
}
Run Code Online (Sandbox Code Playgroud)
win.pro:
TEMPLATE += app
QT += gui declarative
SOURCES += win.cpp
Run Code Online (Sandbox Code Playgroud)
将这些文件保存到同一目录并执行,qmake然后make编译应用程序.
| 归档时间: |
|
| 查看次数: |
16965 次 |
| 最近记录: |