Results 1 to 19 of 19

Thread: How to insert a drawing in QWidget in Qt Designer form ?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to insert a drawing in QWidget in Qt Designer form ?

    I created a dialog in Qt Designer di Qt 6.5 per Windows 10 in which I put a QWidget that will contain a drawing made with QPainter in my application code. What should I write in the paintEvent(QPaintEvent* event) where the drawing is created and that I want to insert in the QWidget of the Qt Designer form ?
    To better understand what I'm saying, I attach a screenshot of what I have now: a red led drawn with QPainter in the :aintEvent(QPaintEvent* event) code of my application and a check box inserted by Qt Designer.
    Here instead the paintEvent method in which I draw the led:

    Qt Code:
    1. void Led::paintEvent(QPaintEvent* event)
    2. {
    3. Q_UNUSED(event);
    4.  
    5. QPainter p(this);
    6.  
    7. QRect geo = ui.widgetLed->geometry();
    8. int width = geo.width();
    9. int height = geo.height();
    10.  
    11. int x = 0, y = 0;
    12. if (alignment_ & Qt::AlignLeft)
    13. x = 0;
    14. else if (alignment_ & Qt::AlignRight)
    15. x = width - diamX_;
    16. else if (alignment_ & Qt::AlignHCenter)
    17. x = (width - diamX_) / 2;
    18. else if (alignment_ & Qt::AlignJustify)
    19. x = 0;
    20.  
    21. if (alignment_ & Qt::AlignTop)
    22. y = 0;
    23. else if (alignment_ & Qt::AlignBottom)
    24. y = height - diamY_;
    25. else if (alignment_ & Qt::AlignVCenter)
    26. y = (height - diamY_) / 2;
    27.  
    28. QRadialGradient g(x + diamX_ / 2, y + diamY_ / 2, diamX_ * 0.4,
    29. diamX_ * 0.4, diamY_ * 0.4);
    30.  
    31. g.setColorAt(0, Qt::white);
    32. if (state_)
    33. g.setColorAt(1, color_);
    34. else
    35. g.setColorAt(1, Qt::black);
    36. QBrush brush(g);
    37.  
    38. p.setPen(color_);
    39. p.setRenderHint(QPainter::Antialiasing, true);
    40. p.setBrush(brush);
    41. p.drawEllipse(x, y, diamX_ - 1, diamY_ - 1);
    42.  
    43. if (flashRate_ > 0 && flashing_)
    44. timer_->start(flashRate_);
    45. else
    46. timer_->stop();
    47. }
    To copy to clipboard, switch view to plain text mode 

    All this to be able to see the check box and the led side by side and not as in the figure I have attached.
    Attached Images Attached Images
    Last edited by giorgik; 4th May 2023 at 11:41.

Similar Threads

  1. Replies: 8
    Last Post: 19th September 2014, 11:16
  2. Promoting the parent QWidget of a QWidget form
    By extrakun in forum Qt Tools
    Replies: 6
    Last Post: 16th April 2010, 14:19
  3. Insert picture in a form using label
    By cwnelatury in forum Newbie
    Replies: 14
    Last Post: 18th March 2010, 23:43
  4. [Qt4.1] How to insert an image inside a Form?
    By Gonzalez in forum Qt Tools
    Replies: 5
    Last Post: 23rd September 2008, 11:20
  5. How to insert an image in background of a Form?
    By awanish_jmi in forum Qt Tools
    Replies: 2
    Last Post: 8th August 2008, 10:52

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