martedì 15 novembre 2016

QML trick: force re-evaluation of a property binding

Everyone who has used the QML programming language should love its property binding mechanism, which is made very easy to express via its declarative paradigm:


import QtQuick 2.0

Column {
  TextEdit {
    id: inputField
  }
  Text {
    id: secondLabel
    text: "You typed: " + inputField.text
  }
}

If you have worked on a project where QML was used along with C++ code, you might have bumped into a scenario where some bindings don't seem to work as you'd expect:


import QtQuick 2.0

Text {
  text: cppModel.get(row, "description")

  MyCppModel {
    id: cppModel
  }
}

Whenever the value of the row variable changes, the property binding is re-evaluated and the text is correctly updated; however, if row is not changed and it's only the cppModel contents to change, then you'll be out of luck: the text won't update accordingly.
This apparently unexplicable behaviour has in fact an obvious explanation: the QML engine re-evaluates the property bindings only when a variable appearing in the expression (or in a javascript function used in the expression) is modified. But since in this case the function get() is a C++ method, the QML engine is not aware of this method's internals and does not see any change requiring refreshing the binding.

The first thing we have to do in order to solve this problem is to find a signal which tells us when it's time to re-evaluate the bindings. If you are working with a model derived from QAbstractItemModel, then you probably want to bind to the dataChanged signal, or maybe modelReset (if you simply reset the whole model when updating it). In the general case, your C++ class should have a signal or a property which gets updated when needed. If not, add it! :-)

The trick I've been using in some occasions is then to artificially trigger a change in a property used in the binding, in order to force the refresh:

import QtQuick 2.0

Text {
  text: cppModel.get(row, "description")

  MyCppModel {
    id: cppModel
    onDataChanged: {
      var tmp = row
      row = -1
      row = tmp
    }
  }
}

I know, I know, this is horrible. It works, but it causes the expression to be re-evaluated twice, since we are changing the row property twice. Besides, chaning the row property might have some unwanted side effects in other objects, if this property is used in other property bindings. And what if the C++ method doesn't take any parameters at all? How can we trigger its re-evaluation then?

I recently came up with another solution, which is the reason of this blog post. And yes, it's still a hack, but an elegant one. It uses the rarely used comma operator, which in Javascript works in the same way as in C++: it evaluates all operands, and returns the value of the last one. So, this is what we can do:


import QtQuick 2.0

Text {
  text: cppModel.updateCounter, cppModel.get(row, "description")

  MyCppModel {
    id: cppModel
    property int updateCounter: 0
    onDataChanged: updateCounter++
  }
}

Note that we've added an extra property, updateCounter, which we'll change whenever we want the binding expression to be re-evaluated. In this case I've added the property to the QML component, but if you own the C++ object you could add it to your C++ object. And of course, if your C++ object already has a property which gets updated when you also want to update the binding, then you can use that instead: it can be of any type, it doesn't have to be an int.

I hope you'll find this tip useful. :-)

Etichette: , , ,

mercoledì 9 novembre 2016

Why Russians (and me) are cheering for Trump

Let me start by saying that I'm far from being a Trump fan, and that I believe that in this election the American people were voting between self destruction (Trump) and World War III (Clinton).
They could have voted for a far better candidate, whom the whole world would have envied them, that is Jill Stein. But the media and the propaganda campaign from the two main candidates ensured that people were just too worried of averting the risk of either self destruction or WW3 to take any other option into consideration. Hopefully time and experience will teach us, that in a democracy we must choose the option that we like the most, and not be calculators. But I digress.

Clinton's campaign has often hinted that Putin was cheering for Trump's victory. While the connection is not obvious at all, and the markets seems to think otherwise (now, as results are out, the Pound, Dollar and Ruble are down, while Euro is gaining), there's indeed some support for Donald Trump in the Russian media and people. And while I'm not Russian, I do share this feeling and I'd like to try to explain you the reasons. Given that I do not represent Russians, and that I might be completely misreading their thought, I'm going to use the first person from now on; so if you don't like what you read, just blame me. :-)

