Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Movable QWidget

    General and Desktop
    2
    3
    1445
    Loading More Posts
    • 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.
    • D
      DMIII last edited by

      I have created a graphical item containing a button and an editable text box, and I have added this item to an existing scene :
      0_1528632673411_stackoverflow.png
      But I want the whole item to be movable.
      I tried to use the setFlags method on the QGraphicsProxyWidget, but it doesn't work. Here is my code :

      QWidget widget;
      
          QPushButton* button = new QPushButton("Button1");
          QLineEdit *numberEdit = new QLineEdit;
      
          QFormLayout *layout = new QFormLayout;
          layout->addRow(button, numberEdit);
          widget.setLayout(layout);
      
          widget.setFixedSize(200,80);
      
          QGraphicsScene scene;
      
          QGraphicsWidget* proxy =  scene.addWidget(&widget);
      
          proxy->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
      
          QGraphicsView view (&scene);
      
          view.show();
      

      So here's my question : do I have to create a custom QWidget class and reimplement the mouse event methods, or is there a simpler way to do this ?
      Thank you for your help.

      1 Reply Last reply Reply Quote 0
      • KillerSmath
        KillerSmath last edited by KillerSmath

        Here is a good way to solve this problem:

        • https://stackoverflow.com/questions/15413564/make-qgraphicsproxywidget-movable-selectable/35684779#35684779

        Resume of code (@rbaleksandar):

            // Create the rect to control the widget
            QGraphicsRectItem *proxyControl = scene.addRect(0, 0, widget.width(), 20, QPen(Qt::black), QBrush(Qt::darkGreen));
            proxyControl->setFlag(QGraphicsItem::ItemIsMovable, true);
            proxyControl->setFlag(QGraphicsItem::ItemIsSelectable, true);
        
            // Insert the widget
            QGraphicsProxyWidget *proxy = scene.addWidget(&widget);
            proxy->setPos(0, proxyControl->rect().height());
            proxy->setParentItem(proxyControl);
        

        @Computer Science Student - Brazil
        Web Developer and Researcher
        “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

        1 Reply Last reply Reply Quote 4
        • D
          DMIII last edited by

          @KillerSmath This is great, thank you very much for the answer !

          1 Reply Last reply Reply Quote 0
          • First post
            Last post