I dynamically added some qml components to my gui according to this blog post. How can I add event handlers for those newly created components?
I'll explain with an example.
1)Create a custom button component as follows
//Button.qml ... This component's objects will be dynamically
// created
import QtQuick 2.1
Rectangle {
width: 100
height: 50
color:"blue"
//Since the buttons are created on the fly,
//we need to identify the button on which the user
// has clicked. The id must be unique
property string buttonId;
signal clicked(string buttonId);
MouseArea {
anchors.fill: parent
onClicked:parent.clicked(parent.buttonId)
}
}
This is a simple button which emits clicked signal on clicking on it..
Now lets create some buttons on the fly.
//Main.qml ... creates some buttons on the fly
import QtQuick 2.1
Rectangle{
id:root
width:500
height:500
function buttonClicked(buttonId)
{
console.debug(buttonId);
}
function createSomeButtons()
{
//Function creates 4 buttons
var component = Qt.createComponent("Button.qml");
for(var i=0;i<4;i++)
{
var buttonY = i*55; //Button height : 50 + 5 unit margin
var button = component.createObject(root,{"x":0,"y":buttonY,"buttonId":i+1});
//Connect the clicked signal of the newly created button
//to the event handler buttonClicked.
button.clicked.connect(buttonClicked)
}
}
Component.onCompleted: {
createSomeButtons();
}
}
Here when the Main.qml component creation has been completed, buttons are created.
4 buttons are created and after creation of each button, the javascript function buttonClicked is connected as event handler to the 'Button.qml''s clicked signal. Whenever the user clicks on the button, buttonClicked function will be called with buttonId as argument. You can do whatever you want in the event handler from here on.
Related
I have a button within my main ApplicationWindow (root) that dynamically loads and opens a second, different ApplicationWindow that is declared in a separate .qml file.
Button {
id: btnLogger
text: "Logger"
onClicked: {
var component = Qt.createComponent("logger.qml")
var window = component.createObject(logRoot)
window.show()
}
}
This works fine for opening a window when clicking the button. Subsequent clicks create further new windows.
My intent is that subsequent clicks should instead focus the preexisting window. If the new window is later closed, then clicking the button should revert back to opening the window.
i.e. if a window doesn't currently exist or exists but has been closed, create it and open it; else, focus it.
How would this be done from within qml? Alternatively, I am currently loading the application from a QQmlApplicationEngine in my C++, how could I use that to achieve this functionality?
The example code for my comment above:
Button {
id: btnLogger
text: "Logger"
property var wnd: undefined
onClicked: {
if(wnd == undefined)
{
var component = Qt.createComponent("logger.qml")
wnd = component.createObject(logRoot);
wnd.closing.connect(function() { btnLogger.wnd = undefined;});
wnd.show();
}
else
{
wnd.requestActivate();
}
}
}
I have created a custom ComboBox that uses a ListView with a TextField in the footer, that's used to dynamically add options to the ComboBox.
The problem is, that as soon as the popup loses focus (so when the TextField receives focus), the popup gets closed.
I tried to force the popup to stay open, which does work, but then prevents the TextField from receiving focus (I guess because the popup regains focus as soon as open() is called).
ComboBox {
// ...
popup: Popup {
property bool forceOpen: false
onClosed: {
if(forceOpen)
open()
}
contentItem: ListView {
// ...
footer: TextField {
onPressed: forceOpen = true
}
}
}
}
I also tried all values for the closePolicy property of the Popup, but none of them helped.
I am using Qt5.11. The forceOpen solution used to work with Qt 5.10, but does not anymore.
Your problem should be fixed if you do not accept the focus on the ComboBox:
ComboBox {
focusPolicy: Qt.NoFocus
popup: Popup {
// ...
}
}
I'm editing a component in Qt Creator. It suggested me to split the component in UI and not UI parts. My components exposes 2 custom properties.
ComponentViewForm {
property string step: '0'
property string setStep: '0'
}
A TextInput inside the UI-Part is bound to step
It should set the property setStep in onAccepted handler.
First one is easy. The binding can be edited in UI-Editor directly
But how do I implement the signal-handler of the child?
I've implemented it directly in the UI.
TextInput {
id: step
text: parent.step
onAccepted:
{
parent.setStep = text
}
}
It works, but Qt-Creator rejects to open it in UI-mode any more.
You can export the TextInput from your ComponentViewForm. There's a small Export button in the Navigator tab in Qt Quick UI forms editor. Assume that the id of TextInput is stepInput, ComponentViewForm.ui.qml should have an alias property property alias stepInput: stepInput in source code after you click the Export button.
You can implement property binding and signal handlers in ComponentView.qml like this:
ComponentViewForm {
property string step: '0'
property string setStep: '0'
stepInput.text: step
stepInput.onAccepted:
{
setStep = stepInput.text;
}
}
My QML application is displaying a Calendar element.
When the selected date is changed (clicked), I need to update an other element (an image).
I have something like this:
Calendar {
id: calCalendar
onClicked: {
// update other QML element
}
}
It works fine when the user click with the mouse on the calendar: the other element (the image) is correctly updated.
My problem is initialization : when my app is started, the calendar displays the current date by default, and I'd like to programmatically call the onClicked handler, to make sure the image is up to date.
I don't know how to do that.
If you want to do something when a QML component is done initializing you can use the Component.onCompleted : slot.
Calendar {
id: calCalendar
onClicked: {
// update other QML element
}
Component.onCompleted: {
// Do stuff for initialization.
// you could do this here : calCalendar.Clicked()
// if you want to use the same code for initialization and for user input handling later on.
}
}
The point is the following : onXXX : { declares a slot to handle the singal XXX. Here the signal is Clicked. You can trigger the signal programmatically as you say, just by invoking it like a function. You'll need to know a valid overload for the arguments (if any).
I'm using QtQuick 2.0 and and a QML ListView to display some items, and I need to know when the user chooses a different item. Emitting a signal when the user clicks a mouse area in the delegate works, i.e.
MouseArea{
onClicked: {
controller.itemChanged(model.item);
someList.currentIndex = index;
}
}
but only if the user uses the mouse to choose the item, but it doesn't work if the user uses the arrow keys.
I've been looking through the docs to find what signal is emitted when the currentIndex is changed, but I can't seem to find any. I'm looking for something similar to QListWidget::itemSelectionChanged() but it seems QML ListView doesn't have that.
You just need onCurrentItemChanged:{} in your ListView.
I ended up having to re-implement keyboard behaviour and exposing the model data from the delegate so I could fire the signal when a key is pressed.
ListView {
id: myList
focus: true
orientation: "Horizontal" //This is a horizontal list
signal itemChanged(var item)
interactive: false //Disable interactive so we can re-implement key behaviour
Keys.onPressed: {
if (event.key == Qt.Key_Left){
myList.decrementCurrentIndex(); //Change the current list selection
itemChanged(myList.currentItem.selectedItem.data); //Fire signal notifying that the selectedItem has changed
}
else if (event.key == Qt.Key_Right){
myList.incrementCurrentIndex(); //Change the current list selection
itemChanged(myList.currentItem.selectedItem.data); //Fire signal notifying that the selectedItem has changed
}
}
delegate: Component {
Rectangle {
id: myItem
property variant selectedItem: model //expose the model object
MouseArea {
anchors.fill: parent
onClicked: {
myList.currentIndex = index; //Change the current selected item to the clicked item
itemChanged(model.data); //Fire signal notifying that the selectedItem has changed
}
}
}
}
}
With this solution you have to manually change the item in QML whenever the user clicks an item or presses a key. I'm not sure this'd be an optimal solution with GridView but it works fine with ListView.
See this question. There are two approaches you can take
Connect to another component's event
Handle the event within that component
The signal handler is named on<SignalName> with the first letter of the signal in uppercase.