Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Adding a GraphicsScene into a QLabel



  • I have a problem displaying two labels, the label "label" is displayed with no problem ( it has some buttons adjusted Horizontally ) and the second label "label1" is not being displayed ( it contains a scene which has a view , in the scene i have a title and a picture ). This is the code i am trying to use.

    widget1 = new QWidget;
    setCentralWidget(widget1);
    
    label1 = new QLabel(widget1);
    scene = new QGraphicsScene(label1);
    vue = new QGraphicsView(scene);
    label1->move(100,100);
    
    label = new QLabel(widget1);
    layout = new QHBoxLayout(label);
    label->resize(500,100);
    

    It's my first time using QGraphicsScene/View and it is confusing me a little bit.


  • Lifetime Qt Champion

    @chrisLucr Do you call

    label1->show();
    

    somewhere?
    Also, why do you use QLabel to embed QGraphicsScene? Why not a simple QWidget?



  • On my main.cpp i am using w.show(); with w is my MainWindow where i have put the label the scene and the central widget . I am kind of new to qt so i don t know a lot of things , is using a QWidget will make the work easier ?


  • Lifetime Qt Champion

    @chrisLucr QLabel is usually used to show text and images, for QGraphicsScene I would use QWidget.


  • Moderators

    hi @chrisLucr and welcome

    I think you're confusing some things.

    The GraphixsView object will show/display your QGraphicsScene

    What you did was creating a Scene and a view as children of your QLabel. But that will not lead to automatic resizing of the QGraphicsView nor to an automatic show() call.

    this, for example, will create and show a QScene inside a view

    QGraphicsScene scene;
    scene.addText("Hello, world!");
    
    QGraphicsView view(&scene);
    view.show();
    

    I assume, you can simply add your QGraphicsView to a layout, no need to go the way over a Label.



  • i have tried this ,
    but it does not work too,
    i still can t see the scene in my window, all i am seeing are the buttons i have made
    ( the picture show exactely what i am trying to do : 0_1551258173240_Untitled.png )
    ```

    widget2 = new QWidget(widget1);
        scene = new QGraphicsScene(widget2);
        vue = new QGraphicsView(scene);
    

  • Lifetime Qt Champion

    @chrisLucr you still don't call show()...



  • well i have tried it and i deleted the line (view.show();) since i don t need two windows to appear but still does not work , i am trying to display the scene/view on the same window as the other labels.
    @jsulm i don t call it cause when i do two windows appear when i need only one. this is the code in my main.cpp where i am showing the window ( i think it does the job ?? )
    ```
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
    

  • Lifetime Qt Champion

    @chrisLucr What about giving your view a parent widget, so it is not shown in its own window?

    widget2 = new QWidget(widget1);
    scene = new QGraphicsScene();
    vue = new QGraphicsView(scene, widget2);
    widget2->show();
    vue->show();
    


  • Thanks a lot @jsulm , it is working fine now
    really appreciate it



  • @chrisLucr if your issue is solved, please don't forget to mark your post as such. Thanks



  • Thanks i did not know if it exists . it will be done


Log in to reply