Results 1 to 20 of 30

Thread: Custom Property On Custom Widget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Custom Property On Custom Widget

    May be I asked a wrong question, we should be looking at Extension class, did you properly implement these
    Qt Code:
    1. virtual QVariant property ( int index ) const = 0
    2. virtual QString propertyGroup ( int index ) const = 0
    3. virtual QString propertyName ( int index ) const = 0
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2011
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom Property On Custom Widget

    Hi
    I have not found very clear explanation of implementation of these function in the documenatation .so can provide me some stuff on this

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Custom Property On Custom Widget

    Most of the functions should be clear enough, provided you understand how Qt Designer works. What did you implement in the virtual functions of Extension class?

    All these virtual functions should be implemented properly for the widget custom properties to be displayed in Qt Designer.

    for example if your widget has 2 QString properties "PRO1", "PRO2" then implementation would like this,

    Qt Code:
    1. int count() const { return 2; }
    2.  
    3. QVariant property(int index) const
    4. {
    5. if(index == 0) return pro1;
    6. else if(index == 1) return pro2;
    7. else return QVariant;
    8. }
    9.  
    10. QString propertyName(int index) const
    11. {
    12. if(index == 0) return QString("PRO1");
    13. else if(index == 1) return QString("PRO2");
    14. else return QString;
    15. }
    To copy to clipboard, switch view to plain text mode 

    note you need to implement all the functions properly..
    Last edited by Santosh Reddy; 27th June 2011 at 12:04.

  4. #4
    Join Date
    Feb 2011
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom Property On Custom Widget

    Hi
    very very Thanks. I think that now i can implement the Custom (Compound Property ) On the Widget .

  5. #5
    Join Date
    Feb 2011
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom Property On Custom Widget

    Hi,
    If I implement my own property sheet extension so in this case i will miss the default property editor given by the widget or the qt designer . But i want to use the both property editor the one provided by the QT designer and other which is written by me So is there any way

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Custom Property On Custom Widget

    I not very sure, but this should work, as per documentation

    Quote Originally Posted by Qt Documnetation
    "When implementing a custom widget plugin, a pointer to Qt Designer's current QDesignerFormEditorInterface object is provided by the QDesignerCustomWidgetInterface::initialize() function's parameter."
    From the QDesignerFormEditorInterface object, get the previous property editor interface instance, and map the properties in your class, like map 0 to n properties from previous property editor, and n to m are your custom properties, as you know you can get and set the 0 to n properties using previous property editor interface instance

  7. #7
    Join Date
    Feb 2011
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom Property On Custom Widget

    Hi
    The Problem is that how can i map those property becoz In the QDesignerPropertyEditorInterface No method is there ,also if i will go for meta object and get the Property through meta object i am not sure it will work .However i am doing work on this
    By the way Thanks For Reply Sir

  8. #8
    Join Date
    Jun 2012
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom Property On Custom Widget

    If I implement my own property sheet extension so in this case i will miss the default property editor given by the widget or the qt designer . But i want to use the both property editor the one provided by the QT designer and other which is written by me So is there any way
    @Ashutosh2k1

    I had to solve the same issue. Here is what i came up with after several experiments.

    First declare a custom property sheet factory:

    Qt Code:
    1. class CCustomWidgetPropertySheetFactory : public QExtensionFactory
    2. {
    3. typedef QExtensionFactory inherited;
    4.  
    5. public:
    6. CCustomWidgetPropertySheetFactory(QExtensionManager * apParent);
    7.  
    8. protected:
    9. virtual QObject * createExtension(QObject * apObject, const QString & arIId, QObject * apParent) const;
    10.  
    11. private:
    12. QExtensionManager * mpExtensionManager;
    13. mutable bool mCreateExtensionCalled;
    14. };
    To copy to clipboard, switch view to plain text mode 

    ...then add the implementation of the factory...

    Qt Code:
    1. CCustomWidgetPropertySheetFactory::CCustomWidgetPropertySheetFactory(QExtensionManager * apParent)
    2. : inherited(apParent)
    3. , mpExtensionManager(apParent)
    4. , mCreateExtensionCalled(false)
    5. {}
    6.  
    7. QObject * CCustomWidgetPropertySheetFactory::createExtension(QObject * apObject, const QString & arIId, QObject /* QExtensionFactory */ * apParent) const
    8. {
    9. QObject * lResult(0);
    10.  
    11. if (!mCreateExtensionCalled)
    12. {
    13. // note: setting flag to avoid infinite loop caused by later usage of qt_extension<>
    14. mCreateExtensionCalled = true;
    15.  
    16. if (arIId == Q_TYPEID(QDesignerPropertySheetExtension))
    17. {
    18. CCustomWidget * lCustomWidget(qobject_cast<CCustomWidget *>(apObject));
    19. if (lCustomWidget)
    20. {
    21. QDesignerPropertySheetExtension * lDefaultPropertySheetExtensionImplementor(qt_extension<QDesignerPropertySheetExtension *>(mpExtensionManager, apObject));
    22. if (lDefaultPropertySheetExtensionImplementor)
    23. {
    24. lResult = new CCustomWidgetPropertySheetExtension(apParent, *lDefaultPropertySheetExtensionImplementor);
    25. }
    26. }
    27. }
    28.  
    29. mCreateExtensionCalled = false;
    30. }
    31.  
    32. return lResult;
    33. }
    To copy to clipboard, switch view to plain text mode 

    So far we created a custom property sheet and pass the default property sheet to it. Now declare the custom property sheet (i simplified the code for better understanding here):

    Qt Code:
    1. class CCustomWidgetPropertySheetExtension : public QObject, public QDesignerPropertySheetExtension
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. CCustomWidgetPropertySheetExtension(QObject * apParent,
    7. QDesignerPropertySheetExtension & apDefaultPropertySheetExtension);
    8. virtual ~CCustomWidgetPropertySheetExtension();
    9.  
    10. virtual int count() const;
    11.  
    12. virtual int indexOf(const QString &name) const;
    13.  
    14. virtual QString propertyName(int index) const;
    15.  
    16. virtual QString propertyGroup(int index) const;
    17.  
    18. virtual bool isVisible(int index) const;
    19.  
    20. virtual QVariant property(int index) const;
    21.  
    22. ...
    23.  
    24. private:
    25. QDesignerPropertySheetExtension & mpDefaultPropertySheetExtension;
    26. int mPropertyCount;
    27. };
    To copy to clipboard, switch view to plain text mode 

    ...and add the implementation of the custom property sheet...

    Qt Code:
    1. CCustomWidgetPropertySheetExtension::CCustomWidgetPropertySheetExtension(QObject * apParent,
    2. QDesignerPropertySheetExtension & apDefaultPropertySheetExtension)
    3. : QObject(apParent)
    4. , mpDefaultPropertySheetExtension(apDefaultPropertySheetExtension)
    5. , mPropertyCount(mpDefaultPropertySheetExtension.count() + 1)
    6. {}
    7.  
    8. CCustomWidgetPropertySheetExtension::~CCustomWidgetPropertySheetExtension()
    9. {}
    10.  
    11. ...
    12.  
    13. int CCustomWidgetPropertySheetExtension::count() const
    14. {
    15. return mPropertyCount;
    16. }
    17.  
    18. int CCustomWidgetPropertySheetExtension::indexOf( const QString& name ) const
    19. {
    20. if (name == "MyCustomProperty")
    21. return mPropertyCount - 1;
    22. else
    23. return mpDefaultPropertySheetExtension.indexOf(name);
    24. }
    25.  
    26. QString CCustomWidgetPropertySheetExtension::propertyName( int index ) const
    27. {
    28. if (index == mPropertyCount - 1)
    29. return "MyCustomProperty";
    30. else
    31. return mpDefaultPropertySheetExtension.propertyName(index);
    32. }
    33.  
    34. QString CCustomWidgetPropertySheetExtension::propertyGroup( int index ) const
    35. {
    36. if (index == mPropertyCount - 1)
    37. return "MyCustomPropertyGroup";
    38. else
    39. return mpDefaultPropertySheetExtension.propertyGroup(index);
    40. }
    41.  
    42. bool CCustomWidgetPropertySheetExtension::isVisible( int index ) const
    43. {
    44. if (index == mPropertyCount - 1)
    45. return true;
    46. else
    47. return mpDefaultPropertySheetExtension.isVisible(index);
    48. }
    49.  
    50. QVariant CCustomWidgetPropertySheetExtension::property( int index ) const
    51. {
    52. if (index == mPropertyCount - 1)
    53. return QVariant(true);
    54. else
    55. return mpDefaultPropertySheetExtension.property(index);
    56. }
    To copy to clipboard, switch view to plain text mode 

    Compile and you will see the custom property "MyCustomProperty" displayed in the designer in the property group "MyCustomPropertyGroup" for the widget "CCustomWidget".

    Hope this helps.

    So lonG
    Daniel

  9. The following user says thank you to ViRuSTriNiTy for this useful post:

    Olumide (23rd October 2014)

  10. #9
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    2

    Default Re: Custom Property On Custom Widget

    Hello Daniel, thanks for your post. I've tried to follow your example. Unfortunately the cast

    Qt Code:
    1. QDesignerPropertySheetExtension* defaultPropertySheetExtension = qt_extension<QDesignerPropertySheetExtension*>( m_extensionManager , object );
    To copy to clipboard, switch view to plain text mode 

    makes Qt Designer crash.

  11. #10
    Join Date
    Jun 2012
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom Property On Custom Widget

    ...makes Qt Designer crash.
    What kind of crash exactly? Do you get a stack strace, exception message or anything like that?

  12. #11
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    2

    Default Re: Custom Property On Custom Widget

    Quote Originally Posted by ViRuSTriNiTy View Post
    What kind of crash exactly? Do you get a stack strace, exception message or anything like that?
    I've just found the cause of the problem. I did not guard against recursive calls to createExtension(). I've just done so however and I no longer get crashes, although I used a static variable like so

    Qt Code:
    1. QObject* CustomWidgetFactory::createExtension(QObject *object, const QString &iid, QObject* parent ) const
    2. {
    3. static bool extensionSheetIsInitialized = false;
    4.  
    5. if( !extensionSheetIsInitialized )
    6. {
    7. extensionSheetIsInitialized = true;
    8. ...
    9. }
    10.  
    11. return 0;
    12. }
    To copy to clipboard, switch view to plain text mode 

    There is now the larger issue of how to proceed. How do I assign my indexes to my widget properties? So that I can control whether they are shown in the the properties editor? Ideally what I'd like to do is to display a subset of relevant properties of the widget depending on the value of a master property. For example, the master property could represent the shape of the wigdet while the remaining properties could include radius (for circles), length for squared etc, where the radius property is displayed only of the mater property is set to circle.

  13. #12
    Join Date
    Jun 2012
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom Property On Custom Widget

    Quote Originally Posted by Olumide View Post
    I've just found the cause of the problem. I did not guard against recursive calls to createExtension()
    Yep, thats why i added the red comment to mCreateExtensionCalled, back then i stumble upon this recursion too.

    Quote Originally Posted by Olumide View Post
    How do I assign my indexes to my widget properties? So that I can control whether they are shown in the the properties editor? Ideally what I'd like to do is to display a subset of relevant properties of the widget depending on the value of a master property. For example, the master property could represent the shape of the wigdet while the remaining properties could include radius (for circles), length for squared etc, where the radius property is displayed only of the mater property is set to circle.
    The only solution i had found was to use a "index"-"property name" map. With that in mind i would suggest you

    1) enumerate all properties of your widget in the constructor of the property sheet class
    2) remember those properties in the "index"-"property name" map
    3) override each virtual function of the property sheet and return the appropriate property values by using the "index"-"property name" map

    I'm sorry that i cannot post any additional code because it's lost. But i closely remember what i was doing back then.

Similar Threads

  1. Custom property in designer
    By avgust in forum Newbie
    Replies: 1
    Last Post: 9th September 2010, 10:14
  2. File dialog for custom widget property?
    By TheJim01 in forum Newbie
    Replies: 1
    Last Post: 6th May 2010, 10:14
  3. Animatin a custom property
    By Luc4 in forum Qt Programming
    Replies: 2
    Last Post: 13th April 2010, 08:37
  4. Replies: 4
    Last Post: 5th March 2010, 14:20
  5. Replies: 5
    Last Post: 16th May 2006, 20:38

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.