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()
}
Related
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"
}
}
I am attempting to customize the text on the buttons of my custom Dialog.
For this I used a DialogButtonBox as the footer of the dialog, together with some loaders that will load the appropriate buttons as chosen by the user.
However what I'm seeing is that for some popups the buttons get stacked at the far left, and you can't click them. I have managed to reproduce it with a small example:
main.qml
import QtQuick 2.12
import QtQuick.Window 2.2
import QtQuick.Controls 2.4
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Row{
anchors.centerIn: parent
Button{
property var popup: SimDialog { }
onClicked:
{
popup.open()
}
text: "Open"
}
}
}
SimDialog.qml
import QtQuick 2.12
import QtQuick.Controls 2.12
Dialog {
// ID
id: popupRoot
width: 500
height: 200
topMargin: 20
bottomMargin: 10
leftMargin: 20
rightMargin: 10
// Centered position
x: parent ? parent.x + (parent.width / 2) - (width / 2) : 0// align horizontally centered
y: parent ? parent.y + (parent.height / 2) - (height / 2) : 0// align vertically centered
modal: true
focus: true
clip: true
property int buttons: Dialog.Close | Dialog.Ignore
title: "Test"
Button
{
anchors.centerIn: parent
text: "HELLO"
}
// personalized dialog box buttons so we can translate the button text.
footer: DialogButtonBox {
id: dialogBox
alignment: Qt.AlignRight
Loader
{
active: buttons & Dialog.Help
sourceComponent:
Button {
parent: dialogBox
text: qsTr("Help")
DialogButtonBox.buttonRole: DialogButtonBox.HelpRole
flat: true
}
}
Loader
{
active: buttons & Dialog.Discard
sourceComponent:
Button {
parent: dialogBox
text: qsTr("Continue without saving")
DialogButtonBox.buttonRole: DialogButtonBox.DestructiveRole
flat: true
}
}
Loader
{
active: buttons & Dialog.Save
sourceComponent:
Button {
parent: dialogBox
text: qsTr("Save")
DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
flat: true
}
}
Loader
{
active: buttons & Dialog.Ignore
sourceComponent:
Button {
parent: dialogBox
text: qsTr("Ignore")
DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
flat: true
}
}
Loader
{
active: buttons & Dialog.Apply
sourceComponent:
Button {
parent: dialogBox
text: qsTr("Apply")
DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
flat: true
}
}
Loader
{
active: buttons & Dialog.Ok
sourceComponent:
Button {
parent: dialogBox
text: qsTr("Ok")
DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
flat: true
}
}
Loader
{
active: buttons & Dialog.Close
sourceComponent:
Button {
parent: dialogBox
text: qsTr("Close")
DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
flat: true
}
}
Loader
{
active: buttons & Dialog.Cancel
sourceComponent:
Button {
parent: dialogBox
text: qsTr("Cancel")
DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
flat: true
}
}
}
}
Note that in my actual application I have several of these popups and only some of them have this behavior. I haven't been able to pinpoint a commonality between those that don't work either, so I'm really lost.
Here's how it looks on my end
you don't really need the loaders. You can just use the visible property in Button.
This worked for me as expected:
// personalized dialog box buttons so we can translate the button text.
footer: DialogButtonBox {
id: dialogBox
Button {
text: qsTr("Help")
DialogButtonBox.buttonRole: DialogButtonBox.HelpRole
flat: true
visible: true //false
}
Button {
text: qsTr("Continue without saving")
DialogButtonBox.buttonRole: DialogButtonBox.DestructiveRole
flat: true
}
Button {
text: qsTr("Save")
DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
flat: true
}
Button {
text: qsTr("Ignore")
DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
flat: true
}
Button {
text: qsTr("Apply")
DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
flat: true
}
Button {
text: qsTr("Ok")
DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
flat: true
}
Button {
text: qsTr("Close")
DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
flat: true
}
Button {
text: qsTr("Cancel")
DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
flat: true
}
}
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. :)
I'm trying to find any way to implement an transition when i navigate between tabs in an TabView in QML, but I can't find that, is there any way to do it ?
my code is:
TabView{
id : tbView
height: parent.height
width: parent.width
Tab{
HomePage{
id: home
height: tbView.height
width: tbView.width
onBtnConfigClicked: btnPlay()
}
}
Tab{
Rectangle{
anchors.fill: parent
color: "blue"
}
}
}
function btnPlay()
{
tbView.currentIndex = 1
}
the HomePage element has an signal btnConfigClick, then i attach it to the function btnPlay, it function change currentIndex property to 1, the first tab is 0, I would like an slide transition when a change the currentIndex.
Well, you can use the TabBar compontent together with a SwipeView:
From documentation:
TabBar {
id: bar
width: parent.width
TabButton {
text: qsTr("Home")
}
TabButton {
text: qsTr("Discover")
}
TabButton {
text: qsTr("Activity")
}
}
SwipeView {
width: parent.width
currentIndex: bar.currentIndex
Item {
id: homeTab
}
Item {
id: discoverTab
}
Item {
id: activityTab
}
}
So changing the currentIndex property of TabBar will automatically perform a SwipeView transition to corresponding item.
Because your question is not entirely clear to me, I do not know whether that is what you wanted to achieve.
I want to perform the expansion action in the ToolBar when the user taps on the title of it, as in the pictures that I attach.
When the user touches the title of the toolbar then you should see a list of the filters that you can apply.
You have any ideas on how to implement this action in QML?
Pretty easy. Copy the following in the awesome QML web editor:
import QtQuick 2.0
Column {
width: 500
Rectangle {
id: toolbar
width: parent.width
height: 50
Text {
text: "Elenco"
anchors.centerIn: parent
font.pointSize: 24; font.bold: true
}
MouseArea {
anchors.fill: parent
onClicked: listBox.visible = !listBox.visible
}
}
Rectangle {
id: listBox
color: "gray"
width: parent.width
visible: false
height: 200
Column {
width: parent.width
Repeater {
model: 4
delegate:
Rectangle {
width: parent.width
color: index % 2 ? "#C9D6DE" : "#E7F6FF"
height: 50
Text { anchors.centerIn: parent; text: "Persona " + (index + 1) }
}
}
}
}
}