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. Movable QWidget
Qt 6.11 is out! See what's new in the release blog

Movable QWidget

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 2.4k 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.
  • D Offline
    D Offline
    DMIII
    wrote on last edited by
    #1

    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
    0
    • KillerSmathK Offline
      KillerSmathK Offline
      KillerSmath
      wrote on last edited by KillerSmath
      #2

      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
      4
      • D Offline
        D Offline
        DMIII
        wrote on last edited by
        #3

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

        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