QGraphicsRectItem 类
QGraphicsRectItem类提供了一个矩形项目,您可以将其添加到QGraphicsScene中。
Header: | #include < QGraphicsRectItem > |
---|---|
qmake: | QT += widgets |
Inherits: | QAbstractGraphicsShapeItem |
Inherited By: | |
Since: | Qt 4.2 |
详述
要设置项目的矩形,可以将QRectF传递给QGraphicsRectItem的构造函数,或者调用setRect()函数。函数 rect() 的作用是:返回当前的矩形。
QGraphicsRectItem使用矩形和笔宽来提供boundingRect() ,shape() 和contains() 的合理实现。 paint() 函数使用项目的关联笔刷绘制矩形,您可以通过调用setPen() 和setBrush() 函数进行设置。
注意:无效矩形(例如,宽度或高度为负数的矩形)的渲染未定义。 如果不能确定使用的是有效矩形(例如,如果使用不可靠来源的数据创建矩形),则应使用QRectF :: normalized() 创建标准化的矩形,而应使用这些矩形。
参见QGraphicsPathItem,QGraphicsEllipseItem,QGraphicsPolygonItem,QGraphicsTextItem,QGraphicsLineItem,QGraphicsPixmapItem和图形视图框架。
公共类型
enum anonymous
由virtual type()函数返回的值。
Constant | Value | Description |
---|---|---|
QGraphicsRectItem::Type | 5 | 图形矩形项 |
公共函数
构造和析构
- QGraphicsRectItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent = nullptr)
- QGraphicsRectItem(const QRectF &rect, QGraphicsItem *parent = nullptr)
- QGraphicsRectItem(QGraphicsItem *parent = nullptr)
- virtual ~QGraphicsRectItem()
矩形
- QRectF rect() const
- void setRect(const QRectF &rectangle)
- void setRect(qreal x, qreal y, qreal width, qreal height)
重写的公共函数
- virtual QRectF boundingRect() const override
- virtual bool contains(const QPointF &point) const override
- virtual bool isObscuredBy(const QGraphicsItem *item) const override
- virtual QPainterPath opaqueArea() const override
- virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override
- virtual QPainterPath shape() const override
- virtual int type() const override
演示代码
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setApplicationName ("图形框架");
QGraphicsView view;
int x = -200,y = -200;
int w = 400,h=400;
QGraphicsScene scene(x,y,w,h);
scene.setBackgroundBrush (QBrush(qRgb (173,241,174)));
QPen pen(qRgb (140,159,141));
pen.setWidth (0);
for (int i = x + 40; i < x+w; i+=40) {
scene.addLine (x,i,x+w,i,pen);
scene.addLine (i,y,i,y+h,pen);
}
QGraphicsItemGroup xAxis;
scene.addItem (&xAxis);
xAxis.addToGroup (new QGraphicsLineItem(x,0,x+w,0));
xAxis.addToGroup (new QGraphicsLineItem(x+w-10,-10,x+w,-1));
xAxis.addToGroup (new QGraphicsLineItem(x+w-10, 10,x+w, 1));
QGraphicsItemGroup yAxis;
scene.addItem (&yAxis);
yAxis.addToGroup (new QGraphicsLineItem(0,y,0,y+h));
yAxis.addToGroup (new QGraphicsLineItem(-1, y+h,-10,y+h-10));
yAxis.addToGroup (new QGraphicsLineItem(1 , y+h, 10,y+h-10));
auto *gi = new QGraphicsRectItem;
gi->setFlag (QGraphicsItem::ItemIsMovable);
QRectF rect(0,0,210,140);
gi->setRect (rect);
QLinearGradient gLg(rect.topLeft (),rect.bottomRight ());
gLg.setColorAt (0,qRgb (221,180,68));
gLg.setColorAt (1,qRgb (221,240,68));
gi->setPen (pen);
gi->setBrush (gLg);
gi->setOpacity (0.8);
gi->moveBy (-80,-60);
scene.addItem (gi);
view.setScene (&scene);
view.setRenderHint (QPainter::Antialiasing);
view.setWindowFlag (Qt::WindowMinMaxButtonsHint,false);
view.show ();
return a.exec();
}