How to create "float" elements?



  • I want place a QGraphicsView fullfill the window, and some buttons "overlay" it. How to layout them?
    I found that no matter what layout I use, these buttons "beside" the QGraphicsView other than "float" above it, unless I set the QGraphicsView's size equal to window's size. But when my user adjust the window's size at run time, The QGraphicsView's size will not change follow the window size.
    0_1531112366915_29f0010f-887e-48fc-bce6-20f4a22b7734-image.png


  • Moderators

    @rock-wang You don't have any layout. Select both QGraphicsView and the button and then press on "Layout in a Grid" button.
    And you should read http://doc.qt.io/qt-5/layout.html



  • @jsulm said in How to create "float" elements?:

    u don't hav
    I modified my UI
    0_1531118987034_2a7e2d5f-fe5c-49b6-8d10-6752a61620ca-image.png

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>810</width>
        <height>526</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralWidget">
       <layout class="QGridLayout" name="gridLayout">
        <item row="0" column="0">
         <widget class="QGraphicsView" name="graphicsView"/>
        </item>
        <item row="0" column="1">
         <widget class="QGroupBox" name="buttonGroup">
          <property name="sizePolicy">
           <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
            <horstretch>0</horstretch>
            <verstretch>0</verstretch>
           </sizepolicy>
          </property>
          <layout class="QVBoxLayout" name="verticalLayout">
           <item>
            <widget class="QPushButton" name="button1">
             <property name="text">
              <string>Button1</string>
             </property>
            </widget>
           </item>
           <item>
            <widget class="QPushButton" name="Button2">
             <property name="text">
              <string>Button2</string>
             </property>
            </widget>
           </item>
           <item>
            <widget class="QPushButton" name="pushButton_3">
             <property name="text">
              <string>Button3</string>
             </property>
            </widget>
           </item>
           <item>
            <widget class="QPushButton" name="pushButton_6">
             <property name="text">
              <string>Button4</string>
             </property>
            </widget>
           </item>
           <item>
            <widget class="QPushButton" name="pushButton_4">
             <property name="text">
              <string>Button5</string>
             </property>
            </widget>
           </item>
           <item>
            <widget class="QPushButton" name="pushButton_5">
             <property name="text">
              <string>Button6</string>
             </property>
            </widget>
           </item>
           <item>
            <widget class="QPushButton" name="pushButton_9">
             <property name="text">
              <string>Button7</string>
             </property>
            </widget>
           </item>
           <item>
            <widget class="QPushButton" name="pushButton_7">
             <property name="text">
              <string>Button8</string>
             </property>
            </widget>
           </item>
           <item>
            <widget class="QPushButton" name="pushButton_8">
             <property name="text">
              <string>Button9</string>
             </property>
            </widget>
           </item>
           <item>
            <widget class="QPushButton" name="pushButton_10">
             <property name="text">
              <string>Button10</string>
             </property>
            </widget>
           </item>
          </layout>
         </widget>
        </item>
       </layout>
      </widget>
     </widget>
     <layoutdefault spacing="6" margin="0"/>
     <resources/>
     <connections/>
    </ui>
    
    

    What I expected is this
    0_1531119179828_29dbc99a-68ac-4c22-9a79-f64e5c026cfd-image.png



  • @rock-wang said in How to create "float" elements?:

    The QGraphicsView's size will not change follow the window size.

    Just reimplement resizeEvent and resize the QGraphicsView



  • @VRonin said in How to create "float" elements?:

    Just reimplement resizeEvent and resize the QGraphicsView

    Any other solutions?For example the layout.



  • Nope, there's no layout that puts stuff on top of each other. The resizeEvent solution, however, should be just a couple of lines of code...

    void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE{
    ui->graphicsView->resize(size());
    QWidget::resizeEvent(event);
    }
    


  • @VRonin said in How to create "float" elements?:

    Nope, ther's no layout that puts stuff on top of each other.

    Actually, iirc, you can trick the QGridlayout. It's not possible with the desginer, but you can asign multiple objects to the same cell via code. Those are placed on top of each other and the size of each object is managed via the Layout.


Log in to reply
 

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