QT QML not resizing dynamically - qt

I am fairly new to QT and would need some help with qml dynamic resizing. I am using Visual Studio with QT plugin, MSVC 2017 compiler.
Here is my qml file:
import QtQuick 2.0
import QtQml.Models 2.1
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.11
Rectangle
{
anchors.fill: parent
// or width/height here
Rectangle
{
anchors.fill: parent
color: "blue"
}
}
My custom QQuickWidget:
customWidget::customWidget(QWidget *parent)
{
this->setParent(parent);
this->setGeometry(QRect(QPoint(0, 0), parent->size())); //this is for debugging only
this->setResizeMode(QQuickWidget::SizeRootObjectToView);
this->setSource(QUrl::fromLocalFile("qmlpath"));
}
Widget Initialization
myWidget = new customWidget(ui.widgetArea); // widgetArea is an empty QWidget from the Qt Designer
qDebug() << myWidget->errors();
QVBoxLayout* layout = new QVBoxLayout(myWidget);
ui.widgetArea->setLayout(layout);
I have tried other suggestions like enclosing the rectangles in a grid layout for example and use "layout.fillWidth: true" etc.
Everything gives me a blank space (widget size of 0, 0) instead of a blue rectangle, unless I define a fixed width and height for it. Is there anything that I'm missing?

Related

QML: Qt.labs.platform.MenuItem's icon

There's a plugin called Qt.labs.platform. Among other things, it provides tray icon with a menu. That menu has a list of MenuItems. Any menu item may have an icon, which would be displayed on the left side of item's text.
There's two ways of setting the icon, none of which work for me:
1) Version 1.0 defined iconSource and iconName properties.
Silently does not work, just shows no icon.
2) Revision 1.1 (declared as Q_REVISION(1)) introduces icon.name, icon.source and icon.mask "sub-properties" (not sure what's the right name for it?)
Fails QML engine with a message:
"MenuItem.icon" is not available in Qt.labs.platform 1.1.
I tried both import Qt.labs.platform 1.1 and 1.0.
Am I missing something in the mechanics of QML revisions or this is a bug in Qt?
A MenuItem is declared in qquickplatformmenuitem_p.h and defined in qquickplatformmenuitem.cpp files.
I'm using ArchLinux, KDE/Plasma. Some other apps (like electron-based) do have their icons in menu showing properly.
UPD Reported as a Qt bug.
I don't know what exactly you are doing to achieve your goal, because there is no minimal reproducible example.
But take a look at here
import QtQuick 2.12
import QtQuick.Window 2.12
// import Qt.labs.platform 1.1
import QtQuick.Controls 2.12
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle {
width: 200
height: 200
anchors.centerIn: parent
MenuBar {
id: menuBar
Menu {
id: firstMenu
MenuItem {
icon.name: "download"
icon.source: "icons/cloud-down-icon.png"
}
MenuItem {
text: qsTr("Rewrap Paragraph")
onTriggered: rewrapParagraph()
}
}
}
}
}
Note: put your icon inside the icons folder in the project folder.
If you click on the menu then you should get a dropdown with two items, first one is just an icon without the text (you can include some text by yourself), second item is just a text.

Is it possible to use a QML Singleton as a header Item inside a QML Page?

I have a StackView that displays several pages of type "TabletPage.qml" :
// TabletPage.qml
import QtQuick 2.0
import QtQuick.Controls 2.0
import QtQuick.Window 2.2
import QtQml.Models 2.1
// import TopBar.Singleton 1.0
Page {
id: root
property ObjectModel menuBarModel
signal homePageRequested()
signal backPageRequested()
signal pageRequested(string url)
signal pageRequestedWithProp(string url, var prop)
width: Screen.width
height: Screen.height
background: Rectangle { color: "black" }
header: TopBar {} // Ok but each Page has its own TopBar
// header : TopBarSingleton // -> crash the UI, does not work
footer: HorizontalMenuBar { model: root.menuBarModel }
}
Each TabletPage has its own footer (a custom QML component HorizontalMenuBar) and a header (a custom QML TopBar) but now i need to make the header "global" : all pages should refer to the same QML instance of TopBar.qml. I have tried to make the TopBar a Singleton (using the qmlRegisterSingletonType method) but how then i can refer to it in the header of each Page ?
Is the singleton approach even possible in this case ?
Thank you.

