QQuickView as item in a QGraphicsScene - c++

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

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

QPlainTextEdit Refuses to Change its Background Color (Qt / C++)

I was trying to change the background color of a QPlainTextEdit widget to black and apply a glow effect to it. But when I apply a DropShadowEffect to a QTextEdit or a QPlainTextEdit widget, its background color reverts back to the original and refuses to change. I'm using Qt version 5.12.2. Here's the code:
#include <QtWidgets>
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QMainWindow window;
window.setWindowTitle("Title");
window.setStyleSheet("QMainWindow{background-color: #191b21}");
window.setFixedSize(800, 600);
window.show();
QPlainTextEdit* w = new QPlainTextEdit(&window);
w->setGeometry(250, 50, 300, 50);
w->setStyleSheet("QPlainTextEdit{background-color: black; color: white;}");
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect();
effect->setColor("#59f1ff");
effect->setBlurRadius(30);
effect->setOffset(.0);
w->setGraphicsEffect(effect);
w->show();
return app.exec();
}
I've tried using QPalette but it won't work too. What am I doing wrong?

QQuickView window freezes upon mouse resize

I have problem resizing the window using this example. It works, but unpredictable when it becomes unresponsive. Window minimization and maximization works every time, but resizing with the mouse causes weird, frozen behavior. Happens on Windows 10 using Qt 5.12.0. What I do wrong here?
Thanks!
#include <QGuiApplication>
#include <QQuickView>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView view;
view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
view.show();
return app.exec();
}
And this is the main QML code:
import QtQuick 2.10
Rectangle {
anchors.fill: parent
color: "red"
}
Ok, I have figured out the solution:
anchors.fill: parent should not be used in the root element, but instead we have to tell to the view in C++ the following:
view.setResizeMode(QQuickView::SizeRootObjectToView);

Qt/Qml: how to make widget (createWindowContainer) fill parent widget

I have this problem for months. I try to implement a webview on iOS with QtWebView by Qml and embed the webview into a widget by QWidget::createWindowContainer (following a online tutorial). But I just can't get this widget to fill the parent widget, nor change its size at all.
I have write the following minimal example for explaining the problem:
main.cpp
#include <QApplication>
#include <QQuickView>
#include <QBoxLayout>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *mainwindow = new QWidget();
mainwindow->setStyleSheet("background-color:red;"); // so that we can see the problem more clearly
QHBoxLayout *layout = new QHBoxLayout(mainwindow);
QQuickView *view = new QQuickView();
QWidget* container = QWidget::createWindowContainer(view, mainwindow);
// container->setMinimumSize(200, 200);
// container->setMaximumSize(200, 200); // the displaying size doesn't change with or without these lines
// container->setFocusPolicy(Qt::TabFocus);
view->setSource(QUrl("qrc:///webview.qml"));
layout->addWidget(container);
mainwindow->setLayout(layout);
mainwindow->show();
return a.exec();
}
webview.qml
import QtQuick 2.2
import QtWebView 1.0
Item {
visible: true
anchors.fill: parent
WebView {
anchors.fill: parent
url: "https://www.google.de"
}
}
When I run this mini program on iphonesimulator in QtCreator, the layout is like the following screenshots (sorry I can't embed the pictures yet):
We can see the container doesn't fill the top area. And even when the parent widget is much smaller (in my actual program), the container has always this same size. When I rotate the iphonesimulator, it looks then like this!.
Could anyone please help to solve this? It's annoying me for months. Thanks!

Qml / Qt / C++ : QQuickView in a QWidget - Need Background Transparency

I am trying to get my container with a qquickview in it to be transparent, and only display the elements from the QML file without the background.
Is there a nicer way to implement this? Here is the relevant code, you can see I have commented out adding the QML to it so that the only offender is the QWidget containing a QQuickView
The QML will make a menu similar to Apple's "Cover Flow" using only text. I only want the text it generates to be visible.
Currently it has a solid white background.
QQuickView *view = new QQuickView();
QWidget *container = QWidget::createWindowContainer(view, this);
container->setObjectName("wrappingContainer");
container->setMinimumSize(1000, 240);
container->setMaximumSize(1000, 240);
container->setFocusPolicy(Qt::TabFocus);
//view->setSource(QUrl("qrc:/qml/wrappingMenu.qml"));
ui->testLayout->addWidget(container);
Try QQuickWidget:
#include <QtWidgets>
#include <QQuickWidget>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget() {
setStyleSheet("background-color: 'grey';");
mQQuickWidget = new QQuickWidget(QUrl(QStringLiteral("main.qml")), this);
mQQuickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
mQQuickWidget->setAttribute(Qt::WA_AlwaysStackOnTop);
mQQuickWidget->setClearColor(Qt::transparent);
mQQuickWidget->resize(400, 400);
mQQuickWidget->raise();
}
private:
QQuickWidget *mQQuickWidget;
};
int main(int argc, char** argv)
{
QApplication app(argc, argv);
Widget widget;
widget.resize(400, 400);
widget.show();
return app.exec();
}
#include "main.moc"
main.qml:
import QtQuick 2.2
Item {
Text {
text: "Qt Quick Text"
font.pixelSize: 32
anchors.centerIn: parent
}
}
I doubt it could be done for a QQuickView embedded in a QWidget. But you can have a transparent QQuickView like :
QQuickView view;
view.setSurfaceType(QSurface::OpenGLSurface);
QSurfaceFormat format;
format.setAlphaBufferSize(8);
format.setRenderableType(QSurfaceFormat::OpenGL);
view.setFormat(format);
view.setColor(QColor(Qt::transparent));
view.setClearBeforeRendering(true);
view.setFlags(Qt::FramelessWindowHint);
view.setSource(QStringLiteral("qrc:/qml/wrappingMenu.qml"));
view.show();
You can do it with:
QQuickView view;
view.setColor(Qt::transparent);
view.setSource("main.qml");