Page 1 of 2 12 LastLast
Results 1 to 20 of 26

Thread: Use own Class in some Qt class

  1. #1
    Join Date
    Feb 2013
    Posts
    29
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Use own Class in some Qt class

    Hi,
    I start with example: i have for example QQuickView class and i want set my own instance of QQuickItem but how i know one way:
    implement own instance of QQuickView class and use own QQuickItem class. This is hard way and only way or is there some another way to do this?

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

    Default Re: Use own Class in some Qt class

    Please rephrase your question. It is hard to make out what you want.
    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
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Use own Class in some Qt class

    Indeed hard to understand the problem, but my guess is you are looking for qmlRegisterType()?

    Cheers,
    _

  4. #4
    Join Date
    Feb 2013
    Posts
    29
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Use own Class in some Qt class

    ok i want use my own instance of QQuickItem in QQuickView. For example:
    I make my class MyItem - public class Item : public QQuickItem
    QQuickView *view = new QQuickView();
    view->rootObject->itemsChild(); and i want that this will return QList<MyItem> items;

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

    Default Re: Use own Class in some Qt class

    Do you mean that you want to be able to put instances of your MyItem class into QtQuick scene or that all items in the scene are MyItem instances instead of QQuickItem instances?
    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.


  6. #6
    Join Date
    Feb 2013
    Posts
    29
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Use own Class in some Qt class

    all items in the scene are MyItem instances instead of QQuickItem instances
    this is waht i want, sorry for hard understanding but i realy didn't know how say it

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

    Default Re: Use own Class in some Qt class

    In that case that is not possible. Why do you want that?
    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.


  8. #8
    Join Date
    Feb 2013
    Posts
    29
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Use own Class in some Qt class

    Because when i did one program i wanted to storage friend QQuickItem. I this case i have to use objectUserData method, but i think if i make own class it will much better but this is only one example this happen me few times

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

    Default Re: Use own Class in some Qt class

    Quote Originally Posted by Ichi View Post
    Because when i did one program i wanted to storage friend QQuickItem.
    I don't understand what it means.

    I this case i have to use objectUserData method
    Why do you have to use that method (whatever it is)? And what does QQuickItem have to do with this?
    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.


  10. #10
    Join Date
    Feb 2013
    Posts
    29
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Use own Class in some Qt class

    In this program every QquickItem has friends. And i need to get this friend. But i can't call function which get friends every time when i need it so it is comfortable when you can customise this class

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

    Default Re: Use own Class in some Qt class

    Quote Originally Posted by Ichi View Post
    In this program every QquickItem has friends.
    What friends?

    But i can't call function which get friends every time when i need it so it is comfortable when you can customise this class
    I'm sorry, I don't understand you. I'm afraid I can't help.
    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.


  12. #12
    Join Date
    Feb 2013
    Posts
    29
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Use own Class in some Qt class

    ok i think it doesn't matter what friend or whatever.
    i simply wanted customise this class because in some cases it is much comfortable, i wanted add 2 or 3 methods this is all.

  13. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Use own Class in some Qt class

    i simply wanted customise this class because in some cases it is much comfortable, i wanted add 2 or 3 methods this is all.
    You can always derive a custom class from QQuickItem and add more methods to it.

    However the QQuickItem::childItems() will always return QList< QQuickItem * >. You need to do something like this:

    Qt Code:
    1. QList< QQuickItem * > children = root->childItems();
    2.  
    3. QListIterator< QQuickItem * > it( children );
    4. while ( it.hasNext() )
    5. {
    6. QQuickItem * pItem = it.next();
    7. MyItem * pMyItem = qobject_cast< MyItem * >( pItem );
    8. if ( pMyItem ) // check for NULL - if it is null, then the item was not a MyItem
    9. {
    10. pMyItem->callMyMethod();
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Use own Class in some Qt class

    qobject_cast will definitely fail here. He doesn't want to insert new items in the scene that are to be MyItem instances, he wants all items in the scene to magically be MyItem instances (because items in his scene have "friends") thus any cast other than a crude static_cast will fail. I don't understand the point of all this discussion, it seems to me OP just wants to do myItem->callMyMethod() instead of callMyMethod(myItem).
    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.


  15. #15
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Use own Class in some Qt class

    Well, it is really unclear.

    If the OP has a QtQuick item class and only creates those or subclasses of it, then the approach by d_stranz will of course work.

    Ichi, can you provide a QML snippet showing your use of QtQuick?

    Cjeers,
    _

  16. #16
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Use own Class in some Qt class

    If the OP has a QtQuick item class and only creates those or subclasses of it, then the approach by d_stranz will of course work.
    That's what I understood he was trying to say here:

    ok i want use my own instance of QQuickItem in QQuickView. For example:
    I make my class MyItem - public class Item : public QQuickItem
    QQuickView *view = new QQuickView();
    view->rootObject->itemsChild(); and i want that this will return QList<MyItem> items;

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

    Default Re: Use own Class in some Qt class

    I don't see him create any instances of MyItem in here. And he also said he wants all items to be MyItem instances. I assumed there is no QtQuick program that can work without even a single instance of any predefined elements.
    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.


  18. #18
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Use own Class in some Qt class

    Quote Originally Posted by wysota View Post
    I don't see him create any instances of MyItem in here.
    I assumed that the whole scene is created in QtQuick. For QtQuick2 (which we have here judging by class name) this is basically the only sensible way.
    Hence my request to see the QML part

    Quote Originally Posted by wysota View Post
    And he also said he wants all items to be MyItem instances. I assumed there is no QtQuick program that can work without even a single instance of any predefined elements.
    I don't see why it would not, i.e. I am not aware of any requirement to have a certain type for top level element, I assume it can be any QQuickItem subclass.

    Cheers,
    _

  19. #19
    Join Date
    Feb 2013
    Posts
    29
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Use own Class in some Qt class

    simply forgot all what i said i want customise QQuickItem (add 2 methods). And use this for all items in QQuickView.

  20. #20
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Use own Class in some Qt class

    OK, then you will want to use something like the code I posted:

    Qt Code:
    1. QList< QQuickItem * > children = root->childItems();
    2.  
    3. QListIterator< QQuickItem * > it( children );
    4. while ( it.hasNext() )
    5. {
    6. QQuickItem * pItem = it.next();
    7. MyItem * pMyItem = qobject_cast< MyItem * >( pItem );
    8. if ( pMyItem ) // check for NULL - if it is null, then the item was not a MyItem
    9. {
    10. pMyItem->callMyMethod();
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. show object (scene) in widget in class class mainwindow
    By rimie23 in forum Qt Programming
    Replies: 8
    Last Post: 1st May 2012, 16:15
  2. Replies: 1
    Last Post: 20th January 2012, 04:39
  3. Replies: 7
    Last Post: 18th August 2011, 14:43
  4. Replies: 3
    Last Post: 27th December 2008, 19:34
  5. Replies: 3
    Last Post: 16th May 2007, 11:07

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.