I want a button to show/hide popup,like below:
Button{
id:btn
text:"open"
onClicked: {
if(pup.opened)
{
pup.close()
}
else
{
pup.open()
}
}
}
Popup {
id: pup
x: 100
y: 100
width: 200
height: 300
}
Open the popup is ok.
But,when mouse above the button and be clicked, the popup is closed by effects of default closePolicy first.
And then onClicked of button is emitted,so the popup is opened again.
I want it looks like the popup is closed by button clicked(I want keep default closePolicy also).Has any advise?
Qt version:5.15.2
Open the popup is ok. But,when mouse above the button and be clicked,
the popup is closed by effects of default closePolicy first. And then
onClicked of button is emitted,so the popup is opened again. I want it
looks like the popup is closed by button clicked(I want keep default
closePolicy also). Has any advise?
It's impossible because the default policy value is Popup.CloseOnEscape | Popup.CloseOnPressOutside. In your case, the "open" button is outside the popup area and that's why it breaks policy. You have two solutions:
I. Change policy:
Button{
id:btn
text:"open"
z: 100
onClicked: {
if(pup.opened)
{
pup.close()
}
else
{
pup.open()
}
}
}
Popup {
id: pup
x: 100
y: 100
width: 200
height: 300
closePolicy: Popup.NoAutoClose
}
II. Fake behaviour:
Use the "open" button only to open the dialogue and close it by tapping outside the popup. You can disable the button when the popup is opened and it should fake the behaviour which you want. Additionally, you can customise the button and the style can be the same for enabled and disabled states.
Button{
id:btn
text:"open"
z: 100
enabled: !pup.opened
onClicked: {
if(pup.opened)
{
pup.close()
}
else
{
pup.open()
}
}
}
Popup {
id: pup
x: 100
y: 100
width: 200
height: 300
}
Related
I'm struggling with a very basic question..
Using QT 5.15.2:
We have a simple application with one main window and 2-3 sub-window (1 level down from main). The main window consists of a content item, a header and some menu-flaps distributed across the main window. So Far the sub-pages were opened with a drawer element.
However, the drawer overlays the flaps and header once opened and we need to re-instanciate the flaps and header within the drawer to have it visible. This is not really nice. Is there any way to define the z-level on which the drawer is opened? (apparently setting z doesn't work).
Item{
id: id_mainWindow
z: 0
Drawer{
id: id_subMenu1
anchors.fill: parent
z: 1
/* Not so nice workaround */
Button{
id: id_subClose
z: 100
onClicked{
id_subMenu1.close()
}
}
}
/* Unfortunately, this one gets hidden once, the drawer is open */
Button{
id: id_subOpenClose
z: 100
onClicked{
if( id_subMenu1.open ){
id_subMenu1.close()
} else {
id_subMenu1.open()
}
}
}
}
I would suggest that a Drawer is not the right component for this job as it is technically a Popup. It might be worth checking out the TabBar component instead.
Nevertheless here's a re-write of your code so that your Drawer opens without covering your id_subOpenClose button.
import QtQuick
import QtQuick.Controls
import QtQuick.Controls.Material
Rectangle {
id: id_mainWindow
anchors.fill: parent
Drawer {
id: id_subMenu1
/*
Set the Drawer's height and y position so that it does not cover your button
*/
y: id_subOpenClose.height
height: id_mainWindow.height - id_subOpenClose.height
width: id_mainWindow.width
// Do not dim background
dim: false
// Set this to zero if you want no shadow
Material.elevation: 2
edge: Qt.RightEdge
Label {
text: 'Hello World'
anchors.centerIn: parent
}
}
/*
This is your header button that was getting hidden
Here it stays as if it were part of a global header and does not get hidden by
the Drawer.
*/
Button{
id: id_subOpenClose
text: id_subMenu1.visible? 'close': 'open'
onClicked: id_subMenu1.visible? id_subMenu1.close(): id_subMenu1.open()
}
}
For an interactive WASM example of the above see here.
Iam using QML Busyindicator when a user login or loading a data in a page or something like this, I want to be able to make the busyindicator overlay over the current window and make the window not editable, something similar to the messagebox when it shows up and disables all the Window, so I cant interact with the widgets on this window until the loading finish, something like the ProgressDialog in Android.
this is my code and what I try to do, also I tried to use MessageDialog and use this indicator inside it and remove all buttons, but it does not work.
Popup {
id: waitpop
width: 100
height: 100
BusyIndicator {
id: login_progress
running: true
anchors.fill: parent
}
anchors.centerIn: Overlay.overlay
closePolicy: Popup.NoAutoClose
}
this code shows the busy indicator but the user still can interact with the button and text field and everything, so any ideas?
The modal property prevents you from being able to click outside the popup.
Popup {
anchors.centerIn: Overlay.overlay
closePolicy: Popup.NoAutoClose
modal: true
BusyIndicator {
running: true
}
}
I'm using a TextArea to display multi-line text with embedded <IMG ...> tags in the delegate for a ListView. I have it set to read-only (and not disabled) because I need hyperlinks in the text to be clickable so I need to make use of its onLinkActivated event handler. This is normally something that would call for a Label (which does not handle mouse wheel events), but a Label does not render line-breaks correctly when the text includes <IMG ...> tags in the HTML.
The problem I'm having is that a TextArea handles mouse wheel events even when it is read-only, so if the cursor happens to be over one of the visible TextArea controls, the ListView will not respond to mouse wheel events (and so it does not scroll). In other words, the TextArea is capturing the mouse wheel events and I want it to not do this.
I see in the docs that controls have a wheelEnabled: property, but TextArea does not seem to support this.
Update: here is a minimum code sample that demonstrates the problem:
import QtQuick.Controls 1.4 as Controls
Rectangle {
id: test
color: "white"
width: 300
anchors {
left: parent.left
top: parent.top
bottom: parent.bottom
}
Controls.ScrollView {
id: _scrollview
anchors.fill: parent
ListView {
anchors.fill: parent
model: 100
delegate: Rectangle {
id: tableRow
width: test.width
height: 50
color: "yellow"
TextArea {
width: test.width / 2
height: tableRow.height
readOnly: true
text: "Row # " + index
}
}
}
}
}
If you hold the mouse cursor over the right side of this listview (i.e. not over the TextArea control in the row), the mouse wheel works as expected. But if you hold the mouse cursor over the TextArea in any of the rows, the ListView will not scroll with the mouse wheel (because the readOnly TextView is capturing the events).
This is actually pretty easy, too bad I wasted the bounty. All this requires is a MouseArea positioned over the TextArea like so:
MouseArea {
anchors.fill: txtTester
onPressed: {
mouse.accepted = false
}
onReleased: {
mouse.accepted = false
}
property int scrollValue: 15
onWheel: {
if (wheel.angleDelta.y < 0) {
//make sure not to scroll too far
if (!_scrollview.flickableItem.atYEnd)
_scrollview.flickableItem.contentY += scrollValue
}
else {
//make sure not to scroll too far
if (!_scrollview.flickableItem.atYBeginning)
_scrollview.flickableItem.contentY -= scrollValue
}
}
}
This ignores press and release events so clicking on hyperlinks in the TextArea still works, but it intercepts mouse wheel events and applies them to moving the ScrollView as if the TextArea were not there.
Try this out:
import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.3
Window {
visible: true
width: 400
height: 200
TextArea {
id: text
anchors.fill: parent
text: "Current\ntext\n\\to\nmove\ndown\ndown\ndown
\ndown\ndown\ndown\ndown\ndown\ndown\ndown"
flickableItem.interactive: false
}
}
TextArea has flickableItem.enabled property. Since you're stuck with !t 5.6 this should work for you.
EDIT: changed to flickableItem.interactive instead.
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
}
}
Encountered the following problem with rendering in QML. I have implemented the 'minimize window' button:
Image {
source: "minimize.png"
scale: mouse.pressed ? 0.8 : 1.0
smooth: mouse.pressed
MouseArea {
id: mouse
anchors.fill: parent
anchors.margins: -5
onClicked: {
console.log("MinimizeButton clicked");
viewer.showMinimized();
}
}
}
where 'viewer' is the object inherited from QDeclarativeView which represents the main application window.
The button shrinks when user clicks the mouse onto it and window has been minimized. But button stays shrinked when window is restored.
Tried to add the timer which prints 'mouse.pressed' every 1 sec:
Timer {
repeat: true
interval: 1000
running: true
onTriggered: {
console.log("mouse.pressed =",mouse.pressed);
}
}
It always prints mouse not pressed. But button is scaled to 0.8, not 1.0.
"viewer.showMinimized()" appears to be guilty: button is rendered OK if it is commented out.
Any suggestions to solve the problem?