QML: how to hide submenu? - qt

I have some menu and this menu contains submenu. In some cases this submenu should be visible. In other cases this submenu should be invisible. How to do this?
I try to use visible property, but it does not work. The submenu is always visible. In code example below submenu should be visible if we click left mouse button and invisible if we click right mouse button. But it is visible in both cases.
import QtQuick 2.13
import QtQuick.Window 2.13
import QtQuick.Controls 2.13
Window {
visible: true
width: 640
height: 480
property bool visibleSubMenu : false
Menu {
id: contextMenu
MenuItem {
text: "Menu item"
}
Menu {
title: "Sub menu"
visible: visibleSubMenu
MenuItem {
text: "Sub menu item"
}
}
}
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.RightButton
onClicked: {
if (mouse.button === Qt.RightButton)
visibleSubMenu = false;
else
visibleSubMenu = true;
console.log(visibleSubMenu)
contextMenu.popup()
}
}
}

The Menu(QQuickMenu) is an element that saves the information but it is not a visual element, the visual element is its parent that is a MenuItem(QQuickMenuItem) so you must hide the parent:
property bool visibleSubMenu : true
onVisibleSubMenuChanged: sub_menu.parent.visible = visibleSubMenu
Menu {
id: contextMenu
MenuItem {
text: "Menu item"
}
Menu {
id: sub_menu
title: "Sub menu"
MenuItem {
text: "Sub menu item"
}
}
}

Related

How to align button text

I have a button with some text which is normally centered, but I want this text to align to the left. I have tried some things but they don't seem to work. Is this possible, if yes how can it be done?
import QtQuick 2.15
import QtQuick.Window 2.2
import QtQuick.Controls 2.15
Window {
id: window
visible: true
height: 400
width: 400
Button {
id: button
text: "Align me to the left"
//horizontalAlignment: Text.AlignLeft
width: parent.width
height: 30
flat: true
onClicked: {
console.log("You clicked me")
}
}
}
To customize a Button, you can override the contentItem and/or background properties. If you want left-aligned text, just use the contentItem to create a Text object that looks the way you want it.
Button {
id: button
contentItem: Text {
text: button.text
font: button.font
horizontalAlignment : Text.AlignLeft
}
...
}
Button {
id: button
contentItem: Text {
text: "Align me to the left"
horizontalAlignment : Text.AlignLeft
}
width: parent.width
height: 30
flat: true
onClicked: {
console.log("You clicked me")
}
}

Create simple starter menu QtQick/QML

I want to create menu of buttons in QML with simple animation
Simple QML Menu
When I add a button after another I only get the last one
Edit: I added this code also but everytime I click any button the menu disappears
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.15
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Menu {
id: menu
visible: true
MenuItem {
Button{
text:"Play"
}
}
MenuItem {
Button{
text:"Play"
}
}
MenuItem {
Button{
text:"Sett"
}
}
}
}
A MenuItem already derives from AbstractButton, so there's no point in adding a Button as a child of the MenuItem.
The docs show many simple examples, like this:
Menu {
id: menu
MenuItem {
text: "New..."
onTriggered: document.reset()
}
MenuItem {
text: "Open..."
onTriggered: openDialog.open()
}
MenuItem {
text: "Save"
onTriggered: saveDialog.open()
}
}

Show only part of Qt Qml Drawer

I want to show only half of a QML Drawer. The idea is to keep some important information in the visible part of the drawer and then let the user show the full drawer with more information.
From the documentation I thought that the
position property should be suitable for this:
Drawer {
modal: false
interactive: false
position: 0.5 // does not work
}
But setting the position does not have an effect. Is it possible to show only a part of the drawer?
As mentioned in my comment, you may want to turn your concept inside out, and have the Drawer inherit its size from its contents, and have the contents change, rather than hardcode its size and manipulate its position.
Here is a full example that shows the idea. The drawer contains a RowLayout which contains "info" and "extra info" - the extra info's visibility is toggled via interaction, and thus changes the size of the drawer, which always stays at the 100% open position, but changes width automatically.
import QtQuick 2.12
import QtQml 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
Window {
id: root
width: 640
height: 480
visible: true
Drawer {
id: drawer
height: root.height
// width automatically derived from RowLayout child's implicitWidth
onClosed: detailed.visible = false
RowLayout {
height: parent.height
spacing: 0
Rectangle {
id: detailed
color: "lightcyan"
Layout.fillHeight: true
Layout.preferredWidth: 200
visible: false // when not visible, this does not add to the RowLayout's implicitWidth
Text {
anchors {
centerIn: parent
}
text: "Extra Info\n Click to close"
}
MouseArea {
anchors {
fill: parent
}
onClicked: {
detailed.visible = false
}
}
}
Rectangle {
color: "lightpink"
Layout.fillHeight: true
Layout.preferredWidth: 200
Text {
anchors {
centerIn: parent
}
text: "Regular Info\n Click to open extra info"
}
MouseArea {
anchors {
fill: parent
}
onClicked: {
detailed.visible = true // toggling visibility automatically makes the Drawer wider
}
}
}
}
}
MouseArea {
id: mouse
anchors {
fill: parent
}
onClicked: {
drawer.open()
}
}
Text {
anchors {
centerIn: parent
}
text: "Click to open drawer"
}
}

How do I stop a QML drawer from being swiped?

I'm making a side menu using a Drawer and I want to stop the user from swiping the drawer. The drawer should open and close based on button clicks. Is there any way to achieve this or am I better off creating my own component?
Drawer {
id: menu
width: 0.37 * parent.width
height: parent.height
edge: Qt.RightEdge
closePolicy: Popup.NoAutoClose
Button {
id: option
onClicked: menu.close()
}
}
Button {
id: menuButton
onClicked: menu.open()
}
You have to set the interactive property of the Drawer to false:
Drawer {
id: menu
width: 0.37 * parent.width
height: parent.height
edge: Qt.RightEdge
closePolicy: Popup.NoAutoClose
interactive: false
Button {
id: option
onClicked: menu.close()
}
}
Button {
id: menuButton
onClicked: menu.open()
}

QML Swipeview no animation

Is it possible to remove the animation from swipeviews? The one where you see the transition from the previous and the next page. I have many pages and I have a menu that selects the active item like:
mainContent.setCurrentIndex(0)
where mainContent is the swipeview.
// The content changes based on what is clicked on in the menu
SwipeView{
width: mainWindow.width - mainMenuId.width -anchors.leftMargin
id:mainContent
anchors.leftMargin: 20
anchors.topMargin: 20
clip:true
Component.onCompleted: contentItem.interactive = false
currentIndex: 0
Item{PageMain{}}
Item{PageTests{}}
Item{PageData{}}
Item{PageSavedFiles{}}
Item{PageProbe{}}
}
Either you can override the contentItem and disable ListView's animation, or, if you don't really need the swipe part of SwipeView, use e.g. StackLayout instead:
TabBar {
id: bar
width: parent.width
TabButton {
text: qsTr("Home")
}
TabButton {
text: qsTr("Discover")
}
TabButton {
text: qsTr("Activity")
}
}
StackLayout {
width: parent.width
currentIndex: bar.currentIndex
Item {
id: homeTab
}
Item {
id: discoverTab
}
Item {
id: activityTab
}
}
That code is using TabBar, but I think you get the idea. :)

Resources