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. [Solved] Open new UI -> Closes immediately
QtWS25 Last Chance

[Solved] Open new UI -> Closes immediately

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 4.1k 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.
  • I Offline
    I Offline
    itsmemax
    wrote on last edited by
    #1

    Hello, I've got a more or less simple question. I'm trying to open a second UI (settings.ui) when a user presses a specific button. It works, but the UI closes again immadiately. Here's the code:

    @settings settingsUi;
    settingsUi.setAttribute(Qt::WA_DeleteOnClose);
    settingsUi.show();@

    Can anyone help me with this?

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

      Hi,

      I suppose you have something like:
      @
      void MyWidget::myFunction(){
      settings settingsUi;
      settingsUi.setAttribute(Qt::WA_DeleteOnClose);
      settingsUi.show();
      }
      @

      After calling show(), your settings variable goes out of scope so it's destroyed.

      You need to either exec() rather than show() it or allocate in on the heap

      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
      • I Offline
        I Offline
        itsmemax
        wrote on last edited by
        #3

        Thank you, I already managed it myself. Here's what I did:

        mainwindow.h
        @#include <newdialog.h>
        ...
        public slots:
        void openSettings();
        ...
        private:
        Ui::MainWindow *ui;
        NewDialog *m_dialogSettings;
        };@

        mainwindow.cpp:
        @void MainWindow::openSettings()
        {
        m_dialogSettings = new NewDialog();
        m_dialogSettings->show();
        m_dialogSettings->raise();
        m_dialogSettings->activateWindow();
        }@
        Then I called it with openSettings(). Simple. Thanks for your help though!

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

          But now you have a memory leak. Each time you call openSettings you create a new "NewDialog" and replace the old one without deleting it.

          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
          • I Offline
            I Offline
            itsmemax
            wrote on last edited by
            #5

            True, checked it with the TaskManager. The dialog stays in the memory. Couldn't I do something like this?

            @void MainWindow::openSettings()
            {
            m_dialogSettings = new Dialog();
            if(m_dialogSettings){
            m_dialogSettings->close();}
            m_dialogSettings->show();
            m_dialogSettings->raise();
            m_dialogSettings->activateWindow();
            }@

            /edit: Wrong solution. This works:
            @Dialog::Dialog(QWidget *parent) :
            QDialog(parent),
            ui(new Ui::Dialog)
            {
            Dialog::setAttribute(Qt::WA_DeleteOnClose);
            ui->setupUi(this);
            }@
            It's my new constructor for the Dialog.

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

              @Dialog::setAttribute(Qt::WA_DeleteOnClose);@

              Should throw an error since you are trying to call a non static function without any object.

              @
              m_dialogSettings = new Dialog();
              if(m_dialogSettings){
              m_dialogSettings->close();}
              @

              Doesn't really make sense, you close the dialog each time you call this function then show it again, and you still have the memory leak.

              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