// File: ZProblem.cpp
//
// QGraphicsItems:
// (1) Top-Level QGraphicsRectItem .. Z = 1.0
// (2) ... Child QGraphicsTextItem .. Z = 3.0
// (3) Top-Level QGraphicsLineItem .. Z = 2.0
//
// Notice that the Line Item obscures the Text Item, even though the
// Text Item has a higher Z-Value (but does not have the same parent
// QGraphicsItem). [Qt 4.6.3, 12-2010].
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsLineItem>
#include <QGraphicsRectItem>
#include <QGraphicsTextItem>
#include <ostream>
int main(int argc, char** argv)
{
// **************************************************
// *** Create Top-Level Rectangle QGraphicsItem ***
// **************************************************
const int RectWidth (300.0);
const int RectHeight (220.0);
-0.5 * RectWidth, -0.5 * RectHeight, RectWidth, RectHeight);
rectItem
->setBrush
(QColor (0x66, 0xCC, 0xCC
));
// blue-green rectItem
->setPen
(QColor (Qt
::black));
rectItem->setZValue (1.0); // *** Z VALUE 1.0, Top-Level Item ***
rectItem->setToolTip ("Rectangle: Z = 1.0");
myScene.addItem (rectItem);
// *****************************************
// *** Create Child Text QGraphicsItem ***
// *****************************************
QFont textFont;
// application default textFont.setPixelSize (75);
textFont.
setWeight (QFont::Black);
childTextItem->setFont (textFont);
childTextItem->setZValue (3.0); // *** Z VALUE 3.0, Child Item ***
childTextItem->setToolTip ("Child Text: Z = 3.0");
const QRectF childRect
= childTextItem
->boundingRect
();
childTextItem->setPos (-0.5 * childRect.width(),
-0.5 * childRect.height());
// *********************************************
// *** Create Top-Level Line QGraphicsItem ***
// *********************************************
-0.6 * RectWidth, 0.0, 0.6 * RectWidth, 0.0);
linePen.setCapStyle (Qt::RoundCap);
linePen.setWidth (20);
lineItem->setPen (linePen);
lineItem->setZValue (2.0); // *** Z VALUE 2.0, Top-Level Item ***
lineItem->setToolTip ("Line: Z = 2.0");
myScene.addItem (lineItem);
// ******************************
// *** Create QGraphicsView ***
// ******************************
myView.setCaption ("Test QGraphicsItems Z-Values");
myView.resize (3.0 * RectWidth, 3.0 * RectHeight);
myView.show();
return app.exec();
}
// File: ZProblem.cpp
//
// QGraphicsItems:
// (1) Top-Level QGraphicsRectItem .. Z = 1.0
// (2) ... Child QGraphicsTextItem .. Z = 3.0
// (3) Top-Level QGraphicsLineItem .. Z = 2.0
//
// Notice that the Line Item obscures the Text Item, even though the
// Text Item has a higher Z-Value (but does not have the same parent
// QGraphicsItem). [Qt 4.6.3, 12-2010].
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsLineItem>
#include <QGraphicsRectItem>
#include <QGraphicsTextItem>
#include <ostream>
int main(int argc, char** argv)
{
QApplication app (argc, argv);
QGraphicsScene myScene;
// **************************************************
// *** Create Top-Level Rectangle QGraphicsItem ***
// **************************************************
const int RectWidth (300.0);
const int RectHeight (220.0);
QGraphicsRectItem* rectItem = new QGraphicsRectItem (
-0.5 * RectWidth, -0.5 * RectHeight, RectWidth, RectHeight);
rectItem->setBrush (QColor (0x66, 0xCC, 0xCC)); // blue-green
rectItem->setPen (QColor (Qt::black));
rectItem->setFlags (QGraphicsItem::ItemIsMovable);
rectItem->setZValue (1.0); // *** Z VALUE 1.0, Top-Level Item ***
rectItem->setToolTip ("Rectangle: Z = 1.0");
myScene.addItem (rectItem);
// *****************************************
// *** Create Child Text QGraphicsItem ***
// *****************************************
QGraphicsTextItem* childTextItem =
new QGraphicsTextItem ("ENIGMA", rectItem);
QFont textFont; // application default
textFont.setPixelSize (75);
textFont.setWeight (QFont::Black);
childTextItem->setFont (textFont);
childTextItem->setZValue (3.0); // *** Z VALUE 3.0, Child Item ***
childTextItem->setToolTip ("Child Text: Z = 3.0");
const QRectF childRect = childTextItem->boundingRect();
childTextItem->setPos (-0.5 * childRect.width(),
-0.5 * childRect.height());
// *********************************************
// *** Create Top-Level Line QGraphicsItem ***
// *********************************************
QGraphicsLineItem* lineItem = new QGraphicsLineItem (
-0.6 * RectWidth, 0.0, 0.6 * RectWidth, 0.0);
QPen linePen = QPen (QColor (0xFF, 0x66, 0x66)); // red
linePen.setCapStyle (Qt::RoundCap);
linePen.setWidth (20);
lineItem->setPen (linePen);
lineItem->setFlags (QGraphicsItem::ItemIsMovable);
lineItem->setZValue (2.0); // *** Z VALUE 2.0, Top-Level Item ***
lineItem->setToolTip ("Line: Z = 2.0");
myScene.addItem (lineItem);
// ******************************
// *** Create QGraphicsView ***
// ******************************
QGraphicsView myView (&myScene);
myView.setCaption ("Test QGraphicsItems Z-Values");
myView.resize (3.0 * RectWidth, 3.0 * RectHeight);
myView.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks