PDA

View Full Version : QQuickWidget with QML inside. Show another QML above but not in another window



ddonate
22nd November 2016, 15:27
0
down vote
favorite
I have a QQuickWidget (MovableWidget) with a QML inside (QML1: Test.qml)

This QML has a button, and when clicked I want to show/hide another QML (Test2.qml) above QML2, but not in another window (in Windows, both panels must be in the same 'Task bar' window). QML1 must keep the position in both cases, with QML1 visible or hidden

I tried to add a QML2 instance inside QML1, setting it above, but I can not paint outside QML1 boundaries. So I suppose I must increase QML1 size, and so TestWidget size, but in this case the best thing I have achieved is that the window is increased but to the bottom...

main.cpp

.
..
MovableWidget *view = new MovableWidget;
view->setSource(QUrl("qrc:/Test.qml"));
view->setWindowFlags(Qt::FramelessWindowHint);
view->show();
if (view->rootObject())
QObject::connect(view->rootObject(), SIGNAL(signal_showMenu(bool)), view, SLOT(onMenuShown(bool)));
...

MovableWidget.cpp


#include "movableWidget.h"

#include <QMouseEvent>

// ************************************************** **************************
MovableWidget::MovableWidget(QWidget *parent)
: QQuickWidget(parent)
{
}

// ************************************************** **************************
void SptMiniWindow::onMenuShown(bool bShown)
{
// setGeometry() here? parameters??
}

Test1.qml


import QtQuick 2.0

Rectangle {
id: myWindow

signal signal_showMenu(bool show)

width: 250; height: 100
color: "red"

Button {
id: idButtonClick

anchors { bottom: parent.bottom; bottomMargin: 10; horizontalCenter: parent.horizontalCenter }

height: 20
width: 50

text: "click"

onClicked: {
console.log("idButtonClick");
test2.visible = !test2.visible

// Here 'myWindow' height must be changed?

signal_showMenu(test2.visible)
}

Test2 {
id: test2
anchors { bottom: myWindow.top; left: myWindow.left; right: myWindow.right; }
height: 50
visible: false
}
}

Test2.qml


import QtQuick 2.0

Rectangle {
color: "green"
}

anda_skoa
22nd November 2016, 20:52
What do you need to call into C++ for?
Do you want to load a different QML file into the view? I.e. do you want to replace the window's content?

Cheers,
_

ddonate
24th November 2016, 10:51
What I wanted is to display a QML object above another QML object.
Finally I have used a QML 'Window' object to set my QML object 2 above my QML object 1.
I load the QML2 object dinamically (Qt.createComponent) in my QML1 I added "flags: Qt.Tool | Qt.FramelessWindowHint" to remove frame and avoid a new Windows window (what I needed)
Thanks!

anda_skoa
24th November 2016, 13:13
Ok, so another window.

Instead of the script based approach to dynamic loading you might also want to look at the Loader (http://doc.qt.io/qt-5/qml-qtquick-loader.html) element.

Cheers,
_