I'm trying to add QWidgets at the top of a QVBoxLayout. But they get inserted at the bottom, growing upwards. In the QVBoxLayout is a vertical spacer to 'push' all widgets to the top (but they are pushed to the bottom). With Ctrl+A the widgets are inserted.
The widget inserted is a simple widget with horizontal layout and two labels.
How do I get these widgets to be inserted/pushed at/to the top?
On every insert there's a small windows window visible without caption or text. It disappears really quickly. Why is this? I don't want it. How do I get rid of this?
Below is a small example.
mainwindow.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>400</width>
<height>300</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="QWidget" name="widget" native="true">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>First at top</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>221</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menuAdd">
<property name="title">
<string>File</string>
</property>
<addaction name="actionFile"/>
<addaction name="separator"/>
<addaction name="actionE_xit"/>
</widget>
<addaction name="menuAdd"/>
</widget>
<action name="actionFile">
<property name="text">
<string>Add</string>
</property>
<property name="shortcut">
<string>Ctrl+A</string>
</property>
</action>
<action name="actionE_xit">
<property name="text">
<string>E&xit</string>
</property>
<property name="shortcut">
<string>Ctrl+X</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_actionFile_triggered();
void on_actionE_xit_triggered();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionE_xit_triggered()
{
QApplication::quit();
}
void MainWindow::on_actionFile_triggered()
{
int i = ui->verticalLayout->count();
QHBoxLayout* layout = new QHBoxLayout();
layout->addWidget(new QLabel(tr("One.%1").arg(i)));
layout->addWidget(new QLabel(tr("Two.%1").arg(i)));
QWidget* window = new QWidget;
window->setLayout(layout);
window->show();
ui->verticalLayout->addWidget(window);
}
Instead of addWidget()
, use insertWidget()
with an index of 0
.
About the small, unwanted window: This is probably because you are creating QLabel
s without specifiying a parent. Thus they are created as toplevel-windows until you add them to the layout. Specify your window
as parent (first ctor argument of QLabel
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With