PDA

View Full Version : customized widget overwrites mainwindow paintevent()



saman_artorious
30th March 2013, 10:49
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:




MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QWidget * widget = new QWidget;
QLayout * layout = new QVBoxLayout;
Widget *w = new Widget;

layout->addWidget(w);
widget->setLayout(layout);
setCentralWidget(widget);

QImage myImage;
myImage.load("path-to-image1");
QImage image = myImage.scaled(300, 250, Qt::IgnoreAspectRatio );

QImage myImage_2;
myImage_2.load("path-to-image2");
QImage image_2 = myImage_2.scaled(180, 50, Qt::IgnoreAspectRatio );

w->setPixmap1(QPixmap::fromImage(image));
w->setPixmap2(QPixmap::fromImage(image_2));

QPropertyAnimation anim1(w, "rot1");
QPropertyAnimation anim2(w, "rot2");

w.show();

}


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:


#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
//#include <QtGui>
#include <QDebug>
#include <QPainter>
#include <QPaintDevice>

class Widget : public QWidget {

Q_OBJECT
Q_PROPERTY(int rot1 READ rot1 WRITE setRot1);
Q_PROPERTY(int rot2 READ rot2 WRITE setRot2);

public:
Widget(QWidget *parent = 0) : QWidget(parent) { m_rot1 = 0; m_rot2 = 0; }
void setPixmap1(QPixmap px) { m_px1 = px; update(); }
void setPixmap2(QPixmap px) { m_px2 = px; update(); }

int rot1() const { return m_rot1; }
int rot2() const { return m_rot2; }

void set_teta(int teta) { degrees = teta; }
int get_teta() { return degrees; }

public slots:
void setRot1(int r1) { m_rot1 = r1; update(); }
void setRot2(int r2) { m_rot2 = r2; update(); }

protected:

void paintEvent(QPaintEvent *e) {

QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
p.translate(width()/2, height()/2);
if(!m_px1.isNull()) {
p.save();
p.rotate(m_rot1);
QRect r = m_px1.rect();
r.moveCenter(QPoint(-350,-270));
p.drawPixmap(r, m_px1);
p.restore();
}
if(!m_px2.isNull()) {

if(get_teta() > -10)
{
ratio = 1;
}
else
{
ratio = get_teta() / 10;
if(ratio > 0)
ratio *= -1;

qDebug() << ratio;
}
p.save();
p.rotate(m_rot2);
QRect r = m_px2.rect();

int x_ = m_px1.width()/2;
x_ *= -1;

// r.moveCenter(QPoint(x_+138,35 + (32 * ratio)));
r.moveCenter(QPoint(-400, -230));
p.drawPixmap(r, m_px2);
p.restore();
}
}
private:
int m_rot1;
int m_rot2;
QPixmap m_px1, m_px2;

int degrees;
int ratio;


};

#endif

Santosh Reddy
30th March 2013, 12:23
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.