PDA

View Full Version : I can not get a red window



rezas1000
31st August 2014, 17:37
Hello
I can not get a red window:( pleaze help me.

#ifndef E_H
#define E_H

#include <QtWidgets>

class paint : public QWidget
{
Q_OBJECT

public:
paint(QWidget *parent = 0);

protected:
void paintEvent(QPaintEvent *event);
void drawLines(QPainter *qp);

};
#endif // E_H


#include "paint.h"
#include <QPainter>


paint::paint(QWidget *parent)
: QWidget(parent)
{

}

void paint::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e);
QPainter qp(this);
drawLines(&qp);
}

void paint::drawLines(QPainter *qp)
{QColor color(Qt::red);

qp->setPen(color);
}


#include "paint.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

paint window;

window.resize(280, 270);
window.move(300, 300);
window.setWindowTitle("Lines");
window.show();

return app.exec();
}

anda_skoa
31st August 2014, 17:45
What do you mean?
This should bring up an empty window, why do you expect a red one?

Cheers,
_

rezas1000
31st August 2014, 18:04
I like the red window.
But the following code does not.


void paint::drawLines(QPainter *qp)
{QColor color(Qt::red);

qp->setPen(color);
}

Radek
31st August 2014, 18:28
Okay, you have set the pen but where are the drawn lines? IMO, the paint::drawLines() needs some updating. If you want a red window, get the rectangle from the event and qp->fillRect() the rectangle by red color.

rezas1000
31st August 2014, 19:21
Thank you very much
But,I'm sorry.I do not understand.

Radek
31st August 2014, 20:16
Setting the pen tells the painter which pen it should use in subsequent painting but it paints nothing. Your drawLines() takes the pen in a hand and - returns without painting anything. If you want a red rectangle (or lines or something else), you need taking the pen in a hand and painting. Something like this:


void paint::paintEvent(QPaintEvent *e)
{
QPainter qp(this);
QRect rect(e->rect());

qp.fillRect(rect,Qt::red);
}

and remove the drawLines(). Or, if you plan to use such procedure, pass it the rectangle along with the painter. Also, for better readibility of your code, make the paint::paintEvent() virtual. Yes, I know, it is already virtual because the QWidget::paintEvent() is virtual but specifying what is what allows you to orient in your code better.

stampede
31st August 2014, 21:02
Also, for better readibility of your code, make the paint:aintEvent() virtual. Yes, I know, it is already virtual because the QWidget:aintEvent() is virtual but specifying what is what allows you to orient in your code better.
Or use "override" specifier (http://en.cppreference.com/w/cpp/language/override) if you are using C++11.

Nomad_Tech
12th September 2014, 07:18
Do you mean you want a window with a red background?

I usually find it easiest to give my widgets/windows a custom palette if I just want to change the color only. Something like:


QPalette redWindowPalette;
redWindowPalette.setColor(QPalette::Background, QColor(225,75,75,255)); //set background role to a pale red
setPalette(redWindowPalette);//set our widgets palette to the new red background palette