void MainWindow::slotAddPolygonItem() //在场景中加入一个多边形图元 { QVector<QPoint> v; v<<QPoint(30,-15)<<QPoint(0,-30)<<QPoint(-30,-15)<<QPoint(-30,15)<<QPoint(0,30)<<QPoint(30,15); QGraphicsPolygonItem *item = new QGraphicsPolygonItem(QPolygonF(v)); item->setBrush(QColor(qrand()%256,qrand()%256,qrand()%256)); item->setFlag(QGraphicsItem::ItemIsMovable); scene->addItem(item); item->setPos((qrand()%int(scene->sceneRect().width()))-200,(qrand()%int(scene->sceneRect().height()))-200); }
void Main::addPolygon() { const int size = int(canvas.width()/2); Q3PointArray pa(6); pa[0] = QPoint(0,0); pa[1] = QPoint(size,size/5); pa[2] = QPoint(size*4/5,size); pa[3] = QPoint(size/6,size*5/4); pa[4] = QPoint(size*3/4,size*3/4); pa[5] = QPoint(size*3/4,size/4); QGraphicsPolygonItem* i = canvas.addPolygon(pa); i->setFlag(QGraphicsItem::ItemIsMovable); i->setPen(Qt::NoPen); i->setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) ); i->setPos(qrand()%int(canvas.width()),qrand()%int(canvas.height())); i->setZValue(qrand()%256); }
void FormView::showSetting(Setting setting) { QGraphicsScene *s = scene(); s->clear(); resetTransform(); int scale_fac = 10; int spacing = 20; int planeWidth = setting.get_problem()->get_plane_width()*scale_fac; int planeHeight = setting.get_problem()->get_plane_height()*scale_fac; QRectF bound; for (int i=0; i<setting.get_number_of_planes(); ++i) { int x_offset = i*(planeWidth+spacing)+(spacing/2); int y_offset = (spacing/2); QRectF plane(x_offset,y_offset,planeWidth, planeHeight); s->addRect(plane,QPen(), QBrush(QColor(188, 198, 204),Qt::SolidPattern)); for (int j=0; j<setting.get_plane_at(i)->get_number_of_forms(); ++j) { QPolygonF polygon; Form form; setting.get_plane_at(i)->get_form_at(j, form); vector<Point> points_of_current_form = *(form.get_points()); for (int k=0; k<points_of_current_form.size(); ++k){ Point point = points_of_current_form[k]; polygon.push_back(QPointF(point.get_x()*scale_fac, point.get_y()*scale_fac)); } QGraphicsPolygonItem * polyitem = s->addPolygon(polygon, QPen(QColor(Qt::red)), QBrush(Qt::SolidPattern)); polyitem->setPos(x_offset, y_offset); bound = polygon.boundingRect(); } } float realwidth = container->width() - 50; float width = setting.get_number_of_planes()*(planeWidth+spacing); float realheight = container->height() - 50; float height = planeHeight+spacing; s->setSceneRect(0,0,width,height); float relw = 1; if(width > 0){ relw = realwidth / width; } float relh = 1; if(height > 0){ relh = realheight / height; } float rel = relw; if(relh < relw){ rel = relh; } scale(rel,rel); }