Imagine the following:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
TextField {
anchors.centerIn: parent
onEditingFinished: console.log("input is: " + text)
}
}
How do I achive that the input of the TextField is accepted (onEditingFinished emitted), on clicking anywhere outside of the TextField (not pressing enter, tab,.. just a mouse click)?
I might set a MouseArea around it with onClicked: forceActiveFocus() to force onEditingFinished, but how do I achive this within a large application with many layers/views? This does not seem to be the right solution.
You can take this approach:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
Window {
id: root
width: 640
height: 480
visible: true
title: qsTr("Hello World")
MouseArea {
anchors.fill: parent
onClicked: {
if (root.activeFocusItem != null) {
root.activeFocusItem.focus = false;
}
}
}
TextField {
anchors.centerIn: parent
onEditingFinished: console.log("input is: " + text)
}
}
This places a single MouseArea filling your Window as a backstop to anything that might appear above it. Either a mouse click will be accepted by a control sitting above the mouse area or will pass through to it and clear focus on anything that might have it.
Related
When my mouse event happened,the menu can popup,but not immediately,it seems a little latency. this is my code, is anything wrong?
My Qt version is 5.15, my system is Windows 10.
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.12
Window {
visible: true
width: 450
height: 350
title: qsTr("ListView")
property bool refreshFlag: false
Rectangle {
id: rightview
width: 60
height: 300
x: 100
color: "#EEEEEE"
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.RightButton | Qt.LeftButton
onClicked: {
console.log(width)
if (mouse.button === Qt.RightButton)
contextMenus.popup()
}
Menu {
id: contextMenus
MenuItem { text: "open" }
MenuItem { text: "save " }
MenuItem { text: "else..." }
}
}
Text {
font.pointSize: 12
text: "content"
}
}
}
this is my screenshot
A couple of issues with your program snippet:
Don't mix QtQuick.Controls 1.x with QtQuick.Controls 2.x
Recommend you update all your references to versions to 2.15
Do not declare Menu inside MouseArea, it doesn't make sense
The MouseArea can be optimized to only accept the RightButton
Declare the Menu at the "top level"
Here's a cleanup of your code:
import QtQuick 2.15
import QtQuick.Controls 2.15
Page {
anchors.fill: parent
Rectangle {
x: 100
width: 60
height: 300
color: "#EEEEEE"
Text {
font.pointSize: 12
text: "content"
}
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.RightButton
onClicked: contextMenus.popup()
}
}
Menu {
id: contextMenus
MenuItem { text: "open" }
MenuItem { text: "save " }
MenuItem { text: "else..." }
}
}
You can Try it Online!
There appears to be no performance issue when I run the above snippet using qmlonline. I don't think the code is an issue. I think we need to get an understanding of:
Your version of Qt
Your platform (i.e. OS, hardware, etc)
I need to have a Popup that stays visible outside the bounds of the main window.
I couldn't find anything in the Qt documentation.
This is the code:
import QtQuick 2.15
import QtQuick.Window 2.2
import QtQuick.Controls 2.12
ApplicationWindow {
id: window
width: 400
height: 400
visible: true
Button {
text: "Open"
onClicked: popup.open()
}
Popup {
id: popup
x: 100
y: 100
width: 300
height: 400
modal: true
focus: true
dim: false
contentItem: Rectangle
{
anchors.fill: parent
color: "red"
}
closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent
}
}
This is the output of this:
I want the red to go beyond the window borders.
Something like this:
I'd appreciate the help!
Note: using a Dialog is no good for me.
Popups are not proper windows, so you'd need to create a new window like Michael mentioned:
import QtQuick 2.15
import QtQuick.Controls 2.15
ApplicationWindow {
id: mainWindow
width: 640
height: 480
visible: true
ApplicationWindow {
id: redWindow
x: mainWindow.x + mainWindow.width / 2
y: mainWindow.y + mainWindow.height / 2
width: 300
height: 400
flags: Qt.Popup | Qt.Dialog
visible: true
Rectangle {
color: "red"
anchors.fill: parent
}
}
}
There is a suggestion to make Popups proper windows here.
So I have some Custom Types aligned in a Column. However when I run the program they get displayed on the same row. What am I missing here?
main.qml:
import QtQuick 2.15
import QtQuick.Window 2.15
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Column{
OSDisplay{
osName: "Kde Neon User Edition"
}
KernelDisplay{
kernelName: "Kernel: 5.3.0-51-generic"
}
//....
}
}
OSDisplay.qml:
import QtQuick 2.15
Rectangle {
property string osName: ""
Text{
anchors.fill: parent
text: "OS: " + parent.osName
}
}
KernelDisplay.qml:
import QtQuick 2.15
Rectangle {
property string kernelName: ""
Text{
anchors.fill: parent
text: "Kernel: " + parent.kernelName
}
}
Your custom types rectangle needs width and height.
e.g.KernelDisplay.qml
import QtQuick 2.15
Rectangle {
width: 100 // or your text.width
height: 50 // or your text.height
property string kernelName: ""
Text{
anchors.fill: parent
text: "Kernel: " + parent.kernelName
}
}
I working on a qt qml application and I want to use the virtual keyboard feature of qt quick. But I want the virtual keyboard to appear only when I touch my laptop screen. The current solution of mine brings virtual keyboard up even when I click mouse on the editable area.
I tried to a lot but couldnt get close to solution. I was thinking of suppressing the mouse click event on the Input Panel but couldnt figure out how
import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
import QtQuick.Layouts 1.1
import QtQuick.VirtualKeyboard 2.1
import QtQuick.Window 2.12
ApplicationWindow {
visible: true
width: 720
height: 480
minimumWidth: 400
minimumHeight:350
TabView {
id:frame
anchors.fill: parent
style: myTabViewStyle
implicitHeight: 100
Tab{
id: setupPage
objectName:"TabParentOfSetup"
enabled: true
title: "Setup"
active: true
SetupTab { }
}
Tab{
id:tabletab
objectName: "TabParentOfTable"
title: "Table"
TableTab{}
}
}
statusBar: StatusBar
{
Label{
id: label
}
}
InputPanel {
id: inputPanel
y: Qt.inputMethod.visible ? parent.height - inputPanel.height :
parent.height
anchors.right: parent.right
anchors.left: parent.left
}
}
I recently installed the Qt5 RC2 for Mac OS X and started developing some QML applications. After looking at the new elements, I especially wanted to try the Window and Screen Element. (http://qt-project.org/doc/qt-5.0/qtquick/qmlmodule-qtquick-window2-qtquick-window-2.html)
So I set the imports at the top of the file like this:
import QtQuick 2.0
import QtQuick.Window 2.0
The import is found, but I can use neither Window nor Screen. Everytime I type Screen or Window an error appears which says "Unknown component (M300)"
Has anyone an idea what the problem is?
Sometimes QtCreator doesn't recognize some types/properties, mainly the ones that were not present in Qt4.x, but that doesn't mean you can't use them, so yes, 'Window' is unknown just like properties 'antialiasing', 'fontSizeMode' or 'active' are, but you can use them, here is an example of use for QtQuick.Window 2.0 :
import QtQuick 2.0
import QtQuick.Window 2.0
Window {
id: win1;
width: 320;
height: 240;
visible: true;
color: "yellow";
title: "First Window";
Text {
anchors.centerIn: parent;
text: "First Window";
Text {
id: statusText;
text: "second window is " + (win2.visible ? "visible" : "invisible");
anchors.top: parent.bottom;
anchors.horizontalCenter: parent.horizontalCenter;
}
}
MouseArea {
anchors.fill: parent;
onClicked: { win2.visible = !win2.visible; }
}
Window {
id: win2;
width: win1.width;
height: win1.height;
x: win1.x + win1.width;
y: win1.y;
color: "green";
title: "Second Window";
Rectangle {
anchors.fill: parent
anchors.margins: 10
Text {
anchors.centerIn: parent
text: "Second Window"
}
}
}
}
You just need to have a Window item as root object, and you can embed other Window items into it.