QML Dynamic Load Window On Button Press, But Focus Instead If Exists - qt

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();
}
}
}

Related

Qml ComboBox with TextField in Popup

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 {
// ...
}
}

Using QtQuick / QML Passively with C++

Veteran C++ and C#/WinForms programmer here, getting familiar with Qt/QML.
There's a lot of information on how to call into C++ from QML, and I've got that working just fine, but I'd rather treat QML passively and manipulate it from C++ when at all possible.
For example:
frmLogin::frmLogin()
{
// Load QML file
// Wire up controls to their own pointers
cmdOK = QtQuick_GetControlFromQML("cmdOK");
cmdQuit = QtQuick_GetControlFromQML("cmdQuit");
}
void frmLogin::Show()
{
MyQMLPointerToWindow->Show();
}
void frmLogin::DoSomethingFromCPP()
{
cmdOK->SetProperty("text", "I just changed the button text");
rectBox->SetProperty("visible", true); // rectBox Rectangle from QML now appears on the screen
frmMainMenu = new frmMainMenu(); // Create new main menu window
frmMainMenu->ShowDialog(); // modal display
}
Is there a way to do this? Is it highly discouraged? I'm trying to make a multi-form modal application. It's difficult to find straightforward answers on all of this because it seems like QtQuick has gone through several design iterations. Any advice is appreciated!
If you know the objectName of the item you're interested in, you can use QObject::findChild():
QQuickItem *okButton = findChild<QQuickItem*>("cmdOK");
If the button is declared as a property in QML:
Item {
id: item
property alias button: item.button
Button {
id: button
text: "OK"
}
}
Then you can access it as a property in C++:
QObject *button = property("button").value<QObject*>();
button->setProperty("text", "I just changed the button text");

How to know when the selected item has been changed in a QML QListView?

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.

How to add an event handler for dynamically created QML elements?

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.

Flex: How to determine if a PopUpManager window is open (or when it has closed)?

In Flex (Flash Builder 4) I am opening a new window via PopUpManager.addPopUp. I have timer code that runs in my component and I need to stop my timer when that window opens and start the timer again when the window closes.
I figure it's easy enough to stop the timer in the function that opens the window, but how can I start the timer again when the window closes?
Is there a way to tell if there is a pop-up window in front of my component, or if a specific pop-up window is still open via PopUpManager?
Maybe events are a better approach?
Thanks!
Events! is the way to go.
Fire events during launch/close. Add your logic in the event Handlers!
You can use following code to check whether opened popup window is getting closed or not.
if it is closed you can stop the timer.
//set the flag to find your popup window is exist or not.
private var isPopupExist:Boolean = false;
private function closePopUpWindow():void
{
var systemManager:SystemManager = FlexGlobals.topLevelApplication.systemManager;
//Returns a list of all children.
var childList:IChildList = systemManager.rawChildren;
for(var i:int=childList.numChildren-1;i>=0;i--)
{
var childObject:* = childList.getChildAt(i);
//If child object is Uicomponent.
if (childObject is UIComponent)
{
var uiComponent:UIComponent = childObject as UIComponent;
//If uicomponent is popup and class name is equal to **your popup component name** here i am using "ChatComp".
if (uiComponent.isPopUp && uiComponent.className == "ChatComp")
{
isPopupExist = true;
}
}
}
}
in your Timer,
private function checkPopUpExistance():void
{
call closePopUpWindow() function for every 1 sec or any seconds(your wish) to check whether popup is exist or not.
if(isPopupExist)
{
here you stop the timer.
}
}
Now you can start the Timer, when you opened the Popup window.
The popupmanager is a singleton class, so you can easily know how many popups have been created with his ChildList
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/managers/PopUpManagerChildList.html

Resources