I have a small problem with adding widget to QMainWindow. When i do it like that:
wsk_mainStatki = new mainStatki(this);
wsk_mainStatki ->setGeometry(0,0,400,300);
this->layout()->addWidget(wsk_mainStatki);
it's ok but i get warning:
QMainWindowLayout::addItem: Please use the public QMainWindow API instead
this is my game class
#include "game.h"
game::game()
{
setGeometry(200, 200, 400, 300);
setWindowTitle("Statki");
wsk_mainStatki = new mainStatki(this);
wsk_mainStatki ->setGeometry(0,0,400,300);
this->layout()->addWidget(wsk_mainStatki);
}
game header
#ifndef WIDGET1_H
#define WIDGET1_H
#include "k_plansza.h"
#include "mainStatki.h"
#include "settings.h"
#include <QApplication>
#include <QMainWindow>
class game : public QMainWindow
{
public:
game();
~game() {};
private:
mainStatki *wsk_mainStatki;
settings *wsk_settings;
};
#endif // WIDGET1_H
mainstatki class
#include "mainstatki.h"
mainStatki::mainStatki(QWidget *parent){
setupUi(this);
connect(closeButton, SIGNAL(clicked()), parent, SLOT(close()));
}
mainstatki header
#ifndef MAINSTATKI_H
#define MAINSTATKI_H
#include <QWidget>
#include "ui_mainStatki.h"
class mainStatki : public QWidget, public Ui::mainStatki
{
Q_OBJECT
public:
mainStatki(QWidget *parent);
};
#endif // MAINSTATKI_H
How it should look?
Widgets are the primary elements for creating user interfaces in Qt. Widgets can display data and status information, receive user input, and provide a container for other widgets that should be grouped together. A widget that is not embedded in a parent widget is called a window.
QVBoxLayout::QVBoxLayout(QWidget *parent)Constructs a new top-level vertical box with parent parent. The layout is set directly as the top-level layout for parent. There can be only one top-level layout for a widget. It is returned by QWidget::layout(). See also QWidget::setLayout().
Removing widgets from layout We use convenient function removeWidget() from Qt for the purpose. First, we get the item at index 0 in the grid using itemAt(0) then after we can remove it from the layout using removeItem() .
I believe it means you are not expected to manually insert stuff into the layout of a QMainWindow, but instead use methods like addToolBar, setStatusBar or setCentralWidget. The layouting of your own widgets would happen in the centralWidget.
By the way, your mainStatki constructor is missing a call to the QWidget constructor. Unless you have a good reason not to do it, your constructor should rather look like this:
mainStatki::mainStatki(QWidget *parent)
: QWidget(parent)
{
setupUi(this);
connect(closeButton, SIGNAL(clicked()), parent, SLOT(close()));
}
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