Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Problem With QGraphicsView in Qt Creator IDE ?
Forum Updated to NodeBB v4.3 + New Features

[Solved] Problem With QGraphicsView in Qt Creator IDE ?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 5 Posters 8.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tobias.hunger
    wrote on last edited by
    #2

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

    1 Reply Last reply
    0
    • N Offline
      N Offline
      niks1989
      wrote on last edited by
      #3

      @#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]

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Code_ReaQtor
        wrote on last edited by
        #4

        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!");@

        Please visit my open-source projects at https://github.com/Code-ReaQtor.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #5

          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.

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            niks1989
            wrote on last edited by
            #6

            @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 ..

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #7

              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.

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0
              • N Offline
                N Offline
                niks1989
                wrote on last edited by
                #8

                @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).

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mlong
                  wrote on last edited by
                  #9

                  @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...

                  Software Engineer
                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    niks1989
                    wrote on last edited by
                    #10

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

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      Ali Sarlak
                      wrote on last edited by
                      #11

                      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

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved