Results 1 to 20 of 28

Thread: How to change widget shape in QtDesigner ?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: How to change widget shape in QtDesigner ?

    QSvgRenderer::load() returns a bool. In your paint event, you do not check this value. It is possible that the resource is not loading.

    How have you packaged your QLed widget for use in the application? As a DLL or a static library (LIB)? If it is a static library, you may need to insert a call to initialize the resources in main(): Q_INIT_RESOURCE.

    In paintEvent() this doesn't make any sense:
    Qt Code:
    1. renderer = nullptr;
    2. delete renderer;
    To copy to clipboard, switch view to plain text mode 

    If you set the pointer to null before you delete the instance, then delete does nothing and you have a memory leak. And since the renderer instance is local to the paint method, there is no need to set it to null at all. It gets re-recreated and re-initialized with every paintEvent() call. So just get rid of the "renderer = null" line completely.

    I think you can simply create the QSvgRenderer instance on the stack instead of using new() to create a pointer:

    Qt Code:
    1. void QLed::paintEvent(QPaintEvent *)
    2. {
    3. QSvgRenderer renderer;
    4. QString ledShapeAndColor;
    5. QPainter painter(this);
    6. painter.setRenderHint(QPainter::Antialiasing, true);
    7.  
    8. ledShapeAndColor = shapes[m_shape];
    9.  
    10. if(m_value)
    11. ledShapeAndColor.append(colors[m_onColor]);
    12. else
    13. ledShapeAndColor.append(colors[m_offColor]);
    14.  
    15. bool bLoaded = renderer.load(ledShapeAndColor);
    16. assert( bLoaded );
    17.  
    18. renderer.render(&painter);
    19. }
    To copy to clipboard, switch view to plain text mode 

    One other suggestion to improve efficiency: Your two QStringList member variables should be made static const, and probably just moved to the top of the qled.cpp file. Initialize them using list initialization during compilation. In your current code, you create and initialize the lists every time a QLed is created. If your UI has a lot of LEDs, this could slow down your program's startup.

    Qt Code:
    1. static const QStringList sShapes { "shape1", "shape2", "shape3" }; // Don't remember what your shapes and colors are :-)
    2. static const QStringList sColors { "color1", "color2", "color3" };
    3.  
    4. QLed::QLed( QWidget * parent )
    5. : QWidget( parent )
    6. {
    7. // ...
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 29th April 2023 at 17:11.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QCursor change shape in Qt 5
    By cic1988 in forum Qt Programming
    Replies: 5
    Last Post: 4th March 2015, 20:49
  2. Can I change the shape of the form in QML?
    By chong_kimkeang in forum Newbie
    Replies: 1
    Last Post: 7th November 2012, 17:51
  3. how to change shape of Qwidget??
    By anupamgee in forum Qt Programming
    Replies: 4
    Last Post: 29th June 2009, 09:54
  4. Change the shape of a frame.
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 13th November 2007, 06:40
  5. How to change shape fast
    By nileshsince1980 in forum Qt Programming
    Replies: 9
    Last Post: 18th October 2007, 05:49

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.