QQuickView window freezes upon mouse resize - c++

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

Related

How to hide QMainWindow and show splashcreen during startup?

I am trying to hide the MainWindow of my Qt desktop app during startup, and to show a splashscreen. Both only happens after the loading phase, even though I call both splash.show() and window.hide() before the loading phase. I tried to split loading phase and constructor, but result is the same. How can I achieve both before the loading phase ?
Update 1
To display the splash screen, I had to add a call to QApplication::processEvents()
Update 2
The black window was actually not the MainWindow, but a ghost window that popped because scrollArea->setVisible(true) was called in the constructor.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPixmap pixmap(QStringLiteral(":/ressources/icons/icon.png"));
QSplashScreen splash(pixmap);
splash.show();
MainWindow window; // this loads for 5-6 seconds
a.processEvents();
window.showLoginPrompt();
splash.finish(&window);
return a.exec();
}
Based on your code and some example I could make it run like you are trying to do.
You only need to call your promptLogin function instead.
#include <QApplication>
#include <QTimer>
#include <QSplashScreen>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSplashScreen *splash = new QSplashScreen;
splash->setPixmap(QPixmap("D:\\Projects\\SplashScreen\\TestSplashScreen\\splash.png"));
splash->show();
MainWindow mainWin;
QTimer::singleShot(2500, splash, SLOT(close()));
QTimer::singleShot(2500, &mainWin, SLOT(show()));
return app.exec();
}

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

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

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!