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. Custom widget set as centralWidget dissapears upon "setStyleSheet"

Custom widget set as centralWidget dissapears upon "setStyleSheet"

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 466 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.
  • C Offline
    C Offline
    CraigglesO
    wrote on last edited by
    #1

    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());
        ui->setupUi(this);
    
        Homescreen *homescreen = new Homescreen(this);
        qApp->setStyleSheet(
                    "Homescreen { "
                    "background-image: url(\"/User/craigoconnor/Downloads/backgroundTest2.jpg\") 0 0 0 0 stretch stretch;"
                    "background-color: blue;"
                    "}");
        homescreen->setAutoFillBackground(true);
    
        setCentralWidget(homescreen);
    }
    

    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?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      CraigglesO
      wrote on last edited by CraigglesO
      #2

      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..

      https://forum.qt.io/topic/69022/set-background-image-does-not-work/6
      @raven-worx showed that with a custom widget, it needs a paint event to handle backgrounds:

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

      New to this forum - how to mark as solved?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        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