QGraphicsLineItem C++ code
时间: 2024-05-01 14:16:01 浏览: 94
Visual C++中使用Graphics
5星 · 资源好评率100%
Here is an example of how to create a `QGraphicsLineItem` in C++:
```
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsLineItem>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
// Create a line item with starting point (0, 0) and ending point (100, 100)
QGraphicsLineItem *line = new QGraphicsLineItem(0, 0, 100, 100);
line->setPen(QPen(Qt::red, 2)); // Set color and width of line
scene.addItem(line);
view.show();
return app.exec();
}
```
This code creates a `QGraphicsScene` and a `QGraphicsView` to display the scene. It then creates a `QGraphicsLineItem` with starting point (0, 0) and ending point (100, 100), sets the color and width of the line using a `QPen`, adds the line to the scene using `addItem()`, and shows the view.
阅读全文