#include <QtGui>
{
public:
path.moveTo(line().p1());
path.lineTo(line().p2());
stroker.setWidth(50);
return stroker.createStroke(path);
}
return shape().boundingRect();
}
}
};
int main(int argc, char *argv[])
{
LineItem horizontalLine(0,0,200,0);
horizontalLine.setToolTip("horizontal line tool tip");
scene.addItem(&horizontalLine);
LineItem diagonalLine(0,100,200,300);
diagonalLine.setToolTip("diagonal line tool tip");
scene.addItem(&diagonalLine);
view.setScene(&scene);
view.setMinimumSize(300,400);
view.show();
return a.exec();
}
#include <QtGui>
class LineItem : public QGraphicsLineItem
{
public:
LineItem(qreal x, qreal y, qreal w, qreal h): QGraphicsLineItem(x,y,w,h){}
QPainterPath shape() const{
QPainterPath path;
QPainterPathStroker stroker;
path.moveTo(line().p1());
path.lineTo(line().p2());
stroker.setWidth(50);
return stroker.createStroke(path);
}
QRectF boundingRect() const{
return shape().boundingRect();
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
painter->fillPath(shape(),QBrush(QColor(0,0,0,10)));
QGraphicsLineItem::paint(painter, option, widget);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsView view;
QGraphicsScene scene;
LineItem horizontalLine(0,0,200,0);
horizontalLine.setToolTip("horizontal line tool tip");
scene.addItem(&horizontalLine);
LineItem diagonalLine(0,100,200,300);
diagonalLine.setToolTip("diagonal line tool tip");
diagonalLine.setFlag(QGraphicsItem::ItemIsMovable);
scene.addItem(&diagonalLine);
view.setScene(&scene);
view.setMinimumSize(300,400);
view.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Note: I just threw the paint function in there to show the area where the tool tip will be shown. It is not necessary.
Bookmarks