I want to access these Properties mentioned here:
http://qt-project.org/doc/qt-5/qml-qtqml-qt.html
application : object
inputMethod : object
platform : object
But I can't find them, the compiler complains about it, I used:
import QtQml 2.2
but nothing changed!
What to do?
Edit:
Here's the code in which I'm trying to use Qt.inputMethod:
import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.1
import QtQuick.Window 2.0
import QtQuick.Controls.Styles 1.2
import QtQuick.Dialogs 1.2
ApplicationWindow {
id: parentWindow
visible: true
width: 640
height: 480
title: qsTr("Main Window")
TextInput
{
id: text_input1
anchors.fill: parent
font.pixelSize: parent.height-4
text: qsTr("TEXT")
Keys.onReturnPressed:
{
focus = true;
Qt.inputMethod.hide();
}
}
}
Related
I'm trying to automate picking a file in QML in a QtQuick Controls FileDialog. How can I invoke FileDialog's accept with a specific fileUrl when the fileUrl property is read only?
The current attempt involves calling filedialog.clearSelection, filedialog.addSelection and finally filedialog.accept. clearSelection and addSelection are not documented but can be found in https://github.com/qt/qtquickcontrols/blob/dev/src/dialogs/qquickfiledialog.cpp (Assuming Qt has used a DefaultFileDialog as this can be system dependent)
However clearSelection seems to only work sporadically, having no affect if the same FileDialog has been used manually, hence the addSelection fails to set fileUrl.
The following is a QML file (loaded as a basic project within QtCreator) demonstrates this. with a manual file dialog open button and an automatic one:
import QtQuick 2.9
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.4
import QtQuick.Dialogs 1.3
Window {
visible: true;width: 200;height:200
FileDialog {id: filedialog; onAccepted: console.log("File Dialog Accepted: ", fileUrl, fileUrls);}
Row {
Button {text: "manual"; onClicked: filedialog.open()}
Button {
text: "auto_qml"
onClicked: {
console.log("Current selection:", filedialog.fileUrl, filedialog.fileUrls)
filedialog.clearSelection();
console.log("cleared selection:", filedialog.fileUrl, filedialog.fileUrls) // only clears selection if manual not used
let t = filedialog.addSelection("file:/home/user/tempfile.txt");
console.log("add selection success:", t) // a non existent file returns false, so file must exist
filedialog.accept()
}
}
}
}
As variant you can use Qt.labs.platform 1.1 library. It contains FileDialog with a little bit another behavior - file property is not read-only.
And you can do like so:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.4
import Qt.labs.platform 1.1
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
FileDialog {id: filedialog; onAccepted: console.log("File Dialog Accepted: ", file, files);}
Row {
Button {text: "manual"; onClicked: filedialog.open()}
Button {
text: "auto_qml"
onClicked: {
filedialog.file = "file:/home/user/tempfile.txt";
console.log("Current selection:", filedialog.file, filedialog.files)
filedialog.accepted()
}
}
}
}
I am trying to access items that are in a Repeater, using JavaScript. This works as long as everything is in a single QML file. I understand that, when I split things into two files, the id of the Repeater moves out of the scope of my script.
What I don't understand is how I best solve this problem, since I cannot export the id as property.
Here is a MWE with two files:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.2
Window {
id: root
visible: true
width: 640
height: 480
ColumnLayout {
Mwe2 {}
Button {
text: "Test"
onClicked: {
console.log("Clicked");
rect.itemAt(1).color = 'yellow'; # <- out of scope
}
}
}
}
File Mwe2.qml
import QtQuick 2.9
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.2
RowLayout {
spacing: 2
Repeater {
id: rect
model: 3
Rectangle {
color: 'red'
width: 50
height: 20
}
}
}
As you indicate, the ids have the scope of the qml file, so if you want to access those elements you must expose it through a property, function, etc. of the top level of the qml. In this case for simplicity I will use an alias.
import QtQuick 2.9
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.2
RowLayout {
property alias repeater: rect
spacing: 2
Repeater {
id: rect
model: 3
Rectangle {
color: 'red'
width: 50
height: 20
}
}
}
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.2
Window {
id: root
visible: true
width: 640
height: 480
ColumnLayout {
Mwe2 {
id: mwe2
}
Button {
text: "Test"
onClicked: {
console.log("Clicked")
mwe2.repeater.itemAt(1).color = "yellow"
}
}
}
}
I am running QtCreator with msvc2017_64. Everything was working fine suddenly i am unable to load this simple qml code in QQmlApplicationEngine
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Button {
height: 50
width: 100
anchors.bottom: parent.bottom
text: "close"
//onClicked: closeApp();
}
}
and when i am commenting Button then i can run application. it looks like my QtCreator suddenly stopped support for
import QtQuick.Controls 2.0
I'm using Qt5.9.1 . when i try to open drawer from another qml file i got error
QML Drawer: cannot find any window to open popup in
I have my drawer in drawerBottom.qml and i want to open it from tablePage.qml using a qml function call. Here is my code....
DrawerBottom.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts1.3
import QtQuick.Window 2.3
import QtQuick.Dialogs 1.2
import Qt.labs.settings 1.0
Item {
id: itemDrawerBottom
Settings {
id: settingBtm
property string style: "Default"
}
Drawer{
id: drawerBottom
Flickable{
width: 1338
height: 142
anchors.rightMargin: 8
anchors.leftMargin: 13
anchors.topMargin: 15
anchors.bottomMargin: 0
id: flickable
clip: true
anchors.fill: parent
contentWidth: flickable.width ;
contentHeight: flickable.height*1.2;
Grid {
id: gridBottom
anchors.rightMargin: 9
anchors.bottomMargin: 10
anchors.fill: parent
anchors.leftMargin: 21
anchors.topMargin: 10
columns: 2
spacing: 20
flow: Grid.LeftToRight
Component{
id: componentDrawer
Button{
id:tBtnBottom1
implicitWidth: 130
implicitHeight: 35
}
}
}
}
ScrollIndicator.vertical: ScrollIndicator { }
}
function draweropen(){
drawerbottom.open()
} }
and tablePage.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtQuick.Window 2.3
import QtQuick.Dialogs 1.2
import Qt.labs.settings 1.0
Item {
id: itemtable
property Item itemDrawerBottom:DrawerBottom {}
Button {
id: button41
x: 159
y: 503
width: 20
height: 20
onClicked:{
itemDrawerBottom.draweropen()
}
}
}
and i use an application window which is in mail.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtQuick.Window 2.3
import QtQuick.Dialogs 1.2
import Qt.labs.settings 1.0
ApplicationWindow {
id:mainwindow
visible: true
width:Screen.width
height: Screen.height
title: qsTr("E-eatZ")
GridLayout {
id: gridLayouttableview
x: 1048
y: 168
width: 529
height: 592
TablePage{
}
ColumnLayout {
id: columnLayout1DrawerBottom
x: -373
y: 412
width: 50
height: 348
DrawerBottom{
}
}
}
}
Is there a possibility to insert a newline character into the text property of a TextField component:
import QtQuick 2.6
import QtQuick.Controls 1.2
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
Grid {
TextField {
text: "Text\nhere"
}
}
}
prints a blank instead of a newline in the Textfield
According to the docs:
TextField is used to accept a line of text input. [...]
Therefore you can not have multiple lines in that item. You must use another item like TextArea
import QtQuick 2.6
import QtQuick.Controls 1.2
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
Grid {
TextArea {
text: "Text\nshere"
}
}
}