When I create a new project in Qt, Qt use QQmlApplicationEngine to load qml file, rootObject in qml file is Window, like :
Window {
id: root
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Component.onCompleted: {
root.showFullScreen()
}
}
And I can use method showFullScreen to display it full my screen (full my monitor)
But, if I use QQuickView to show qml file, I can't make it full screen, I don't find any function with the same, I use like that:
QQuickView view;
view.setSource(QUrl("qrc:/main.qml"));
view.setWindowState(Qt::WindowFullScreen);
view.setResizeMode(QQuickView::SizeRootObjectToView); //even when i call this method, it doesn't still work
view.show();
qml file with this case:
Rectangle{
// rectangle has with, hight, it still doesn't work
color: "green"
}
Please guide me how to display full screen with QQuickView ? Thanks so much, I appreciate with any help !
CodePudding user response:
In my opinion, there's no much difference setting main window to full-screen for QQuickView
. It inherits QWindow
and also has showFullScreen
method. Therefore you can invoke this method from c
instead of your view.show();
. If, on the other hand, you need to switch to full-screen from QML then you have to expose your QQuickView
object to QML, as described here:
#include <QGuiApplication>
#include <QQuickView>
#include <QQmlEngine>
#include <QQmlContext>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView mainView;
// expose mainView to QML, see
// https://doc.qt.io/qt-6/qtqml-cppintegration-contextproperties.html
mainView.setObjectName("mainView");
mainView.engine()->rootContext()->setContextProperty("mainView", &mainView);
mainView.show(); // we call mainView.show() before loading the qml file in
// order to allow to override mainView.show() with mainView.showFullScreen()
mainView.setSource(QUrl("qrc:/main.qml"));
return app.exec();
}
Here's also the code of main.qml
for reference:
import QtQuick 2
import QtQuick.Controls 2
Rectangle {
id: rect
color: "green"
anchors.fill: parent
Button {
id: toggleFullScreen
property bool isFullScreen: true
text: "toggle full screen"
anchors.centerIn: parent
onPressed: {
toggleFullScreen.isFullScreen = !toggleFullScreen.isFullScreen
executeFullScreen()
}
}
function executeFullScreen() {
if(toggleFullScreen.isFullScreen)
mainView.showFullScreen()
else
mainView.showNormal()
}
Component.onCompleted: {
executeFullScreen()
}
}
CodePudding user response:
Use view.showFullScreen();
instead of view.setWindowState(Qt::WindowFullScreen);
QQuickView view;
view.setSource(QUrl("qrc:/main.qml"));
view.showFullScreen();