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. How to properly create new QWidget "window" - as pointer or not?
Qt 6.11 is out! See what's new in the release blog

How to properly create new QWidget "window" - as pointer or not?

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

    Hi,
    I have main GUI class (QMainWindow). It contains pushbutton that can create MyWidget class (subclass QWidget). MyWidget it is widget that should be showed as a window.
    My question is that:
    If I create MyWidget as:

    class BaseWindow :  public QMainWindow
    {
        Q_OBJECT
    public:
        BaseWindow(QWidget *parent = 0);
        ~BaseWindow();
    
        //Other stuff
    public slots:
        void createMyWidget();
    
    private:
        //Other stuff
        MyWidget* widgetPtr;
    }
    

    and in createMyWidget() method I have:

    void BaseWindow::createMyWidget()
    {
        widgetPtr = new MyWidget(0);
        widgetPtr->show();
    }
    

    How to properly use "new" if can't destroy that widget (it can be closed by clicking exit button). I mean that if the pointer is created by new it should be also deleted.
    The proper way (in C++) is to create MyWidget by new and after closing it should be deleted. But window is deleted after using exit button, so I cannot delete it on my own.
    If I should leave it in above form or there is better way to do this?

    I hope I wrote it clearly.
    Thanks

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      @never_ever said:

      well one way is to let the mainwindow create other windows in constructor (hidden)
      and then delete them in the destructor.

      So you createMyWidget would only show it.

      there is also the
      Qt::WA_DeleteOnClose
      flag to make dialogs delete them self when closed.

      also you dont have to new them. you can have
      private:
      //Other stuff
      MyWidget widget; // no pointer

      and it dies with parent.

      N 1 Reply Last reply
      0
      • mrjjM mrjj

        @never_ever said:

        well one way is to let the mainwindow create other windows in constructor (hidden)
        and then delete them in the destructor.

        So you createMyWidget would only show it.

        there is also the
        Qt::WA_DeleteOnClose
        flag to make dialogs delete them self when closed.

        also you dont have to new them. you can have
        private:
        //Other stuff
        MyWidget widget; // no pointer

        and it dies with parent.

        N Offline
        N Offline
        never_ever
        wrote on last edited by
        #3

        @mrjj thanks

        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