简体   繁体   English

在Qt中绘制内部小部件

[英]painting inside widget in Qt

I created a very simple GUI that has a push button and a "Graphics View" widget from Display Widgets. 我创建了一个非常简单的GUI,它有一个按钮和Display Widgets的“Graphics View”小部件。 On pushing the button I want a line to be drawn across the "Graphics View" widget. 在按下按钮时,我希望在“图形视图”小部件上绘制一条线。 I have changed the name of the "Graphics View" widget to gv by right-clicking the widget in design view and then selecting change objectName. 我已通过右键单击设计视图中的窗口小部件,然后选择更改objectName,将“图形视图”窗口小部件的名称更改为gv。 I am not able to understand how should the line be drawn. 我无法理解该如何绘制线条。 I read various texts on Qt that provided information about QPainter, PaintEvent etc. But I got more confused. 我在Qt上阅读了各种文本,提供了有关QPainter,PaintEvent等的信息。但我更加困惑。

Kindly help me with this. 请帮助我。 A small sample code shall be really helpful for me as I am new to Qt. 一个小的示例代码对我来说真的很有用,因为我是Qt的新手。

A QGraphicsView is meant for displaying instances of QGraphicsItem that are managed by a component called QGraphicsScene. QGraphicsView用于显示由名为QGraphicsScene的组件管理的QGraphicsItem实例。 In your case, you'd create a QGraphicsLineItem and add it to the scene, or directly create it as an item of the scene by calling the addLine member function of your QGraphicsScene instance. 在您的情况下,您将创建一个QGraphicsLineItem并将其添加到场景中,或者通过调用QGraphicsScene实例的addLine成员函数将其直接创建为场景项。

All drawing will be done by Qt itself, assuming that you did connect your graphics view and scene properly. 所有绘图都将由Qt本身完成,假设您确实正确连接了图形视图和场景。 Be sure to read The Graphics View Framework , which gives you an overview over how these components work. 请务必阅读图形视图框架 ,该框架概述了这些组件的工作原理。

You will find code examples of how to manage and display a scene using the graphics view framework here: http://doc.trolltech.com/4.6/examples-graphicsview.html 您将在此处找到有关如何使用图形视图框架管理和显示场景的代码示例: http//doc.trolltech.com/4.6/examples-graphicsview.html

You can paint into a QPainter 你可以画成QPainter

Either override the paintevent and draw there 要么覆盖痛苦的事,要么画在那里

void MyDisplayWidget::paintEvent(QPaintEvent*)
{
    QPainter p(this);   
    p.setPen(Qt::green);

    p.drawText(10,10,"hello");

}

Or draw into a QImage and display that 或者绘制成QImage并显示它

QImage image = QImage(size);
QPainter p(&image);
p.drawText(10,10,"hello");
// draw or save QImage 

You can even use the same draw function taking a QPainter * to draw either direct to the screen or to an image. 您甚至可以使用相同的绘图功能,将QPainter *直接绘制到屏幕或图像上。

first you must knew some information about QPainter to have benefit of it. 首先,你必须知道有关QPainter的一些信息才能从中受益。

QPainter provides highly optimized functions to do most of the drawing GUI programs require. QPainter提供高度优化的功能,以满足大多数绘图GUI程序的要求。 It can draw everything from simple graphical primitives (represented by the QPoint, QLine, QRect, QRegion and QPolygon classes) to complex shapes like vector paths .and we use it to draw on paint devices then render it to view,and we have alot of qpaint devices like : QWidget, QImage, QPixmap, QPicture, QPrinter, and QOpenGLPaintDevice you can use any one of them depending on your requirements then create QGraphic scene and add you paint device as qgraphic scene item to be shown in qgraphic view. 它可以绘制从简单的图形基元(由QPoint,QLine,QRect,QRegion和QPolygon类表示)到复杂形状(如矢量路径)的所有内容。我们使用它在绘制设备上绘制然后渲染它来查看,我们有很多qpaint设备如:QWidget,QImage,QPixmap,QPicture,QPrinter和QOpenGLPaintDevice你可以根据你的要求使用它们中的任何一个然后创建QGraphic场景并添加绘制设备作为qgraphic场景项目以在​​qgraphic视图中显示。

here is simple code: 这里是简单的代码:

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    scene=new QGraphicsScene(this); //allocate your scene to your main widget
    view=new QGraphicsView(scene,this);//here is your view
    pixmap=new QPixmap(QSize(700,700));// paint device
    view->resize(700,700);

}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *e)
{
    painter=new QPainter;// create your painter
    painter->begin(pixmap);//add painter to your paint device

    painter->fillRect(0,0,300,300,Qt::red);//draw rect
    painter->setPen(Qt::yellow);
    painter->drawLine(0,0,700,700);//draw line
    painter->end();
    scene->addPixmap(*pixmap);// add your paint device to your scene
    view->show();//then show your view

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM