现在我们从iconEditor构造函数入手:
iconeditor::iconeditor(QWidget *parent) :
QWidget(parent)
{
setAttribute(Qt::WA_StaticContents);
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
curColor = Qt::black;
zoom = 8;
image = QImage(16, 16, QImage::Format_ARGB32);
image.fill(qRgba(0, 0, 0, 0));
}
setAttribute(Qt::WA_StaticConents);
Qt::WA_StaticContents属性。
这个属性告诉Qt,当重新改变窗口部件大小时,这个窗口部件的内容并没有发生变化,而且它的内容仍旧保留从窗口左上角开始的特性。当重新定义窗口部件的大小时,通过使用这个信息,Qt就可以避免对已经显示区域的重新绘制。
通常情况下,当重新定义一个窗口部件的大小时,Qt会为窗口部件的整个可见区域生成一个绘制事件。但是如果该窗口部件在创建时使用了Qt:: WA_StaticContents属性,那么绘制事件的区域就会被严格限定在之前没有被显示的像素部分上。这也就意味着,如果重新把窗口部件改变为比原来还要小的尺寸,那么就根本不会产生任何绘制事件。
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
原型:void | setSizePolicy ( QSizePolicy::Policy horizontal, QSizePolicy::Policy vertical ) |
enum QSizePolicy::Policy
This enum describes the various per-dimension sizing types used when constructing a QSizePolicy.
Constant | Value | Description |
---|---|---|
QSizePolicy::Fixed | 0 | The QWidget::sizeHint() is the only acceptable alternative, so the widget can never grow or shrink (e.g. the vertical direction of a push button). |
QSizePolicy::Minimum | GrowFlag | The sizeHint() is minimal, and sufficient. The widget can be expanded, but there is no advantage to it being larger (e.g. the horizontal direction of a push button). It cannot be smaller than the size provided by sizeHint(). |
QSizePolicy::Maximum | ShrinkFlag | The sizeHint() is a maximum. The widget can be shrunk any amount without detriment if other widgets need the space (e.g. a separator line). It cannot be larger than the size provided by sizeHint(). |
QSizePolicy::Preferred | GrowFlag | ShrinkFlag | The sizeHint() is best, but the widget can be shrunk and still be useful. The widget can be expanded, but there is no advantage to it being larger than sizeHint() (the default QWidget policy). |
QSizePolicy::Expanding | GrowFlag | ShrinkFlag | ExpandFlag | The sizeHint() is a sensible size, but the widget can be shrunk and still be useful. The widget can make use of extra space, so it should get as much space as possible (e.g. the horizontal direction of a horizontal slider). |
QSizePolicy::MinimumExpanding | GrowFlag | ExpandFlag | The sizeHint() is minimal, and sufficient. The widget can make use of extra space, so it should get as much space as possible (e.g. the horizontal direction of a horizontal slider). |
QSizePolicy::Ignored | ShrinkFlag | GrowFlag | IgnoreFlag | The sizeHint() is ignored. The widget will get as much space as possible. |
Qt颜色的设置,下面列出了QT里面的颜色
image = QImage(32, 16, QImage::Format_ARGB32);
好吧、先让我们看看原型:
QImage::QImage ( int width, int height, Format format )
关于Format的定义:
QImage::Format_ARGB32 | 5 | The image is stored using a 32-bit ARGB format (0xAARRGGBB). |
其他实现部分见下一章。