How to use GrabWindow? - c++

I have found a class GrabWindow but I am a noobie in Qt Quick 2 and can't get how to use it.
Can a someone explain how can I use this view?
At this moment I added in my main function:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include "grabwindow.h"
int main(int argc, char *argv[]) {
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
qmlRegisterType<GrabWindow>("test", 1, 0, "GrabWindow");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/qml/SystemTray.qml")));
if (engine.rootObjects().isEmpty()) {
return -1;
}
GrabWindow grabWnd;
grabWnd.setResizeMode(QQuickView::SizeViewToRootObject);
grabWnd.setSource(QUrl(QStringLiteral("qrc:/qml/main.qml")));
grabWnd.setFlags(Qt::Popup);
grabWnd.show();
return app.exec();
}
And my main.qml:
import QtQuick 2.9
import QtQuick.Controls 2.2
ApplicationWindow {
id: window
visible: true
width: 320
height: 480
title: qsTr("test")
}
But on start I get that only:
How to get a screenshot using that class and remove a square at the left top corner of the screen?
ADDED
I use Ubuntu 17.10

Related

Qt WebEngine url blocking

I would like to ask you a question, how to correctly block some url access. I would like to block access to URLs like "file:///" on QtWebEngine. I was searching something about WebUrlRequestInterceptor and QWebEngineUrlSchemeHandler but, did I find the right answer?
// main.qml
import QtQuick 2.0
import QtQuick.Window 2.0
import QtWebEngine 1.1
Window {
id: window
width: 800
height: 600
WebEngineView {
id: webView
anchors.fill: parent
url: "file:///"
}
}
// request.cpp
#include <QtCore/QRegExp>
#include "request.h"
WebUrlRequestInterceptor::WebUrlRequestInterceptor(QObject *p)
:QWebEngineUrlRequestInterceptor(p)
{
}
void WebUrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) {
if(!info.requestUrl().toString().contains(QRegExp("(?:https?|ftp)://\\S+")))
info.block(true);
}
// main.cpp
#include <QtGui/QGuiApplication>
#include <QtCore/QCoreApplication>
#include <QtQml/QQmlApplicationEngine>
#include <QtWebEngine/QQuickWebEngineProfile>
#include <QtWebEngine/QtWebEngine>
#include <QtQuick/QQuickWindow>
#include "request.h"
int main(int argc, char **argv) {
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
QGuiApplication app(argc, argv);
WebUrlRequestInterceptor *wuri = new WebUrlRequestInterceptor;
QQuickWebEngineProfile::defaultProfile()->setUrlRequestInterceptor(wuri);
QQmlApplicationEngine engine;
engine.load(QStringLiteral("./main.qml"));
QQuickWindow *window = qobject_cast<QQuickWindow*>(engine.rootObjects().first());
QRect rectangle;
rectangle.setWidth(800);
rectangle.setHeight(600);
window->setGeometry(rectangle);
window->show();
return app.exec();
}
Is it the right way, how I made it? I founded some flags here https://doc.qt.io/qt-5/qwebengineurlscheme.html#Flag-enum like QWebEngineUrlScheme::LocalAccessAllowed, but not sure, if this will help me. Isn't possible to control this list somehow https://admx.help/?Category=Chrome&Policy=Google.Policies.Chrome::URLBlocklist ?
Thank you

QQuickView as item in a QGraphicsScene

I would like to integrate a QQuickView in a QGraphicsView, but I only get a gray rectangle.
I know this is a horrible choice to integrate qml into qgraphicsview, but my team is working on this application for 3 years now and it is not possible to change the main QGraphicsView. I just want to know if there is a way to include some QML now.
Maybe there is a solution for this ? Maybe I just have to forget about it. Maybe you know. Thanks !
Code sample
main.cpp
#include <QApplication>
#include <QMainWindow>
#include <QtQuick/QQuickView>
#include <QGraphicsView>
#include <QGraphicsScene>
// Main
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene * scene = new QGraphicsScene;
QGraphicsView * view = new QGraphicsView(scene);
QQuickView * quickView = new QQuickView;
quickView->setSource(QUrl::fromLocalFile("test.qml")); // Displays a text
QWidget * container = QWidget::createWindowContainer(quickView);
container->setFixedSize(500, 100);
scene->addWidget(container);
scene->addEllipse(50, 50, 100, 100); // Just to know my scene is correctly drawn
view->show();
return a.exec();
}
test.qml
import QtQuick 2.15
Rectangle {
width: 500
height: 100
Text {
text: "This is a text in a QML view"
anchors.left: parent.left
anchors.top: parent.top
}
}

