Results 1 to 5 of 5

Thread: How to set Font for widgets in the parent from a child widget

  1. #1
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default How to set Font for widgets in the parent from a child widget

    Dear all,

    I've a mainwindow M with some widgets L and a Custom-dropdown D which contains list of Fonts.
    I need to set font for all the widgets in M if dropdown D is changed.
    I tried to set the font which resulted in Segmentation fault.


    Qt Code:
    1. void UpdateFont()
    2. {
    3. QObject *obj = this->parent();
    4. QObjectList objlistChildren = obj->children();
    5. int iCount = objlistChildren.count();
    6.  
    7. for(int iIndex = 0; iIndex < iCount; iIndex++)
    8. {
    9. QObject *temp = objlistChildren[iIndex];
    10. ( (QWidget*)objlistChildren[iIndex] )->setFont(font);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 


    How can i access the parent widgets and set its font from the child control.

    Thanks in advance for your suggestions.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to set Font for widgets in the parent from a child widget

    Not tested, but shouldn't do QApplication::setFont() the trick?

    EDIT: No, only changes the default font... sorry.

    EDIT EDIT: Umpf... Yes it works!
    Last edited by Lykurg; 7th January 2013 at 13:35.

  3. The following user says thank you to Lykurg for this useful post:

    ramsailesh (7th January 2013)

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

    Default Re: How to set Font for widgets in the parent from a child widget

    1. Emit a signal from the child widget (L) to indicate that font has changed, eg. emit fontChanged(font);
    2. Connect this signal to the Mainwindow's (M) slot where you set the font, eg. M->setFont(font);
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. The following user says thank you to Santosh Reddy for this useful post:

    ramsailesh (7th January 2013)

  6. #4
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to set Font for widgets in the parent from a child widget

    Thanks Lykurg and SantoshReddy.

  7. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to set Font for widgets in the parent from a child widget

    Your segmentation faults come from assuming pointers are valid and point to things of certain types:
    Qt Code:
    1. void Class::UpdateFont()
    2. {
    3. QObject *obj = this->parent(); // this can be NULL
    4. QObjectList objlistChildren = obj->children(); // which will crash here
    5. int iCount = objlistChildren.count();
    6.  
    7. for(int iIndex = 0; iIndex < iCount; iIndex++)
    8. {
    9. QObject *temp = objlistChildren[iIndex];
    10. ( (QWidget*)objlistChildren[iIndex] )->setFont(font); // what do you suppose happens if the child is not a QWidget?
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    You should get out of the habit of using C-style casts preferring C++-style casts of the correct type. Casts in general are last-resort mechanisms and worthy of avoidance. In this circumstance you could use qobject_cast and check the cast was successful.

Similar Threads

  1. How to resize parent widget when child widget is resized
    By ainne810329 in forum Qt Programming
    Replies: 4
    Last Post: 29th November 2011, 07:47
  2. Replies: 1
    Last Post: 11th March 2011, 19:34
  3. Replies: 7
    Last Post: 12th January 2011, 22:01
  4. Replies: 7
    Last Post: 14th January 2010, 08:47
  5. initialize child widgets within parent?
    By ucomesdag in forum Newbie
    Replies: 6
    Last Post: 6th June 2006, 08:11

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.