Qt World Summit: Register Today!

Custom widget set as centralWidget dissapears upon "setStyleSheet"

  • I use a custom widget to display backgrounds. Currently I am just trying to keep the widget displayed.

    Homescreen is currently really basic, it takes the input of the mainwindow and resizes to fit the entirely of the window.

    MainWindow::MainWindow(QWidget *parent, QDesktopWidget *dw): QMainWindow(parent), ui(new Ui::MainWindow) {
        // prep work
        setFixedSize(dw->width(), dw->height());
        Homescreen *homescreen = new Homescreen(this);
                    "Homescreen { "
                    "background-image: url(\"/User/craigoconnor/Downloads/backgroundTest2.jpg\") 0 0 0 0 stretch stretch;"
                    "background-color: blue;"

    It will recognize the background color, ignoring the background image, and if i ever try to change the style in any way (from inside the homescreen object or other) then the object just stops displaying. If i DONT call "homescreen->setAutoFillBackground(true);" nothing will ever get displayed....

    Any advice on how to edit a widget properly upon instantiation?

  • Oof.. two days I've been trying to figure this one out, turns out it's really simple and I just happened to find the solution after I finally bothered to post a question..

    @raven-worx showed that with a custom widget, it needs a paint event to handle backgrounds:

    void CustomWidget::paintEvent(QPaintEvent *)
        QStyleOption opt;
        QPainter p(this);
        style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);

    New to this forum - how to mark as solved?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    The "Topic Tools" button is what you are looking for :)

Log in to reply