How to choose the OpenGL context for QtQuick applications

I am writing an QtQuick application, mainly in QML, but I have some parts that use OpenGL more directly (an OpenSceneGraph scene). For some reasons, I want to use OpenGL version >= 3.3, but Qt only choose a 3.0 implementation. My code is:
main.cpp:
#include <QApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
// Create the app.
QApplication app(argc, argv);
// Start the UI.
QQmlApplicationEngine engine;
engine.load(QUrl("main.qml"));
return app.exec();
}
main.qml:
import QtQuick 2.7
import QtQuick.Controls 2.2
ApplicationWindow {
id: root
width: 800
height: 600
title: "App"
visible: true
Label {
text: "OpenGL: " + OpenGLInfo.majorVersion + ' ' + OpenGLInfo.minorVersion + OpenGLInfo.profile
}
}
One way of doing it is to set the default QSurfaceFormat before the app.exec().
#include <QSurfaceFormat>
...
QSurfaceFormat surfaceFormat;
surfaceFormat.setMajorVersion(3);
surfaceFormat.setMinorVersion(3);
surfaceFormat.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(surfaceFormat);

Global functions or macros to get Qt version in QML code

How Can I get Qt version, for example 5.11.2 or similar, in my QML code. In my C++ code I have these options:
Method available on C++:
qVersion();
Macro available on C++:
QT_VERSION
But I couldn't find anything on QML?
You could use a Context Property, as explained here.
A simple example, given a simple qml file like this:
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
Text {
text: qtversion
}
visible: true
width: 640
height: 480
title: qsTr("Hello World")
}
Set the qtversion property at startup, in main function:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("qtversion", QString(qVersion()));
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}

Run QtVirtualKeyboard using QQuickWidget

I am trying to make the QtVirtualKeyboard example work with QQuickWidget instead of QQuickView. For QuickView, I use the following main.cpp code, which works fine for me:
#include <QQuickView>
#include <QGuiApplication>
#include <QQmlEngine>
int main(int argc, char *argv[])
{
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
QGuiApplication app(argc, argv);
QQuickView view(QString("qrc:/%2").arg(MAIN_QML));
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.show();
return app.exec();
}
I run into problems, when changing to QQuickWidgets with the following implementation of main.cpp:
#include <QQuickWidget>
#include <QApplication>
#include <QQmlEngine>
int main(int argc, char *argv[])
{
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
QApplication app(argc, argv);
QQuickWidget w(QString("qrc:/%2").arg(MAIN_QML));
w.setResizeMode(QQuickWidget::SizeRootObjectToView);
w.show();
return app.exec();
}
When I hit the input fields, the virtual keyboard shows up, but when I start typing at the keyboard, I get the message "input method is not set", which seems to be related to the input method plugin. No chars appear in the input fields.
Any ideas? The QML-code didn't change between the above variants of main.cpp
BTW: I am using Linux, gcc, Qt 5.9.0, EGLFS plugin
Thanks for any suggestions!
Regards,
Patrick
Found the solution for QML looking through inputMethod documentation. Following workaround works for me:
TextArea {
...
onActiveFocusChanged: {
if(activeFocus) {
Qt.inputMethod.update(Qt.ImQueryInput)
}
}
}
Works with other controls as well.
Of course InputPanel should be defined in ApplicationWindow like this:
ApplicationWindow {
...
InputPanel {
id: inputPanel
...
}
}