Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Minimal subclass of QWidget does not display like its ancestor... what more do I need to implement?
Forum Updated to NodeBB v4.3 + New Features

Minimal subclass of QWidget does not display like its ancestor... what more do I need to implement?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kenchan
    wrote on last edited by
    #1

    Hello everyone,

    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.

    The header
    @
    #ifndef MINIMALWIDGET_H
    #define MINIMALWIDGET_H

    #include <QtWidgets/QWidget>

    class QWidget;

    class MyContentWidget : public QWidget
    {
    Q_OBJECT

    public:
    explicit MyContentWidget(QWidget *parent=0);
    ~MyContentWidget() {;}

    };

    #endif // MINIMALWIDGET_H
    @

    The main code
    @
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QWidget>
    #include <QtWidgets/QLabel>
    #include <QtWidgets/QPushButton>

    #include "MinimalWidget.h"

    MyContentWidget::MyContentWidget(QWidget *parent)
    : QWidget(parent)
    {
    }

    int main(int argc, char *argv[])
    {
    QApplication a(argc,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&#40;&#41;;
    

    }
    @

    1 Reply Last reply
    0
    • B Offline
      B Offline
      butterface
      wrote on last edited by
      #2

      What happens in the constructor of you widget and how does your widget look like or what is the difference?

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kenchan
        wrote on last edited by
        #3

        Hi butterface,

        The constructor for my widget is in the code posted above... currently it does nothing itself.

        @
        MyContentWidget::MyContentWidget(QWidget *parent)
        : QWidget(parent)
        {
        }
        @

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved