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. Calling setWidget a second time on QDockWidget

Calling setWidget a second time on QDockWidget

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.0k Views
  • 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.
  • P Offline
    P Offline
    piotrek0
    wrote on last edited by
    #1

    When I call setWidget on a QDockWidget, I'd expect the QDockWidget to take ownership of that widget. In particular, I'd expect that the widget specified via a previous setWidget call would be deleted on a subsequent setWidget call.

    This is not the behavior of QDockWidget in 5.4.1, and I'm wondering whether this is WAI or a bug. If WAI, what's the rationale for this behavior?

    Thanks!
    Peter

    1 Reply Last reply
    0
    • Resurr3ctionR Offline
      Resurr3ctionR Offline
      Resurr3ction
      wrote on last edited by
      #2

      The documentation does not state that the QDockWidget takes ownership of the widget set by its setWidget method. If it would be the case it would be stated in the documentation. I will not speculate about the reasons but it can be simply solved by this code:

      //assuming this is something like QMainWindow or some other widget
      QDockWidget *dock = new QDockWidget(this); 
      QWidget *widget1 = new QWidget(dock); //or widget1->setParent(dock);
      dock->setWidget(widget1);
      
      if(dock->widget())
          dock->widget()->deleteLater();
      
      QWidget *widget2 = new QWidget(dock); //or widget2->setParent(dock);
      dock->setWidget(widget2);
      

      You can write a custom class that inherits the QDockWidget and performs this for you inside it (you can then use that instead of QDockWidget and/or promote any QDockWidget to it in the designer if you use it).

      P 1 Reply Last reply
      1
      • Resurr3ctionR Resurr3ction

        The documentation does not state that the QDockWidget takes ownership of the widget set by its setWidget method. If it would be the case it would be stated in the documentation. I will not speculate about the reasons but it can be simply solved by this code:

        //assuming this is something like QMainWindow or some other widget
        QDockWidget *dock = new QDockWidget(this); 
        QWidget *widget1 = new QWidget(dock); //or widget1->setParent(dock);
        dock->setWidget(widget1);
        
        if(dock->widget())
            dock->widget()->deleteLater();
        
        QWidget *widget2 = new QWidget(dock); //or widget2->setParent(dock);
        dock->setWidget(widget2);
        

        You can write a custom class that inherits the QDockWidget and performs this for you inside it (you can then use that instead of QDockWidget and/or promote any QDockWidget to it in the designer if you use it).

        P Offline
        P Offline
        piotrek0
        wrote on last edited by
        #3

        @Resurr3ction Thanks for the explanation! I thought that widgets taking ownership of children was the norm, even if not explicitly documented. Good to know.

        1 Reply Last reply
        0
        • Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Resurr3ction said:

          If it would be the case it would be stated in the documentation.

          That's not actually true. The fact that ownership is transferred is rarely mentioned in the docs except for the cases it might not be clear(an example). It's the opposite that is always clearly stated - when the ownership is not transferred (an example). If there's nothing in the docs you should assume the ownership is indeed transferred.

          In this particular case the ownership is transferred. You can test it with this small example:

          #include <QApplication>
          #include <QDockWidget>
          #include <QDebug>
          
          struct Test : public QWidget {
            ~Test() { qDebug() << "deleted!"; }
          };
          
          int main(int argc, char *argv[]) {
              QApplication a(argc, argv);
          
              QDockWidget dw;
              dw.show();
          
              dw.setWidget(new Test());
              dw.setWidget(new Test());
          
              return a.exec();
          }
          

          Both instances of Test are correctly deleted by the dock widget's destructor. Note however that setting new widget does not delete previous one immediately. It merely hides it until they are both deleted at the dock's destructor.

          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