3 ms·
This QML example is implementing the example from the article, and the article already implements this example in Svelte. However, the Svelte implementation bec
by onsclom 3y ago
This QML example is implementing the example from the article, and the article already implements this example in Svelte. However, the Svelte implementation becomes even more concise when you inline the functions:
<script>
let count = 0
</script>
<button on:click={()=>count--}>decrement</button>
<span>{count}</span>
<button on:click={()=>count++}>increment</button>
<button on:click={()=>setTimeout(()=>count++, 1000)}>
increment later
</button>
Play with this in the Svelte REPL here: https://svelte.dev/repl/97d42ad98e4b4a929d3c5a3de880e6fc?version=4.1.0 https://svelte.dev/repl/97d42ad98e4b4a929d3c5a3de880e6fc?ver...
Doing this, I also realized rubymamis cheated a good bit on their QML version. It's missing the text for the buttons. It's not wrapped in `ApplicationWindow { }`. And there's a bug with their implementation: If you clicked their "increment later" button many times quickly then it would keep resetting the timer and only increment once.
- rubymamis 3y agoHaha I was typing fast just to show the QML syntax. I don't really think good code is about lower amount of lines, but rather more about "making sense". And that's what I tried to show. Regarding the timer, so in your Svelte code a new timer is being created each time? I wonder how to implement the efficiently in QML. I don't think it's as straightforward. EDIT: Probably the best way is to expose QTimer::singleShot (C++) in QML.
- onsclom 3y agoFair gotcha, yeah honestly I find QML to be super intuitive in the same way Svelte is super intuitive. It feels like two framework authors in very different worlds came to a similar conclusion. > Regarding the timer, so in your Svelte code a new timer is being created each time? `setTimeout`[1] isn't even a Svelte thing, it's a JavaScript API that QML would have access to if it was real JavaScript ;). [1] https://developer.mozilla.org/en-US/docs/Web/API/setTimeout https://developer.mozilla.org/en-US/docs/Web/API/setTimeout
- rubymamis 3y agoI understand, but it's not quite necessary if Qt could add some better functionality exposing C++ code in QML. Luckily, someone created a cool library that allows you to create singleShot timer in QML as follows:[1] `lqtUtils.singleShot(5000, () => console.log("Hello!"))` [1] https://stackoverflow.com/a/75288105/5865379 https://stackoverflow.com/a/75288105/5865379 I also used his library to share ENUMs between QWidgets (C++) and QML.