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