Dynamically add Menu in MenuBar of ApplicationWindow in QML

I'm trying to create an application that is extensible with plugins. Now the plugins should be able to add a Menu dynamically in the MenuBar.
From documentation I can find a MenuBar that is provided by QtLabsPlatform. This has a method addMenu. But Windows was not in the list of supported platforms. So I cannot benefit from it.
I tried the placeholder technique suggested in Error adding a Menu in QML, but this does not work with QtQuick.Controls 2.13
In the #timday answer in the question you indicate indicates the answer but does not show an example:
...
Dynamic creation of Menus is a little harder; see Qt.createQmlObject
or Qt.createComponent docs. (It may be simpler to just declare all the
ones you need in your code, but with their visible property wired to
whatever logic is appropriate). ...
(emphasis mine)
So my answer is just to show you how to do it although I think you want to add MenuItem to a Menu dynamically, instead of a Menu to a MenuBar:
import QtQuick 2.13
import QtQuick.Controls 2.13
ApplicationWindow {
id: root
width: 640
height: 480
visible: true
menuBar: MenuBar {
Menu {
id: plugins_menu
title: qsTr("&Plugins")
}
}
function onTriggered(item){
console.log(item.text)
}
Component.onCompleted:{
var plugin_names = ["plugin1", "plugin2", "plugin3"]
for(var i in plugin_names){
var item = Qt.createQmlObject('import QtQuick 2.13; import QtQuick.Controls 2.13; MenuItem {}',
plugins_menu)
item.text = plugin_names[i]
plugins_menu.addItem(item)
var f = function(it){
it.triggered.connect(function (){ root.onTriggered(it)
})}
f(item)
}
}
}

How to use a GridLayout in combination with exposing data property

I am trying to wrap GridLayout inside an Item and exposing the GridLayout's data property as the default property of the item. But this results in two problems.
I get a crash when exiting the application.
This may in fact be a bug in Qt itself and it might also already been fixed, if not I will report it after fixing 2. I am only able to test on Windows 7 using Qt 5.7.0 MSVC2015 32.bit at the moment.
How to use the attached Layout property? Take look in the following example, which results in the error:
Non-existent attached object
on line
"Layout.alignment: Qt.AlignBottom | Qt.AlignRight".
Example:
//MyCustomLayout.qml
import QtQuick 2.7
import QtQuick.Layouts 1.3
Item {
default property alias data: layout.data
//Some other QML components not to be within GridView here.
GridLayout {
id: layout
anchors.fill: parent
}
//Some other QML components not to be within GridView here.
}
//main.qml
import QtQuick.Controls 2.0
ApplicationWindow {
id: root
visible: true
height: 1024
width: 768
MyCustomLayout {
anchors.fill: parent
Button {
Layout.alignment: Qt.AlignBottom | Qt.AlignRight
}
}
}

Adding QQuickWidget in QStackedWidget

I am trying to add a qquickwidget along with some other qwidgets in qstackedwidget. But when I am trying to set the current widget to the qquickwidget nothing appears on the window. Is there something else that need to be done? I am also setting the view property of the qquickwidget to true
QQuickWidget* mRoom = new QQuickWidget;
connect(mRoom, SIGNAL(statusChanged(QQuickWidget::Status)), this, SLOT(StatusChanged(QQuickWidget::Status)));
mRoom->setSource(QUrl::fromLocalFile("C:/Users/visjain/Desktop/main_vishwas.qml"));
mRoom->setResizeMode(QQuickWidget::SizeRootObjectToView);
QStackedWidget* mStack = new QStackedWidget(mparent);
mStack->addWidget(mRoom);
mStack->setCurrentWidget(mRoom);
mRoom->show();
qml code -
import QtQuick 2.5
import QtQuick.Window 2.2
Window {
visible: true
height: 1000
width: 1800
Rectangle{
height: parent.height
width: parent.width
color: "red"
}
}
Did you assign some QML file to the widget?
QQuickWidget *view = new QQuickWidget;
view->setSource(QUrl::fromLocalFile("myqmlfile.qml"));
view->show();
Some more source code might be helpful.
For some more detailed reference you might see this.
For putting a QWidget inside a QStackedWidget to the front you should use setCurrentIndex or setCurrentWidget. See this.

Resources