Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding widget dynamically in qt

Tags:

c++

qt

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?

like image 632
sebastian Avatar asked Nov 14 '10 18:11

sebastian


People also ask

What is Qtwidgets?

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.

What is QVBoxLayout?

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().

How do I remove a widget from Qt?

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() .


1 Answers

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()));
}
like image 60
Sebastian Negraszus Avatar answered Oct 03 '22 06:10

Sebastian Negraszus