Pergunta

Trying to add text to QGraphicsView:

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    QGraphicsScene scene;
    scene.addText("Hello, world!");
    ui->graphicsView->setScene(&scene);
}

But when the project running, there is nothing on the QGraphicsView.

Foi útil?

Solução

Your QGraphicsScene scene is a local variable and it is deleted immeadiately after the Widget's constructor has been executed.

Change the scene to a private member variable of the Widget class.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top