2011-09-30 23 views
20

有沒有辦法讓一個qml應用程序的窗口透明?如何用Qt Quick製作透明窗口?

我在尋找一個的詳細說明關於如何使用qml繪製簡單的形狀,同時使應用程序的窗口透明,以及背景。一個有效的源代碼演示將非常棒。

+1

我是你的粉絲:) – Avelino

回答

10

我終於找到了一個簡單的方法來繪製幾個紅色/藍色矩形,同時讓窗口保持透明。

enter image description here

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 
     } 
    } 
} 

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(); 
} 

win.pro

TEMPLATE += app 
QT += gui declarative 
SOURCES += win.cpp 

這些文件保存到同一個目錄,然後執行qmake隨後make編譯應用程序。

+0

-1:雖然是一個很好的演示,但它沒有解釋任何東西。透明度的先決條件是什麼?簡而言之:要實現它的工作,需要做些什麼?因此,我認爲它將來不會有用,但至多目前是這樣。 - 編輯:特別是關於你的問題:「我正在尋找一個__detailed描述_」。 –

21

下面是一個簡單的例子:

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(); 
} 

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 

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() 
{ 
} 

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 } 
    } 
} 

transp-qml.pro

QT += core gui declarative 

TARGET = transp-qml 
TEMPLATE = app 


SOURCES += main.cpp\ 
      mainwindow.cpp 

HEADERS += mainwindow.h 

OTHER_FILES += main.qml 

截圖結果:

screenshot

+0

謝謝!我會+1,但這是一個複雜的演示和** qmake **抱怨'警告:未能找到:main.cpp'。 – karlphillip

+1

@karlphillip:糟糕,只是忘了發佈'main.cpp';) – hiddenbit

13

由於至少Qt的5.3,你不需要任何的複雜的如以前的答案:

Window { 
    flags: Qt.ToolTip | Qt.FramelessWindowHint | Qt.WA_TranslucentBackground 

    color: "#00000000" 

完成任務。 (您可能需要更改ToolTip。我正在使用它,因爲我正在提供工具提示。)

+1

值得一提的是,這個解決方案只能在main.cpp中設置DefaultAlphaBuffer:'QQuickWindow :: setDefaultAlphaBuffer(true);' –

1

我在Q ++ 5.3中同時使用C++和QML,發現我需要撥打QQuickWindow::setDefaultAlphaBuffer。這必須在創建第一個QQuickWindow之前完成,因此在C++中不是QML。窗口colorflags很可能在QML設置,但我選擇把所有的代碼winow透明度在一個地方像這樣:

QQuickView view; 
QQuickWindow::setDefaultAlphaBuffer(true); 
view.setColor(Qt::transparent); 
view.setFlags(m_View.flags() | 
       static_cast<Qt::WindowFlags>(Qt::WA_TranslucentBackground));