Reputation: 25
I have 2 files a main.qml (which holds all of the data for the ui) and a main.py which (which starts the window and will control the logic). I can not figure out how to find the value of the text input.
Ui screenshot
main.py
import os
from pathlib import Path
import sys
import pideck_query
import keyboard
import threading
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
#closes window
def quit():
sys.exit(app.exec_())
def UserInput():
while True:
if keyboard.is_pressed('enter'):
print()#i want this to print the text that the user has entered
#pideck_query.ManualEnter()
break
#creates window and calls to check for old
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load(os.fspath(Path(__file__).resolve().parent / "main.qml"))
pideck_query.CheckForOld()
x = threading.Thread(target=UserInput)
x.start()
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
main.qml
import QtQuick 2.14
import QtQuick.Window 2.14
Window {
id: window
width: 1920
height: 1080
visible: true
color: "#39393a"
title: qsTr("PiDeck")
Text {
id: dropshadow
text: qsTr("Searching For Pideck Device...")
anchors.left: title_text.right
anchors.top: title_text.bottom
font.pixelSize: 120
anchors.leftMargin: -1806
anchors.topMargin: -131
font.weight: Font.ExtraBold
font.family: "Tahoma"
}
Text {
id: title_text
color: "#ffffff"
text: qsTr("Searching For Pideck Device...")
anchors.left: parent.left
anchors.top: parent.top
font.pixelSize: 120
anchors.leftMargin: 53
anchors.topMargin: 40
font.weight: Font.ExtraBold
font.family: "Tahoma"
}
TextInput {
id: ip_input
width: 830
height: 121
color: "#ffffff"
text: qsTr("Or enter ip manually")
anchors.left: title_text.right
anchors.top: title_text.bottom
font.pixelSize: 80
anchors.leftMargin: -1813
anchors.topMargin: 188
font.weight: Font.ExtraBold
font.family: "Tahoma"
}
}
/*##^##
Designer {
D{i:0;formeditorZoom:0.33}D{i:1}D{i:2}D{i:3}
}
##^##*/
Upvotes: 0
Views: 575
Reputation: 244132
It is not necessary to use keyboard or an external library to get the QML text. The solution is to create a QObject and expose a method through Slot, and that method must be called when Keys.onReturnPressed is called.
import os
from pathlib import Path
import sys
from PySide2.QtCore import QCoreApplication, QObject, Qt, QUrl, Slot
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
CURRENT_DIRECTORY = Path(__file__).resolve().parent
class Backend(QObject):
@Slot(str)
def foo(self, text):
print("text")
def main():
app = QGuiApplication(sys.argv)
backend = Backend(app)
engine = QQmlApplicationEngine()
engine.rootContext().setContextProperty("backend", backend)
filename = os.fspath(CURRENT_DIRECTORY / "main.qml")
url = QUrl.fromLocalFile(filename)
def handle_object_created(obj, obj_url):
if obj is None and url == obj_url:
QCoreApplication.exit(-1)
engine.objectCreated.connect(handle_object_created, Qt.QueuedConnection)
engine.load(url)
sys.exit(app.exec_())
if __name__ == "__main__":
main()
import QtQuick 2.14
import QtQuick.Window 2.14
Window {
id: window
width: 1920
height: 1080
visible: true
color: "#39393a"
title: qsTr("PiDeck")
Text {
id: dropshadow
text: qsTr("Searching For Pideck Device...")
anchors.left: title_text.right
anchors.top: title_text.bottom
font.pixelSize: 120
anchors.leftMargin: -1806
anchors.topMargin: -131
font.weight: Font.ExtraBold
font.family: "Tahoma"
}
Text {
id: title_text
color: "#ffffff"
text: qsTr("Searching For Pideck Device...")
anchors.left: parent.left
anchors.top: parent.top
font.pixelSize: 120
anchors.leftMargin: 53
anchors.topMargin: 40
font.weight: Font.ExtraBold
font.family: "Tahoma"
}
TextInput {
id: ip_input
width: 830
height: 121
color: "#ffffff"
text: qsTr("Or enter ip manually")
anchors.left: title_text.right
anchors.top: title_text.bottom
font.pixelSize: 80
anchors.leftMargin: -1813
anchors.topMargin: 188
font.weight: Font.ExtraBold
font.family: "Tahoma"
Keys.onReturnPressed: {
backend.foo(ip_input.text)
}
}
}
/*##^##
Designer {
D{i:0;formeditorZoom:0.33}D{i:1}D{i:2}D{i:3}
}
##^##*/
Upvotes: 1