[Solved] Problem With QGraphicsView in Qt Creator IDE ?



  • Hi
    when i use QGraphicsView in my form i can't use any widget in my form if i use other widget with QGraphicsView my application can't be load and i don't see anything (because my form running but not shown)?! why ?
    and other problem occurred when i use following code .
    when i use this code every thing is good :
    @
    ui->setupUi(this);
    scene=new QGraphicsScene(this);

    QGraphicsRectItem *rect = scene->addRect(QRectF(0, 0, 100, 100));
    
    QGraphicsItem *item = scene->itemAt(50, 50);
    ui->graphicsView->setScene(scene);@
    

    but when i use :

    @
    ui->setupUi(this);
    scene=new QGraphicsScene(this);

    lable=new QLabel();
    
    QGraphicsProxyWidget *pw = scene->addWidget(lable);
    ui->graphicsView->setScene(scene);
    

    @

    my app run but not shown ?!!

    thanks for your attention
    Ali Sarlak


  • Moderators

    How is that related to Qt Creator? Is this in the designer?



  • @#include "widget.h"
    #include "ui_widget.h"

    #include <QGraphicsScene>
    #include <QGraphicsItem>
    #include <QGraphicsProxyWidget>
    #include <QLabel>

    widget::widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::widget)
    {
    ui->setupUi(this);
    QGraphicsScene * scene = new QGraphicsScene(this);
    scene->setSceneRect(QRectF(QPointF(0,0),QPointF(1000,1000)));

    ui->graphicsView->setScene(scene);
    ui->graphicsView->setInteractive(true);
    ui->graphicsView->setAlignment((Qt::AlignLeft | Qt::AlignTop));
    
    QLabel * lable=new QLabel();
    lable->setText("nikhil kumar");
    QGraphicsProxyWidget *pw = scene->addWidget(lable);
    ui->graphicsView->setScene(scene);
    

    }

    widget::~widget()
    {
    delete ui;
    }
    @

    NOTE::
    there are few to note down

    1. you have added an item and i.e. QLAbel , you have not inserted any text so its size is very minimum

    2. added an item to scene means you are adding to 0,0 coordinates of scene, because you are not mentioning the item's position in the scene

    3. you have to specify setSceneRect().. this function enables you to see the size of scene on the view. and you have not set that to scene

    i have taken a QWidget as my base widget on which i droped a grphicsview in ui. and then i started ... just read this and apply as what i did..

    mail me if you have any problem regarding this

    [Edit: Please be sure to use @ tags around code for formatting. -- mlong]



  • The problem is your QLabel.

    @ lable=new QLabel();@

    You didn't put any text on it. In fact it behaves the same when adding it to a QWidget GUI. A default QLabel doesn't have any "colored" borders, it is added to the GUI but is not visible since it was "camouflaged" to the color of its parent. Try adding any text to your QLabel, like this:

    @ lable=new QLabel("Hello World!");@


  • Moderators

    bq. The problem is your QLabel.

    No it's not. The line immediately after that sets the text to "nikhil kumar".

    The following sample code worked for me:
    @
    MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
    {
    QHBoxLayout *hb = new QHBoxLayout();
    this->setLayout(hb);

    QGraphicsView *gv = new QGraphicsView();
    hb->addWidget(gv);
    
    QGraphicsScene * scene = new QGraphicsScene(this);
    scene->setSceneRect(QRectF(QPointF(0,0),QPointF(200,200)));
    
    gv->setScene(scene);
    gv->setInteractive(true);
    gv->setAlignment((Qt::AlignLeft | Qt::AlignTop));
    
    QLabel * lable=new QLabel("test");
    lable->setText("nikhil kumar");
    scene->addWidget(lable);
    gv->setScene(scene);
    

    };
    @

    The main difference between my code and the code that you posted is that mine didn't have a ui component to it. There may be an issue with the placement of your QGraphicsView in the .ui file. Without more information, it's hard to tell.

    Oh, also, I tested with a scene rect of 200x200, rather than 1000x1000. Make sure that you're item isn't being rendered offscreen or something, as well.



  • @mlong

    boss firstly the problem is with QLabel , that ali has not set any text that is why he is not able to see his label...

    and secondly, its not the issue of setSceneRect() as what i believe, if he doesnt set his setSceneRect.. he will be able to see his text in the middle ..


  • Moderators

    niks1989:

    He is setting the text in the label to nikhil kumar.

    From the original code posted above (Lines 21-22):
    @
    QLabel * lable=new QLabel();
    lable->setText("nikhil kumar");
    @
    You don't have to set the text in the label's ctor. How is this code not setting the text?

    I still feel like the issue is probably related to the QGraphicsScene in the .ui file. But without seeing that code, it's hard to tell.



  • @mlong

    i think you are missing my point....
    i am not saying that you cant set text in label's ctor.

    i just saying he (ali) has not set text to that label, by this label is of its minimum size, and that is why he couldnt able to see his small label in the middle of the white colour scene.

    and that what you are refering about original code, actualy its not the original its just a solution .. original is Ali's code(which is posted above).


  • Moderators

    @niks1989

    I do sincerely apologize. You're absolutely right. This is what I get for staying up too late at night, I guess. :-)

    Indeed, if there's nothing set in the label, you're not going to see anything on-screen...



  • hahahaahahaha....
    ohk.. enjoy..!!!



  • Hello to all.
    First thanks for your reply.
    This issue solved with MR.niks1989 & MR.Code_ReaQtor codes (for QLable).
    Thanks for MR.mlong code ,this code very helpful for me ;) thank you.

    Indeed my intention was to turn around QDial widget 180 clock wise , finally I did this by your help and QGraphicView.

    Regards :
    Ali Sarlak


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.