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]How to make QGraphicsItem (QGraphicsProxyWidget in this case) to be visible only inside the boundaries of its parent QWidget? Simple example.
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]How to make QGraphicsItem (QGraphicsProxyWidget in this case) to be visible only inside the boundaries of its parent QWidget? Simple example.

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.1k 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.
  • J Offline
    J Offline
    Jhones
    wrote on last edited by
    #1

    I have a QWidget (myWidget) which contains only a QLabel (myLabel) inside of it. There is no layout around myLabel (myLabel must to be free because I want move it anywhere inside myWidget). So, I put myWidget inside a QGraphicsView (trought a QGraphicsScene). Then I set the position of myLabel this way:
    <code>ui->myLabel->move(-10, 0);</code>
    The result is that only the final part of myLabel is visible (the initial 10 pixels are not shown on the screen because they are out the myWidget). That's exactly what I was expecting. But since I'm working with QGraphicsView, I should use QGraphicsProxyWidget (which is a QGraphicsItem) to manipulate the position and to make animation on my widgets. So, what I did:
    <code>QGraphicsProxyWidget *myLabelProxy = this->createProxyForChildWidget(ui->myLabel);
    myLabeProxy->setPos(-10, 0);</code>
    The result is that myLabel is entire visible now. Even the initial 10 pixels which should be hidden because they are outside myWidget. I want that only the parts that are inside the parents widgets boundaries to be visible. Remeber that I wanna use QGraphicsProxyWidget to manipulating the position and to make animations on my widgets to avoid undesirable side effects... Any help is welcome.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jhones
      wrote on last edited by
      #2

      To make the things easier to understand, I created a simple program (runs only in main) where you guys can undestand my issue:

      <code>
      #include <QtGui>

      int main(int argc, char *argv[])
      {

      QApplication a(argc, argv);

      QWidget *w = new QWidget();
      QHBoxLayout *layout = new QHBoxLayout();
      w->setLayout(layout);

      QGraphicsView *view = new QGraphicsView(w);
      layout->addWidget(view);
      QGraphicsScene *scene = new QGraphicsScene();
      view->setScene(scene);

      QWidget *myWidget = new QWidget();
      myWidget->setGeometry(0, 0, 200, 100);
      scene->addWidget(myWidget);

      QLabel *myLabel = new QLabel("Text of the Label!!!", myWidget);
      myLabel->show();

      //run with this line commented later
      myLabel->move(-10, 0);

      //uncomment these 2 lines later
      // QGraphicsProxyWidget *myLabelProxy = myWidget->graphicsProxyWidget()->createProxyForChildWidget(myLabel);
      // myLabelProxy->setPos(-10, 0);

      w->showMaximized();

      return a.exec();
      }
      </code>

      1 Reply Last reply
      0
      • U Offline
        U Offline
        utcenter
        wrote on last edited by
        #3

        You could subclass, reimplement the paint() event, inside of it check the position of the object and skip drawing if it is outside, call the base class paint method if it is on the inside to draw the original object. Might not work but the one sure way to know is to try.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jhones
          wrote on last edited by
          #4

          Thanks utcenter. But I just found the way. I must set a flag. Example: @myParentProxy->setFlag(QGraphicsItem::ItemClipsChildrentoShape);@

          1 Reply Last reply
          0
          • U Offline
            U Offline
            utcenter
            wrote on last edited by
            #5

            If the default clip behavior is the one you want then great.

            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