[英]Add custom QWidget to another QWidget
I'm trying to add an objects (that inherits from QWidget
) as a child to another QWidget
as shown below, it works perfectly with another normal QWidget
instance but not with my custom class, any idea why ?我正在尝试将一个对象(从
QWidget
继承)作为子项添加到另一个QWidget
,如下所示,它与另一个普通QWidget
实例完美配合,但不适用于我的自定义类,知道为什么吗?
fenetre.h fenetre.h
#ifndef FENETRE_H
#define FENETRE_H
#include <QWidget>
#include <QMouseEvent>
class Fenetre : public QWidget
{
Q_OBJECT
public:
Fenetre();
};
#endif // FENETRE_H
fenetre.cpp fenetre.cpp
#include "fenetre.h"
Fenetre::Fenetre() : QWidget()
{
}
main.cpp主程序
#include <iostream>
#include <QApplication>
#include <QWidget>
#include "fenetre.h"
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
QWidget window;
window.setFixedSize(800,600);
//This appears
QWidget rec1;
rec1.setParent(&window);
rec1.setFixedSize(100,100);
rec1.move(400,200);
rec1.setStyleSheet("background-color: red");
//This one not
Fenetre rec2;
rec2.setParent(&window);
rec2.setFixedSize(100,100);
rec2.move(200,200);
rec2.setStyleSheet("background-color: green");
window.show();
return app.exec();
}
PS: I did research on the platform, but the majority of the answers speak of the use of a layout. PS:我在平台上做了研究,但大多数答案都谈到了布局的使用。 Thank you !
谢谢 !
you miss the parent:你想念父母:
//header .h
class Fenetre : public QWidget
{
Q_OBJECT
public:
Fenetre(QWidget *parent = 0);
};
//source .cpp
Fenetre::Fenetre(QWidget *parent) : QWidget(parent)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.