[英]adding widget dynamically in qt
I have a small problem with adding widget to QMainWindow. 将小部件添加到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
QMainWindowLayout :: addItem:请改用公共QMainWindow API
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 mainstatki类
#include "mainstatki.h"
mainStatki::mainStatki(QWidget *parent){
setupUi(this);
connect(closeButton, SIGNAL(clicked()), parent, SLOT(close()));
}
mainstatki header mainstatki标题
#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. 我相信这意味着你不应该手动将东西插入到QMainWindow的布局中,而是使用addToolBar,setStatusBar或setCentralWidget等方法。 The layouting of your own widgets would happen in the centralWidget.
您自己的小部件的布局将在centralWidget中进行。
By the way, your mainStatki constructor is missing a call to the QWidget constructor. 顺便说一句,你的mainStatki构造函数缺少对QWidget构造函数的调用。 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()));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.