I have problems with this code:
(MainWindow.cpp)
{
MyWidget * w = new MyWidget;
w->setRectBackgroundColor(255,255,255,255);
w->setHeight(110);
w->show();
layout->addWidget(w,0);
widget->setLayout(layout);
setCentralWidget(widget);
}
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);
}
To copy to clipboard, switch view to plain text mode
(MyWidget.h)
{
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:
private:
int rectHeight;
};
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;
};
To copy to clipboard, switch view to plain text mode
(MyWidget.cpp)
{
}
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;
}
{
painter.begin(this);
painter.setPen(Qt::NoPen);
painter.
setBrush(QBrush(rectBgColor,Qt
::SolidPattern));
painter.
drawRect(QRect(0,
0,rect
().
width()+1,rectHeight
));
painter.end();
}
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::SolidPattern));
painter.drawRect(QRect(0,0,rect().width()+1,rectHeight));
painter.end();
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks