Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Minimal subclass of QWidget does not display like its ancestor... what more do I need to implement?
kenchan last edited by
I have made a minimal subclass of a QWidget. My subclasses widget does not display exactly the same as the QWidget it is derived from. since I have not changed any settings after deriving it I was expecting it appear and behave exactly like the default QWidget implementation. However, it appears to behave differently in my example code shown below. It does not display in the same way as the straight QWidget object does.
Can someone please explain what is happening here and what I need to make my class behave like the parent class here?
My thanks to all who offer suggestions.
class MyContentWidget : public QWidget
explicit MyContentWidget(QWidget *parent=0);
#endif // MINIMALWIDGET_H
The main code
int main(int argc, char *argv)
// a transparent top level widget QWidget *widget = new QWidget; widget->setWindowFlags(Qt::FramelessWindowHint); widget->setAttribute(Qt::WA_TranslucentBackground); widget->setStyleSheet("background:rgba(0,0,0,50%);"); widget->setFixedSize(QSize(500,500)); // a label so we can see the top widget QLabel *label = new QLabel(widget); label->setFixedSize(500, 500); label->setStyleSheet("border-radius: 20px;"); // a close button QPushButton *button = new QPushButton(label); button->setGeometry(20,20,100,50); button->setStyleSheet("background-color: white; border-radius: 10px;"); button->setText("Close"); widget->connect(button, SIGNAL(pressed()), &a, SLOT(closeAllWindows())); button->setShortcut(QKeySequence("Ctrl+Q")); button->setToolTip(a.tr("Close the application (Qtrl-Q)")); // a widget for some content on the left side QWidget *contentWidget1 = new QWidget(label); contentWidget1->setGeometry(25,150,200,200); // a label describing the widget on the left QLabel *label1 = new QLabel(contentWidget1); label1->setWordWrap(true); label1->setText("This is a QWidget"); label1->setStyleSheet("color : black; background-color : white; border-radius: 0;"); // my minimal subclassed widget on the right side // my problem is why does this not appear to be the same as the default widget on the left? MyContentWidget *contentWidget2 = new MyContentWidget(label); contentWidget2->setGeometry(275,150,200,200); // a label describing my widget QLabel *label2 = new QLabel(contentWidget2); label2->setFixedWidth(200); label2->setWordWrap(true); label2->setText("This is my minimal subclassed QWidget.\nSo what happened to its background colour?\nWhy is it not like the one on the left?\nWhat do I need to do to make it appear like the one on the left?"); label2->setStyleSheet("color: black; background-color: white; border-radius: 0;"); // get the show on the road widget->show(); return a.exec();
butterface last edited by
What happens in the constructor of you widget and how does your widget look like or what is the difference?
kenchan last edited by
The constructor for my widget is in the code posted above... currently it does nothing itself.
The difference from the default implementation is that is does not appear slightly darker like the default QWidget does. Also, It does not respond to changing the background colour with setStyleSheet() like the default one does.
"Here is a screenshot of the output of the posted code" :https://www.dropbox.com/s/rguyeeed4qtv4fo/SubclassedQWidget.png
As you can see the QWidget on the right does not have the same appearance as the one on the left. I expected it to appear as a similar darker area behind the text. It is the same shape and is defined in the same way as the one on the left. Why is it different?
So I am interested to know member functions I must override to make it behave normally.