Results 1 to 12 of 12

Thread: How does qdesigner save non-QObject, such as QRect?

  1. #1
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How does qdesigner save non-QObject, such as QRect?

    Hi,

    I are trying to understand how QRect is saved by qdesigner. It is not a QObject, how does it know the properties of the QRect? and how it can restore those properties?

    Thanks
    Last edited by lni; 15th January 2009 at 16:40.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How does qdesigner save non-QObject, such as QRect?

    Have a look at the .ui file . Open in some editor, or notepad.
    You will get the idea (hint :xml )

  3. #3
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How does qdesigner save non-QObject, such as QRect?

    Quote Originally Posted by aamer4yu View Post
    Have a look at the .ui file . Open in some editor, or notepad.
    You will get the idea (hint :xml )
    I have read those files...

    My questions is when designer saves a QRect object, how does it know that QRect has x, y, width and height properties? It is not an QObject, it has no Q_PROPERTY. Nothing in QRect class tell outside about its properties...

    I could have a MyRect that has x1, x2, y1, y2, will qdesigner save it if I create MyRect class the same way as QRect?

    Somewhere and somehow, x/y/width/height in QRect must be hard-coded in qdesigner for QRect to be saved and restored, I look at QVariant source and didn't find useful hint either.

  4. #4
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How does qdesigner save non-QObject, such as QRect?

    Ok, I found a DomRect at tools/designer/src/lib/uilib/ui4_p.h

    But why clone the member field of QRect in DomRect?

    Why not do like

    Qt Code:
    1. class DomRect {
    2.  
    3. ...
    4. inline int elementX() const { return rect.x();}
    5. void setElementX(int a) {
    6. m_children |= X;
    7. rect.setX( a );
    8. }
    9. ...
    10.  
    11. private:
    12.  
    13. QRect rect;
    14. };
    To copy to clipboard, switch view to plain text mode 

    More over, from what I see, for every data object class, I have to have a Dom class to come with it? That is like double work load...

    I have been looking for a generic Dom that works for all data object, which does not have to derive from QObject...

    Well, I think Qt could provide a template schema class in this case, such as
    schema.addSchemaItem<T>( tag, obj, readMemberFunction, writeMemberFunction...)

    Can this be achived?

  5. #5
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How does qdesigner save non-QObject, such as QRect?

    Designer doesn't save QRect objects, it only works on QWidgets and some closely related classes.

  6. #6
    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: How does qdesigner save non-QObject, such as QRect?

    Designer might have used QDataStream to serialize all the objects, but its creators wanted the file format (ui) to be based on XML and not an internal format thus they had to provide operators for storing properties of those data objects in XML, hence the doubled work.

  7. #7
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How does qdesigner save non-QObject, such as QRect?

    Quote Originally Posted by wysota View Post
    Designer might have used QDataStream to serialize all the objects, but its creators wanted the file format (ui) to be based on XML and not an internal format thus they had to provide operators for storing properties of those data objects in XML, hence the doubled work.
    Sure, but I think if Qt provide some light-weighted base class for savable object and some Xml schemas, then we could reuse some of designer's codes. And designer itself could have a better looking parser. This would reduce a lot of code redundancy.

    For instance
    Qt Code:
    1. class QSavableClass {
    2. public:
    3.  
    4. virtual ~QSavableClass();
    5. };
    6.  
    7. class QXmlSchemaBase {
    8.  
    9. }
    10.  
    11. template <class Type>
    12. class QXmlSchemaItem : public QXmlSchemaBase {
    13. public:
    14. typedef const Type& (QSavableClass::*Getter)() const;
    15. typedef void (QSavableClass::*Setter)( const Type& );
    16.  
    17. QXmlSchemaItem( const QString& tag, QSavableClass& obj, Getter, Setter );
    18.  
    19. };
    20.  
    21. class QXmlSchema : public QXmlSchemaBase {
    22.  
    23. template <typename T>
    24. void addSchemeItem( const QXmlSchemaItem& );
    25.  
    26. void addScheme( const QXmlSchema& );
    27.  
    28. };
    29.  
    30. Now it becomes easier for me:
    31.  
    32. class MyClass : public QSavableClass {
    33. public:
    34.  
    35. const QString& str() const {
    36. return _str;
    37. }
    38.  
    39. void setString( const QString& str ) {
    40. _str = str;
    41. }
    42.  
    43. private:
    44.  
    45. QString _str;
    46. }
    To copy to clipboard, switch view to plain text mode 
    I can now create a schema for MyClass by deriving from QXmlSchema and add the schema items accordingly, and much of the designer codes can be re-used and simplified...Any comments?

    Also, looking at DomUI, I wonder why it has to parse the entire content first and then create the objects. Can we populate the objects while parsing the file? Is there a particular reason for what has been done in qdesigner's parser?

  8. #8
    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: How does qdesigner save non-QObject, such as QRect?

    Quote Originally Posted by lni View Post
    Sure, but I think if Qt provide some light-weighted base class for savable object and some Xml schemas, then we could reuse some of designer's codes. And designer itself could have a better looking parser. This would reduce a lot of code redundancy.
    You can't expect to snap your fingers and classes to emerge magically from the vastness of the Universe. There is a class developed for serializing QObject properties as XML but it's not part of Qt. There is nothing that should prevent you from developing your own set of classes and share it with others but it seems that Qt developers have different goals right now.
    Now it becomes easier for me:

    Qt Code:
    1. class MyClass : public QSavableClass {
    2. public:
    3.  
    4. const QString& str() const {
    5. return _str;
    6. }
    7.  
    8. void setString( const QString& str ) {
    9. _str = str;
    10. }
    11.  
    12. private:
    13.  
    14. QString _str;
    15. }
    To copy to clipboard, switch view to plain text mode 
    Yeah, but then every class would have to be derived from QSavableClass which immediately loses the point of having such class.

    Also, looking at DomUI, I wonder why it has to parse the entire content first and then create the objects.
    That's how DOM works.

    Can we populate the objects while parsing the file? Is there a particular reason for what has been done in qdesigner's parser?
    Yes, but not using DOM. What's wrong with creating objects when the whole tree is ready anyway? UI files are quite short, so performance is not an issue here.
    Last edited by wysota; 16th January 2009 at 18:21.

  9. #9
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How does qdesigner save non-QObject, such as QRect?

    Quote Originally Posted by wysota View Post
    share it with others
    I am in no way to be good at XML things, that is why I need help...

    But I think perhaps we can make it better by having some base classes. I am trying to develop a shared object system, each object will have a unique object name to identify it. So a base class containing name is reasonable.

    In the example attached, I am having trouble to handle Tool within Modeling object. Can someone find a better solution by telling Modeling::tool and Modeling::setTool methods to the schema?

    Thanks
    Attached Files Attached Files

  10. #10
    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: How does qdesigner save non-QObject, such as QRect?

    Quote Originally Posted by lni View Post
    But I think perhaps we can make it better by having some base classes.
    Base classes are bad. They are opposite of "fast"

    I am trying to develop a shared object system, each object will have a unique object name to identify it. So a base class containing name is reasonable.
    Just don't reinvent the wheel.

    In the example attached, I am having trouble to handle Tool within Modeling object. Can someone find a better solution by telling Modeling::tool and Modeling::setTool methods to the schema?
    How about writing a proper serialization mechanism using QXmlStreamReader and QXmlStreamWriter?

    There are some design patterns you might use to get a neat solution.

  11. #11
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How does qdesigner save non-QObject, such as QRect?

    Quote Originally Posted by wysota View Post
    How about writing a proper serialization mechanism using QXmlStreamReader and QXmlStreamWriter?

    There are some design patterns you might use to get a neat solution.
    I would very much appreciate If you can point me to some better solution, I am really struggling with XML things.

    Basically, I have a list of classes that contain QExplicitlySharedDataPointer. Some of the objects may be shared by several other objects. Objects are identified by name, so the requirements are that each object will have a unique name. Those objects are stored in a dictionary (a singleton dictionary global to the application), with its name as lookup key.

    I need to save the objects in the dictionary, keep the object sharing information, and I need to restore them at a later time.

    The requirements are almost the same as qdesigner, except I need shareable objects.

    Are you saying that the current method in qdesigner to save/restore ui file is the right way, that each class needs to have a corresponding Dom class, and the Dom class needs to have all the properties that the class has? It seems too much work in there. The example I have only requires a very light-weight base class, and there won't be more than a few hundreds of such objects in an application...

  12. #12
    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: How does qdesigner save non-QObject, such as QRect?

    I didn't say what is the right or the wrong way of doing things. I said there is nothing wrong in the way Designer saves forms to a file. As I said, there are some design patterns you might use to delegate serialization from some external object to some other object (like the serialized class itself). If you want things to be fast, I'd advise against using XML. I'd use QDataStream instead. All you need to do then is to implement operator<< and operator>> between your class and QDataStream. See docs for details.

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.