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 ?
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
#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. Thank you !
Q_OBJECTfromclass fenetre(as I wasn't able to involve moc properly and haven't any experience with it). In this MCVE,Q_OBJECTis probably rather useless - not sure if it plays a role for your issue... - Scheff's CatsetStyleSheet()function, I had to implement thepaintEventfunction in my custom class to make it work. - ganzo dbfenetre::paintEvent()? It should be inherited fromQWidget(), shouldn't it? In my case, it worked withoutpaintEvent(). I just copy/pasted your code and the only thing I did was to changeQ_OBJECTinto//Q_OBJECT. Could it be that introducing a new meta-type forclass fenetrehas such consequences? - Scheff's CatpaintEventto the custom widget - ganzo dbpaintEvent(). (I swear.) ;-) On what OS, you did run your test? - Scheff's Cat