DropDown menu with QtQuickControls2 on Qt application - qt

on qt quick controls 2, how can I configure a button Item with a dropdown menu of others buttons. When the user click on this button is triggered onClicked signal and under the button should be open a dropdown menu with other buttons. I have try with ButtonGroup but I don't know if this is the best practice. There is some Controls2 item to be use? Thanks in advice
Best Regards
Daniele

There is Menu from QtQuick.Controls 2.0 that can do what you want.
Example taken from the documentation :
Button {
id: fileButton
text: "File"
onClicked: menu.open()
Menu {
id: menu
y: fileButton.height
MenuItem {
text: "New..."
}
MenuItem {
text: "Open..."
}
MenuItem {
text: "Save"
}
}
}

Related

QML MenuBar hovering items with mouse pressed

Using Qt Quick Controls 2, you can create a "traditional" menu bar like this:
ApplicationWindow {
id: window
width: 320
height: 260
visible: true
menuBar: MenuBar {
Menu {
title: qsTr("&File")
Action { text: qsTr("&New...") }
Action { text: qsTr("&Open...") }
Action { text: qsTr("&Save") }
Action { text: qsTr("Save &As...") }
MenuSeparator { }
Action { text: qsTr("&Quit") }
}
Menu {
title: qsTr("&Edit")
Action { text: qsTr("Cu&t") }
Action { text: qsTr("&Copy") }
Action { text: qsTr("&Paste") }
}
Menu {
title: qsTr("&Help")
Action { text: qsTr("&About") }
}
}
}
This works ok, but when the user presses on a menu and then drag the mouse while pressed, on the menus are not hovered. In order to hover over the menus, the mouse cannot be in a pressed state (using Qt Widgets https://doc.qt.io/qt-5/qtwidgets-mainwindows-menus-example.html this is not needed).
Is there a way to make the MenuBar, hover over items while the mouse is pressed?
When you did through this doc
https://doc.qt.io/qt-5/qml-qtquick-controls2-action.html
You then go to this doc and have high hopes when reading about "hoverEnabled"
https://doc.qt.io/qt-5/qml-qtquick-controls2-toolbutton-members.html
You really need these two signals entered() exited() from a MouseArea.
https://doc.qt.io/qt-5/qml-qtquick-mousearea.html
The short hopeful hack is to see if the documentation is "just wrong" and somewhere deep in the class structure they declared a MouseArea for a button and you really do get entered() and exited().
How else would you be able to implement hoverEnabled()? The "widget" has to know the mouse entered and did not yet exit. They may well be consuming that, but you should be able to dig through the source and find child entity that you can connect to the signal of.
There is not an easy way.
The underlying issue is that MenuItem is implemented as a Button.
When you press a button and release on another button, none of them register a click.
However, on traditional menus, if you press an item and release on another one, the item that registers the release is triggered.
The public API offered by QtQuick Controls 2 does not seem to offer a way to easily change this. So to get what you want Isee the following solutions:
Use Qt.labs.platform to build your menus, these will be native menus so they should have the correct behavior. However it still in a preview state and i have not tested them.
Reimplement MenuItem. Since it is part of Qt Quick Controls 2 it is easy to reimplement your own MenuItem, see Qt documentation. However, you will have to use MouseArea to catch user inputs and force the behavior you want.
EDIT
The 2nd solution won't work. In Qt once an Item (or a QWidget) accepts a press event, it grabs the mouse until it is released. So reimplementing MenuItem and adding MouseArea to them won't help.
Knowing that it seems that the solution would to reproduce what QMenu is doing: You need to have a single Item responsible for handling mouse events. So you should not let each MenuItem handles mouse events individually. Instead you should handle mouse events at the Menu or MenuBar level, process the events and manually change the MenuItems.
At this point I do not know if it is easier to customize Menu and MenuItem or to jus write your own Menu from scratch.

WebEngineView variables 'canGoBack' and 'canGoForward' returns wrong values

I'm working on an application that's running on Raspberry Pi 4, and I got this strange behavior of QML's WebEngineView.
I created 3 buttons for going to the main page and for going back and forward:
Button {
id: box_button_main_page
text: "Go to Main Page"
onClicked: mainWebView.url = "https://www.youtube.com/"
}
Button {
id: box_button_go_back
enabled: mainWebView.canGoBack
text: "Back"
onClicked: mainWebView.goBack()
}
Button {
id: box_button_go_forward
enabled: mainWebView.canGoForward
text: "Forward"
onClicked: mainWebView.goForward()
}
It works as expected: the "Back" and "Forward" buttons become grayed at the right points. But, if one makes below actions:
go to any link
go to yet another link
click "Back" button
click "Go to Main Page"
then both "Back" and "Forward" buttons are still enabled. And clicking on "Forward" button does nothing.
I checked in the "Go to Main Page" onClicked action that both canGoBack and canGoForward returns true after setting url property. I tried to search if there is yet another method to order WEV to switch to another page, but there is none :(.
I didn't find on the Net anyone reporting it as a bug, so either I'm doing it wrong, or I actually found a bug >_>
It works with WebAction. Have a look at this demo. All I've done is add the following Button in the ToolBar RowLayout:
Button {
text: "Go to Main Page"
onClicked: webEngineView.url = "https://www.youtube.com/"
}
Here is a full example using WebAction:
import QtQuick 2.15
import QtQuick.Window 2.15
import QtWebEngine 1.10
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
ApplicationWindow {
id: window
visible: true
width: 800
height: 600
title: qsTr("WebEngineAction Example")
header: ToolBar {
RowLayout {
anchors.fill: parent
Button {
text: "Go to Main Page"
onClicked: webEngineView.url = "https://www.youtube.com/"
}
Button {
property int itemAction: WebEngineView.Back
text: "Back"
enabled: webEngineView.action(itemAction).enabled
onClicked: webEngineView.action(itemAction).trigger()
}
Button {
property int itemAction: WebEngineView.Forward
text: "Forward"
enabled: webEngineView.action(itemAction).enabled
onClicked: webEngineView.action(itemAction).trigger()
}
}
}
WebEngineView {
id: webEngineView
url: "https://qt.io"
anchors.fill: parent
}
}

qml dialog removing ok button

I want to create a custom dialog in qml without the ok button.
this is my code :
Dialog {
id: DialogId
title: appName
}
when the dialog is opened there is an Ok button.
I'm using QtQuick.Dialogs 1.2
The property standardButtons controls wich buttons are in your dialog.
The default value is StandardButton.Ok
If you don't whant any button you need to re-implement contentItem
For instance:
contentItem: Rectangle {
color: "lightskyblue"
implicitWidth: 400
implicitHeight: 100
Text {
text: "Hello blue sky!"
color: "navy"
anchors.centerIn: parent
}
}

QML MenuItem trigger by Enter and keyboard events forwarding don't work

Here I have a minimal example:
import QtQuick 2.9
import QtQuick.Controls 2.3
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Component.onCompleted: {
menu.open();
}
Menu {
id: menu
Keys.onPressed: {
console.log("pressed")
}
MenuItem {
text: "test"
Keys.forwardTo: [menu]
focus: highlighted
onTriggered: {
console.log("triggered 1")
}
}
MenuItem {
text: "test2"
focus: highlighted
Keys.forwardTo: [menu]
onTriggered: {
console.log("triggered 2")
}
}
MenuItem {
text: "test3"
focus: highlighted
Keys.forwardTo: [menu]
onTriggered: {
console.log("triggered 3")
}
}
}
}
Navigating through Menu Items with keyboard works as expected. But triggering with Enter key doesn't work. Also as a workaround I tried to forward key events to the parent menu item, and it didn't work as well.
The only way to fix it I see right now: place Keys.onReturnPressed to every MenuItem and make it call triggered() signal. But it feels wrong.
Is it a bug of Qt?
What is a right workaround for it?
I'm using Qt 5.10.1
The Menu is not Item, so it could not set into Keys.forwardTo.
The navigation of Menu via the Forward/Backward key is enabled by default, Keys.forwardTo is not necessary.
Why Enter key does not work because to select the MenuItem is using Space key not the Enter key. [↑] and [↓] is for Navigation(focus) and [Space] is for Selection
I think if you want to listen the [Enter] key for the Selection, add Keys.onReturnPressed: triggered(); is the right way.
Update:
The [Enter] key was worked in Qt 5.6 (verified). check the source code. But looks like it is removed in the current version.

How to make qml tab order navigation?

How to make tab order navigation in QML
For example I have such code:
TextInput { id: one }
TextInput { id: two }
TextInput { id: three }
And I need on pressing tab from focus on "one" move to "three", haven't found that in official documentation.
TextInput {
id: one
KeyNavigation.tab: three
}
Key navigation in QML is documented at this page and provide some example at:
http://qt-project.org/doc/qt-4.8/qml-keynavigation.html

Resources