Results 1 to 10 of 10

Thread: Extending existing QML components

  1. #1
    Join Date
    Nov 2012
    Posts
    21
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Extending existing QML components

    We have a class which is derived from QTextEdit and overrides all manner of things (e.g. createMimeDataFromSelection()), and we have recently come up with a use case to move such a thing into a view which would be great to do in QML. However after 30 mins looking at the source it is fairly evident that the QQuickTextEdit is not really meant to be extended. Given that you can't embed a QWidget into a QtQuick2 interface, are we stuck doing it in QtQuick1?

    It seems rather short sighted to think that the standard implementation of the textedit will suit everyone, but the only alternative at the moment is to completely reimplement the entire thing yourself?! Or have I missed something?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,369
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Extending existing QML components

    A possible solution would be to refactor your widget into two separate components -- one would be the original QTextEdit and the other would be all your modifications encapsulated into a separate class with signals, slots and properties. Then you can reuse this component with Qt Quick's text edit item by providing a QML bridge between this C++ component and the item itself.

    e.g.

    javascript Code:
    1. // MyTextEdit.qml
    2. import QtQuick 2.0
    3. import TextEditEngine 1.0
    4.  
    5. TextEdit {
    6. TextEditEngine { id: engine; }
    7.  
    8. onSelectedTextChanged: engine.createMimeDataFromSelection(selectedText)
    9. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2012
    Posts
    21
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extending existing QML components

    Ah thats an interesting idea. Of course you don't want to create the mime data every time you change the selection, and you also have to then somehow hook into the cut event and get it to call through to your engine to put it into the clipboard. Looks like you'd have to intercept the ctrl-x key directly - can we even do that? Not in a default TextEdit by the look of it... putting a textedit in a keys component also doesn't sound like the right thing to do... little help?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,369
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Extending existing QML components

    Quote Originally Posted by gemmell View Post
    and you also have to then somehow hook into the cut event and get it to call through to your engine to put it into the clipboard. Looks like you'd have to intercept the ctrl-x key directly - can we even do that?
    Crtl+X (or whatever the shortcut is on a particular platform) is only one of the ways of invoking cut functionality. If that's the only thing you are after then using the Keys attached property should work just fine. On a more global scale one would probably want some kind of shortcut mechanism to handle situations such as these. Unfortunately QtQuick is not really tailored towards complex UI operations such as those that often happen on desktop so if you want them, it is important to come up with a framework that you can reuse instead of implementing the same things over and over. In particular it could be an option to implement an attached property (similar to the "Keys" property) that would handle keyboard shortcuts on existing objects.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2012
    Posts
    21
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extending existing QML components

    At least if I stick with QtQuick1 I'll be able to drop in my extended QTextEdit and have it work pretty much straight up.... might give that a shot.
    Last edited by gemmell; 6th March 2013 at 12:25.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,369
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Extending existing QML components

    Quote Originally Posted by gemmell View Post
    At least if I stick with QtQuick1 I'll be able to drop in my extended QTextEdit and have it work pretty much straight up.... might give that a shot.
    If you do that, don't expect good efficiency.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Nov 2012
    Posts
    21
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extending existing QML components

    Quote Originally Posted by wysota View Post
    ... Unfortunately QtQuick is not really tailored towards complex UI operations such as those that often happen on desktop so if you want them, it is important to come up with a framework that you can reuse instead of implementing the same things over and over. In particular it could be an option to implement an attached property (similar to the "Keys" property) that would handle keyboard shortcuts on existing objects.
    Right, that answers a few questions for me as to whether it's suitable. I gather it's totally doable but that you require a fair bit of expertise to design frameworks and bend QML to your will. Given that I'm a QML noob I think I'd struggle to develop such frameworks at this point in time.

    Quote Originally Posted by wysota;
    If you do that, don't expect good efficiency.
    But no worse than regular QWidget based one using the Qt Animation Framework right? Anyways, it's going to be small, displaying 50 or so items, shouldn't be a problem for any technology.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,369
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Extending existing QML components

    Quote Originally Posted by gemmell View Post
    But no worse than regular QWidget based one using the Qt Animation Framework right?
    Much worse because you have to go through a widget proxy.

    Anyways, it's going to be small, displaying 50 or so items, shouldn't be a problem for any technology.
    Well... don't be so sure Widgets on graphics view (which is what you're going to use with QtQuick1) are sort of a nasty hack. If you start moving things around, they'll get really slow.

    First question you should ask yourself is whether you really need (and can make use of) QtQuick (be it 1 or 2). So why do you think you want/need QtQuick?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Nov 2012
    Posts
    21
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extending existing QML components

    Quote Originally Posted by wysota View Post
    Much worse because you have to go through a widget proxy.


    Well... don't be so sure Widgets on graphics view (which is what you're going to use with QtQuick1) are sort of a nasty hack. If you start moving things around, they'll get really slow.

    First question you should ask yourself is whether you really need (and can make use of) QtQuick (be it 1 or 2). So why do you think you want/need QtQuick?
    Yes, I think that first question is turning into a "no I don't need it". I originally thought to do it in quick since we have a QAbstractItemProxyModel based model which is a list and rather than stuffing around with delegates and widget's for index's thought I'd give QML a try since it's apparently easy to do this sort of stuff..... but thank you for your feedback, I'm going to do it with QWidget's - as a side benefit I know wtf I'm doing there so it'll get finished quicker.

    In terms of the system wide shortcut stuff you mentioned, I came across this:
    http://kdeblog.mageprojects.com/2012...out-qshortcut/
    Which essentially allows you to setup you keypresses (e.g. ctrl+c/ctrl+v) and setup reactions in the elements.

    In another post he goes on to add a clipboard object that you can use from QML:
    http://kdeblog.mageprojects.com/2012...apper-for-qml/
    Sounds like the ground work is already being done.
    Last edited by gemmell; 7th March 2013 at 01:08.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,369
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Extending existing QML components

    Quote Originally Posted by gemmell View Post
    Yes, I think that first question is turning into a "no I don't need it". I originally thought to do it in quick since we have a QAbstractItemProxyModel based model which is a list and rather than stuffing around with delegates and widget's for index's thought I'd give QML a try since it's apparently easy to do this sort of stuff..... but thank you for your feedback, I'm going to do it with QWidget's - as a side benefit I know wtf I'm doing there so it'll get finished quicker.
    As a semi-solution I'd suggest using QScrollArea. Not that elegant but let's you arrange widgets quickly.

    In terms of the system wide shortcut stuff you mentioned, I came across this:
    http://kdeblog.mageprojects.com/2012...out-qshortcut/
    Which essentially allows you to setup you keypresses (e.g. ctrl+c/ctrl+v) and setup reactions in the elements.
    Oh, I didn't mean system-wide (or actually application-wide) shortcuts. What you want is highly context-dependent.

    In another post he goes on to add a clipboard object that you can use from QML
    You don't really need that because you have a C++ component that does all the work (at least I assume that's what your createMimeSomethingSomething does). If you have working C++ logic, there is no point in rewriting it in QML.

    And personally I don't like those approaches from the blog you quoted. 10 shortcuts mean 10 event filters on the application object, that's not a good design.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Extending MessageBox
    By thru in forum Qt Programming
    Replies: 7
    Last Post: 17th December 2010, 16:31
  2. Extending QtTest lib
    By davif in forum Newbie
    Replies: 5
    Last Post: 4th November 2010, 19:45
  3. Extending QTimer()
    By rishid in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2009, 01:59
  4. Extending QGraphicsLayout
    By Darkman in forum Qt Programming
    Replies: 0
    Last Post: 13th March 2009, 12:09
  5. extending QHttp?
    By gfunk in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2007, 08:41

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.