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

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!

Related

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

QWidget UI freezes when using a QQuickWidget

I am working on a software that uses both QQuickWidget and QWidget.
The main window is a QWidget that contains a QQuickWidget used to render stuff with OpenGL.
The QQuickWidget is in a QFrame that is sometimes displayed and sometimes hidden.
The problem is that when the QFrame is hidden, all the UI base on QWidget stops being updated
The weird thing is that only the UI freezes. If you know where to click, the software still work, only the UI is no longer updated. I think it might be that QML steals the rendering loop that is no longer accessible when it is hidden ?
I tested this on :
Linux, Qt 5.11.2 : No problem observed
Windows 10 64bit, Qt 5.9 : Problem as described above
Windows 10 64bit, Qt 5.11.2 : Problem as described above
Below is a small code example that can reproduce the problem. it works this way :
Homescreen is a QWidget. UI is updated on mouse hover (colors change)
on click, it switches to QQuickWidget
QML UI is updated on mouse hover (color change)
on click, it switches back to QWidget
The mouse hover no longer updates the UI (no color change)
main.cpp
#include <QApplication>
#include "myMainWindow.hpp"
int main(int argc, char **argv) {
QApplication application(argc, argv);
MyMainWindow window;
window.show();
window.raise();
return application.exec();
}
myMainWindow.hpp
#include <QMainWindow>
#include <QStackedLayout>
class MyMainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MyMainWindow(QWidget *parent = nullptr);
~MyMainWindow(void){}
private:
QStackedLayout *layout;
public slots:
void switchToWidget(void);
};
myMainWindow.cpp
#include <QApplication>
#include <QDebug>
#include <QFrame>
#include <QMainWindow>
#include <QPushButton>
#include <QtQuickWidgets/QQuickWidget>
#include <QQuickItem>
#include <QQmlProperty>
#include "myMainWindow.h"
MyMainWindow::MyMainWindow(QWidget *parent) : QMainWindow(parent) {
// Central Widget
QFrame *central = new QFrame;
central->setFixedSize(300,300);
this->setCentralWidget(central);
// BUTTON - QWidget
QPushButton *buttonWidget = new QPushButton("WIDGET", central);
buttonWidget->setStyleSheet("QPushButton{"
" background-color: red;"
"}"
"QPushButton:hover{"
" background-color: yellow;"
"}");
// BUTTON - QQuickWidget
QQuickWidget *buttonQML = new QQuickWidget(central);
buttonQML->setResizeMode(QQuickWidget::SizeRootObjectToView);
buttonQML->setSource(QUrl("qrc:/qml/ButtonQML.qml"));
buttonQML->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// Main Stack Layout
this->layout = new QStackedLayout(central);
this->layout->addWidget(buttonWidget);
this->layout->addWidget(buttonQML);
this->layout->setCurrentIndex(0);
// Stack siwtch
QObject::connect(buttonWidget, &QPushButton::clicked, [=](){
this->layout->setCurrentIndex(1);
});
QQuickItem *rootObject = buttonQML->rootObject();
QObject::connect(rootObject, SIGNAL(clicked()), this, SLOT(switchToWidget()));
}
void MyMainWindow::switchToWidget(void) {
this->layout->setCurrentIndex(0);
}
ButtonQML.qml
import QtQuick 2.0
import QtQuick.Controls 2.1
Button {
text: "ButtonQML"
id: root
background: Rectangle {
color: root.hovered ? 'green' : 'white'
}
}
Any idea what could cause this ?
I found the problem. Seems that I had not read the QQuickWidget Documentation well enough.
Note: Using QQuickWidget disables the threaded render loop on all platforms. This means that some of the benefits of threaded rendering, for example Animator classes and vsync driven animations, will not be available.
I changed my QQuickWidget to a QQuickView and everything is working.

Qt Data Visualization: How to integrate a Q3DScatter graph into a Widget in a Main Window?

I'm trying to display a Q3DScatter graph in a Main Window designed with Qt Designer so I've added a widget to the main window but I don't know how to embed the graph object in this widget. I tried to create a widget "container" programmatically and embed the graph in it and then putting the widget in a QHBoxLayout (which has been added to the main window using Qt Designer) using the following code in my main window's constructor:
...
Q3DScatter *graph = new Q3DScatter();
QWidget *container = QWidget::createWindowContainer(graph);
ui->horizontal_layout->addWidget(container, 1);
But the window appears to be empty when executing. I'd really appreciate some help.
EDIT: Here's the full code of my main window constructor:
ResultsWindow::ResultsWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ResultsWindow)
{
ui->setupUi(this);
Q3DScatter *graph = new Q3DScatter();
QWidget *container = QWidget::createWindowContainer(graph);
ui->horizontal_layout->addWidget(container, 1);
}
And here's the main code:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ResultsWindow w;
w.show();
return a.exec();
}
EDIT 2: I forgot to specify that the horizontal layout is embedded in a GridLayout. I tried to create a new project and my code actually works perfectly when I add a horizontal layout directly to the main window. So could the problem be due to the GridLayout?

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");

Create a window in qt with shape from an image

Can some one explain me how to make a window in qt according to the shape of some object in an image , for example i have an image of a tree , using that i need to create a window in the shape of a tree ..
After a long search , myself found a good solution , check out this ..
#include <QtGui>
class myMainWindow:public QMainWindow
{
public:
myMainWindow():QMainWindow()
{
setMask((new QPixmap("saturn.png"))->mask());
QPalette* palette = new QPalette();
palette->setBrush(QPalette::Background,QBrush(QPixmap("saturn.png")));
setPalette(*palette);
setWindowFlags(Qt::FramelessWindowHint);
QWidget *centralWidget = new QWidget(this);
QGridLayout *layout = new QGridLayout();
centralWidget->setLayout(layout);
QPushButton* button1 = new QPushButton("Button 1");
button1->setFixedSize(80,50);
layout->addWidget(button1,0,0);
setCentralWidget(centralWidget);
};
~myMainWindow(){};
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
myMainWindow *window = new myMainWindow();
window->resize(600, 316);
window->show();
return app.exec();
}
Here is a recipe for making a widget with a semi-transparent background colour. Just expand from there by making the background fully transparent, then display the tree image on top of that as a background image. Note that the widget will still behave like a rectangular widget in regards to laying out its child elements, so you probably need to deal with this using some custom layout inside the tree shape.
Start from the docs for QWidget::setMask. It has a version which takes a QBitmap and one that takes a QRegion. This is the fundamental function in getting a transparent widget. The toolkit also includes a clock example using the QRegion version -- I suspect a bitmap is just as easy though.