First of all, Trump has made some positive statements in the topics of Russia, and world peace in general, by appearing to be willing to rethink NATO's role. He also made clear that Russia would not have taken Crimea from Ukraine, had he been president, a statement which I like to read in the sense that he would have not supported the Maidan coup in the first place. And generally, Trump appears not to be fond on military interventions all other the world.

Secondly, while Americans and the West in general regard at the U.S. as the most perfect democracy, I do have a much more disillusioned view of democracy. Impopular laws are all over the world, referendums are held and then ignored (like the Greek bailout referendum in 2015, most referendums in Italy, and possibly the Brexit one too), decisions are being taken in secret and/or without any public debate (EU association agreement with Ukraine, EU sanctions against Russia, EU agreement on refugees with Turkey, etc.). Given that I'm convinced that Trump will simply not be allowed to be the U.S. president, as Julian Assange recently said, or that he'll have to radically change his positions on NATO and the finacial establishment if he wants to keep the power, it will be interesting to see how this "perfect democracy" will twist its rules or its president in order to overturn the legitimate electoral result. The hope is that this will help opening people's eyes to how our democracies are really being controlled by the big powers.

Third, the fact that Trump is despised by most intellectuals and politicians in the West is a perfect opportunity for Western countries to rethink their relationship with the U.S., and switch from a complete subjection to a more critical stance. Which does not mean that we should be hostile to the U.S., please do not misunderstand me on that, but simply stop blindly obeying any order coming from there and start thinking of our own interests.

These are my hopes; but to be realist, the most likely thing is that Trump will simply mutate his positions and become a puppet of the big powers and continue preserving the status quo, while continue screaming against Mexicans and immigrants, and continue with the current imperialistic policies and with the exploitable-poor-creating-machine (i.e., "war", which creates floods of cheap labour while we steal their countries' resources) all around the world. And with the media calmly telling us that our vote counts.

Etichette: , , , , ,

venerdì 4 novembre 2016

Le NATO del Est

Io ha trovate un breve interview, registrate alicun menses retro per le television italian, a un ex commandante del NATO. Io vos lo propone, in su original in italiano (il ha anque subtitulos in anglese):


Fabio Mini esseva le commandante del fortias NATO in Kosovo. On pote leger plus informationes super ille in su pagina de Wikipedia in italiano o in iste sito in anglese.

Etichette: , , ,

sabato 29 ottobre 2016

Europa e Islam

(iste articulo es scripte in interlingua, le lingua auxiliar international)

Hodie io legeva un articulo in le sito de Reuters, qui me faceva pensar un poco:

 Lege le articulo in Reuters
Io recommenda que vos lo lege integremente (clicca ci pro vader al articulo); si vos non comprende le anglese, io va synthetisar lo multo brevemente. Alicun refugiatos de Syria trova que le moscheas in Germania es troppo conservative: illos insenia un Islam multo radical, con un interpretation multo litteral del Corano, simile a illo que veni inseniate in le Wahabismo o Salafismo.

Io es convincite que Europa debe esser tolerante de tote religiones que es compatibile con nostre leges. Refugiatos qui arriva a Europa es sovente le victimas de nostre guerras o disfrutamento economic, e nos ha le responsabilitate de offerer les un compensation. Si nos ha destruite lor casa in lor pais, nos debe offerer les un altere casa. E nos debe permitter a illes de professar lor religion.

Le construction de Moscheas in Europa es un cosa que io reguarda positivemente, ma nos non pote abandonar le musulmanos a se mesme. Nos debe laborar con illes e assecurar nos que le Islam que es inseniate in nostre moscheas es compatibile e respectose de nostre valores. Certemente, un interpretation litteral del Corano, qui promove le Jihad (guerra sancte), non es compatibile con nostre leges.

Alicuno poterea pensar que le solution al problema es simplemente prohiber le construction de nove moscheas, ma isto portarea a un resultato totalmente opposite, como jam ha essite verificate in Italia (e io crede, in multe altere partes de Europa): le communitates islamic devenirea ancora plus isolate e impossibile a controlar; le inseniamento del Islam poterea prender formas ancora plus radical.

Europa necessita Moscheas aperite.

Etichette: , , , , ,