Seite 1 von 1

Probleme mit 2D Zeichnung

Verfasst: 5. Dezember 2007 14:50
von NoRulez
Hey @all,

da es für mich gut passt, habe ich mir das beispiel "diagramscene" genommen.
Gut, wenn man beispielsweise 2 rechtecke zeichnet und diese dann mit einer linie verbindet und dann noch eines der rechtecke verschiebt, so wird die linie automatisch an die neue position gezeichnet. Es scheint also so als ob die linie an den beiden rechtecken klebt.

Mein Problem ist nun folgendes. Ich lade die koordinaten für 2 rechtecke aus einer Datei, genauso wie die koordinaten für die linie. Aber entweder die linie wird fix auf die koordinaten gezeichnet oder es erscheint überhaupt keine linie.

Kann mir diesbezüglich jemand helfen?

hier beispiel für die koordinaten:

Code: Alles auswählen

Type, x, y, width, height, red, green, blue, alpha
0, 2294, 2407, 200, 200, 000, 000, 255, 255
0, 2499, 2493, 200, 200, 255, 000, 000, 255
1, 2304, 2471, 2473, 2537, 255, 000, 255, 255
Laden und zeichnen tue ich das ganze wie folgt

Code: Alles auswählen

DiagramScene::DiagramScene(QMenu *itemMenu, QObject *parent) : QGraphicsScene(parent) {
	  myItemMenu = itemMenu;
	  myMode = MoveItem;
	  myItemType = DiagramItem::Step;
	  line = 0;
	  textItem = 0;
	  myItemColor = Qt::white;
	  myTextColor = Qt::black;
 	  myLineColor = Qt::black;

	  FILE *pf = NULL;
      pf = fopen("coord.txt", "r");
      DiagramItem *item;
      DiagramItem *item1;
      DiagramItem *item2;
      int count = 0;

	  int count2 = 0;
      while(!feof(pf)) {
		  count2++;
		  if(feof(pf) || count2 > 3)
			  break;
            myMode = MoveItem;
            count ++;
            int x, y, width, height, type, cr, cg, cb, ca;
            int setx, sety;
            fscanf(pf, "%d, %d, %d, %d, %d, %d, %d, %d, %d", &type, &x, &y, &width, &height, &cr, &cg, &cb, &ca);
            switch(type) {
                  case 0: {
						Mode bkp_mode = myMode;
						myMode = InsertItem;
                        item = new DiagramItem((DiagramItem::DiagramType)type, x, y, width, height);
                       
                        if(count%2 == 0)
                             item1 = item;
                        else if(count%2 != 0)
                             item2 = item;
                             item->setBrush(QColor(cr, cg, cb, ca));
                             addItem(item);
 
                             emit itemInserted(item);
                             myMode = bkp_mode;
                        }
                        break;
                  case 1: {
						Mode bkp_mode = myMode;
						myMode = InsertLine;

						line = new QGraphicsLineItem(QLineF(QPointF(x, y), QPointF(width, height)));
						line->setPen(QPen(QColor(cr, cg, cb, ca), 2));
						addItem(line);

						if (line != 0 && myMode == InsertLine) {
							QList<QGraphicsItem *> startItems = items(line->line().p1());

							if (startItems.count() && startItems.first() == line) startItems.removeFirst();

							QList<QGraphicsItem *> endItems = items(line->line().p2());
             
                            if (endItems.count() && endItems.first() == line) endItems.removeFirst();

							removeItem(line);
                            delete line;

							if (startItems.count() > 0 && endItems.count() > 0 &&
								startItems.first()->type() == DiagramItem::Type &&
								endItems.first()->type() == DiagramItem::Type &&
								startItems.first() != endItems.first()) {
								DiagramItem *startItem = qgraphicsitem_cast<DiagramItem *>(startItems.first());
								DiagramItem *endItem = qgraphicsitem_cast<DiagramItem *>(endItems.first());
								Arrow *arrow = new Arrow(startItem, endItem);
								arrow->setColor(QColor(cr, cg, cb, ca));
								startItem->addArrow(arrow);
								endItem->addArrow(arrow);
								arrow->setZValue(-1000.0);
								addItem(arrow);
								arrow->updatePosition();
							}
						}
						myMode = bkp_mode;
                    }
                    break;
            }
      }
      fclose(pf);
}
DiagramItem:

Code: Alles auswählen

DiagramItem::DiagramItem(DiagramType diagramType, qreal x, qreal y, qreal width, qreal height, 
             QGraphicsItem *parent, QGraphicsScene *scene)
    : QGraphicsPolygonItem(parent, scene)
{
    myDiagramType = diagramType;

    QPainterPath path;
    switch (myDiagramType) {
        case StartEnd:
            path.moveTo(200, 50);
            path.arcTo(150, 0, 50, 50, 0, 90);
            path.arcTo(50, 0, 50, 50, 90, 90);
            path.arcTo(50, 50, 50, 50, 180, 90);
            path.arcTo(150, 50, 50, 50, 270, 90);
            path.lineTo(200, 25);
            myPolygon = path.toFillPolygon();
            break;
        case Conditional:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
            break;
		case Step: {
			qreal x1 = x - (width/2);
			qreal x2 = x + (width/2);
			qreal y1 = y - (height/2);
			qreal y2 = y + (height/2);

			myPolygon << QPointF(x1, y1) << QPointF(x2, y1)
                      << QPointF(x2, y2) << QPointF(x1, y2)
                      << QPointF(x1, y1);
			}
            break;
        default:
            myPolygon << QPointF(-120, -80) << QPointF(-70, 80)
                      << QPointF(120, 80) << QPointF(70, -80)
                      << QPointF(-120, -80);
            break;
    }
    setPolygon(myPolygon);
    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);
}
Kann mir diesbezüglich bitte jemand helfen bzw. mir sagen wie ich eine simple linie zwischen den rechtecken zeichnen kann, die kleben bleibt?

Vielen Dank im Voraus und Lg
NoRulez