Peppy
23rd December 2009, 21:23
I have problems with this code:
(MainWindow.cpp)
MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent)
{
QWidget * widget = new QWidget;
QVBoxLayout * layout = new QVBoxLayout;
MyWidget * w = new MyWidget;
w->setRectBackgroundColor(255,255,255,255);
w->setHeight(110);
w->show();
layout->addWidget(w,0);
widget->setLayout(layout);
setCentralWidget(widget);
}
(MyWidget.h)
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
~MyWidget(void){ };
void setHeight(int height);
void setRectBackgroundColor(int r,int g, int b, int a);
void setRectBackgroundColor(const QColor &bgColor);
protected:
virtual void paintEvent(QPaintEvent * e);
private:
QColor rectBgColor;
int rectHeight;
};
(MyWidget.cpp)
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
}
void MyWidget::setRectBackgroundColor(const QColor &bgColor)
{
rectBgColor = bgColor;
}
void MyWidget::setRectBackgroundColor(int r, int g, int b, int a)
{
rectBgColor = QColor(r,g,b,a);
}
void MyWidget::setHeight(int height)
{
rectHeight = height;
}
void MyWidget::paintEvent(QPaintEvent *e)
{
QPainter painter;
painter.begin(this);
painter.setPen(Qt::NoPen);
painter.setBrush(QBrush(rectBgColor,Qt::SolidPatte rn));
painter.drawRect(QRect(0,0,rect().width()+1,rectHe ight));
painter.end();
}
Problem is: In main window it doesn't show any white rectangle... I think is problem with body of MainWindow constructor, because on QWidget window it is working. I am so unhappy with this problem. :(
(MainWindow.cpp)
MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent)
{
QWidget * widget = new QWidget;
QVBoxLayout * layout = new QVBoxLayout;
MyWidget * w = new MyWidget;
w->setRectBackgroundColor(255,255,255,255);
w->setHeight(110);
w->show();
layout->addWidget(w,0);
widget->setLayout(layout);
setCentralWidget(widget);
}
(MyWidget.h)
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
~MyWidget(void){ };
void setHeight(int height);
void setRectBackgroundColor(int r,int g, int b, int a);
void setRectBackgroundColor(const QColor &bgColor);
protected:
virtual void paintEvent(QPaintEvent * e);
private:
QColor rectBgColor;
int rectHeight;
};
(MyWidget.cpp)
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
}
void MyWidget::setRectBackgroundColor(const QColor &bgColor)
{
rectBgColor = bgColor;
}
void MyWidget::setRectBackgroundColor(int r, int g, int b, int a)
{
rectBgColor = QColor(r,g,b,a);
}
void MyWidget::setHeight(int height)
{
rectHeight = height;
}
void MyWidget::paintEvent(QPaintEvent *e)
{
QPainter painter;
painter.begin(this);
painter.setPen(Qt::NoPen);
painter.setBrush(QBrush(rectBgColor,Qt::SolidPatte rn));
painter.drawRect(QRect(0,0,rect().width()+1,rectHe ight));
painter.end();
}
Problem is: In main window it doesn't show any white rectangle... I think is problem with body of MainWindow constructor, because on QWidget window it is working. I am so unhappy with this problem. :(