开发者

adding widget dynamically in qt

开发者 https://www.devze.com 2023-01-24 20:19 出处:网络
I have a small problem with adding widget to QM开发者_运维百科ainWindow. When i do it like that:

I have a small problem with adding widget to QM开发者_运维百科ainWindow. 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?


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()));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消