Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt5 -porting a simple Qt Quick application to Qt5. help needed

Tags:

qt

qt5

qt-quick

qml

I am trying to port a simple Qt Quick application from Qt4.8 to Qt5.0beta. my initial (Qt4.8) code is similar to what is below:

main.cpp will dipsplay a QDeclarativeView in a frameless window with translucent background

#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QtDeclarative>
#include <QDeclarativeContext>

Q_DECL_EXPORT int main(int argc, char *argv[])
{
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    QmlApplicationViewer viewer;
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile(QLatin1String("qml/main.qml"));

    viewer.setWindowFlags(Qt::FramelessWindowHint);

    viewer.setAttribute(Qt::WA_TranslucentBackground);
    viewer.setStyleSheet("background:transparent;");
    viewer.setResizeMode(QDeclarativeView::SizeViewToRootObject);
    viewer.showExpanded();

    return app->exec();
}

and the main.qml will just display a red rectangle inside another transparent rectangle.

import QtQuick 1.1

Rectangle {
    width: 360
    height: 360
    color: "transparent"
    Rectangle
    {
     x: 125
     y: 122
        width: 110
        height: 116
        anchors.centerIn: parent
        color: "red"
  radius: 27
    }

    Text {
     color: "black"
        text: qsTr("Press me!")
        font.pointSize: 14
        anchors.centerIn: parent
    }

    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }
}

In order to make it compatible with Qt5 few changes were necessary. (I have followed http://qt-project.org/doc/qt-5.0/portingqmlapp.html [qt-project.org] in order not to miss something)

Now main.cpp is similar to:

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QtWidgets/QtWidgets>
#include <QtWidgets/QLabel>

int main(int argc, char *argv[])
{

    QGuiApplication app(argc, argv);
    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("qml/main.qml"));
    viewer.setWindowFlags(Qt::FramelessWindowHint);
    viewer.showExpanded();


    return app.exec();
}

In main.qml only one line was replaced.

import QtQuick 2.0

but I am not able to find a way of keeping the transparency

setAttribute(Qt::WA_TranslucentBackground);
setStyleSheet("background:transparent;");

setAttribute and setStyleSheet were available for a QDeclarativeView (which is a QWidget) but not for QQuickView (which is a QWindow)

like image 794
thenootz Avatar asked Nov 04 '22 09:11

thenootz


1 Answers

Try my QuickWidget, if it helps you:

http://code.google.com/p/quickwidget/

Or, even better, use QQuickWidget from Qt 5.3+.

like image 115
user1095108 Avatar answered Nov 15 '22 05:11

user1095108