Results 1 to 2 of 2

Thread: customized widget overwrites mainwindow paintevent()

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default customized widget overwrites mainwindow paintevent()

    I have a customized widget, this customized widget is supposed to occupy a small portion of the mainwindow. Note that I have already designed the UI, i just need to replace this customized widget to two corners of the UI. for this, I did the following:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. QWidget * widget = new QWidget;
    8. QLayout * layout = new QVBoxLayout;
    9. Widget *w = new Widget;
    10.  
    11. layout->addWidget(w);
    12. widget->setLayout(layout);
    13. setCentralWidget(widget);
    14.  
    15. QImage myImage;
    16. myImage.load("path-to-image1");
    17. QImage image = myImage.scaled(300, 250, Qt::IgnoreAspectRatio );
    18.  
    19. QImage myImage_2;
    20. myImage_2.load("path-to-image2");
    21. QImage image_2 = myImage_2.scaled(180, 50, Qt::IgnoreAspectRatio );
    22.  
    23. w->setPixmap1(QPixmap::fromImage(image));
    24. w->setPixmap2(QPixmap::fromImage(image_2));
    25.  
    26. QPropertyAnimation anim1(w, "rot1");
    27. QPropertyAnimation anim2(w, "rot2");
    28.  
    29. w.show();
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 

    this code executes properly and the MainWindow does pops up.but the content and all the other widgets inside the mainwindow are erased. it only shows an image at the corner of mainwindow. This is because the paintEvent virtual function is overwritten in the customized widget.

    how can I resolve this? I note again that mainwindow has already been designed, I just need to add my customized widgets to two corners of mainwindow.

    customized widget is as follows:
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QWidget>
    5. //#include <QtGui>
    6. #include <QDebug>
    7. #include <QPainter>
    8. #include <QPaintDevice>
    9.  
    10. class Widget : public QWidget {
    11.  
    12. Q_OBJECT
    13. Q_PROPERTY(int rot1 READ rot1 WRITE setRot1);
    14. Q_PROPERTY(int rot2 READ rot2 WRITE setRot2);
    15.  
    16. public:
    17. Widget(QWidget *parent = 0) : QWidget(parent) { m_rot1 = 0; m_rot2 = 0; }
    18. void setPixmap1(QPixmap px) { m_px1 = px; update(); }
    19. void setPixmap2(QPixmap px) { m_px2 = px; update(); }
    20.  
    21. int rot1() const { return m_rot1; }
    22. int rot2() const { return m_rot2; }
    23.  
    24. void set_teta(int teta) { degrees = teta; }
    25. int get_teta() { return degrees; }
    26.  
    27. public slots:
    28. void setRot1(int r1) { m_rot1 = r1; update(); }
    29. void setRot2(int r2) { m_rot2 = r2; update(); }
    30.  
    31. protected:
    32.  
    33. void paintEvent(QPaintEvent *e) {
    34.  
    35. QPainter p(this);
    36. p.setRenderHint(QPainter::Antialiasing);
    37. p.translate(width()/2, height()/2);
    38. if(!m_px1.isNull()) {
    39. p.save();
    40. p.rotate(m_rot1);
    41. QRect r = m_px1.rect();
    42. r.moveCenter(QPoint(-350,-270));
    43. p.drawPixmap(r, m_px1);
    44. p.restore();
    45. }
    46. if(!m_px2.isNull()) {
    47.  
    48. if(get_teta() > -10)
    49. {
    50. ratio = 1;
    51. }
    52. else
    53. {
    54. ratio = get_teta() / 10;
    55. if(ratio > 0)
    56. ratio *= -1;
    57.  
    58. qDebug() << ratio;
    59. }
    60. p.save();
    61. p.rotate(m_rot2);
    62. QRect r = m_px2.rect();
    63.  
    64. int x_ = m_px1.width()/2;
    65. x_ *= -1;
    66.  
    67. // r.moveCenter(QPoint(x_+138,35 + (32 * ratio)));
    68. r.moveCenter(QPoint(-400, -230));
    69. p.drawPixmap(r, m_px2);
    70. p.restore();
    71. }
    72. }
    73. private:
    74. int m_rot1;
    75. int m_rot2;
    76. QPixmap m_px1, m_px2;
    77.  
    78. int degrees;
    79. int ratio;
    80.  
    81.  
    82. };
    83.  
    84. #endif
    To copy to clipboard, switch view to plain text mode 
    Last edited by saman_artorious; 30th March 2013 at 10:07.

  2. #2
    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: customized widget overwrites mainwindow paintevent()

    how can I resolve this? I note again that mainwindow has already been designed, I just need to add my customized widgets to two corners of mainwindow.
    In the mainwindow ui design, put place holder widgets in the corners and promote them the custom widgets.
    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.

Similar Threads

  1. launching application in customized widget
    By PstdEr in forum Newbie
    Replies: 1
    Last Post: 20th March 2013, 19:36
  2. Replies: 0
    Last Post: 6th November 2011, 09:22
  3. Replies: 1
    Last Post: 11th March 2011, 19:34
  4. How to paint a widget outside paintEvent()
    By wesley in forum Qt Programming
    Replies: 10
    Last Post: 27th February 2008, 03:19
  5. painting a widget outside a paintEvent
    By jayw710 in forum Qt Programming
    Replies: 1
    Last Post: 25th June 2007, 23:18

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.