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 open MainWindow with signal
Qt 6.11 is out! See what's new in the release blog

How to open MainWindow with signal

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.2k Views 3 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.
  • I Offline
    I Offline
    isan
    wrote on last edited by
    #1

    I want to open MainWindow with button clicked signal, but the MainWindow close right after open it, why this happening?

     Dialog dialog;
     dialog.show();
     QObject::connect(
         &dialog, &Dialog::onSetClicked,
         [](const QString &port, const QString &config, const QString &model) {
           MainWindow demo(port, config, model);
           demo.show();
         });
    
    M 1 Reply Last reply
    0
    • I isan

      I want to open MainWindow with button clicked signal, but the MainWindow close right after open it, why this happening?

       Dialog dialog;
       dialog.show();
       QObject::connect(
           &dialog, &Dialog::onSetClicked,
           [](const QString &port, const QString &config, const QString &model) {
             MainWindow demo(port, config, model);
             demo.show();
           });
      
      M Offline
      M Offline
      mpergand
      wrote on last edited by
      #2

      @isan said in How to open MainWindow with signal:

      QObject::connect(
      &dialog, &Dialog::onSetClicked,
      [](const QString &port, const QString &config, const QString &model) {
      MainWindow demo(port, config, model); --> local variable !
      demo.show();
      });

      I 1 Reply Last reply
      3
      • M mpergand

        @isan said in How to open MainWindow with signal:

        QObject::connect(
        &dialog, &Dialog::onSetClicked,
        [](const QString &port, const QString &config, const QString &model) {
        MainWindow demo(port, config, model); --> local variable !
        demo.show();
        });

        I Offline
        I Offline
        isan
        wrote on last edited by
        #3

        @mpergand can you explain more?

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

          Hi,

          You create demo in your lambda. It will be destroyed at the end of the function.

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

          I 1 Reply Last reply
          3
          • SGaistS SGaist

            Hi,

            You create demo in your lambda. It will be destroyed at the end of the function.

            I Offline
            I Offline
            isan
            wrote on last edited by
            #5

            @SGaist How can I fix it?

            M 1 Reply Last reply
            0
            • I isan

              @SGaist How can I fix it?

              M Offline
              M Offline
              mpergand
              wrote on last edited by mpergand
              #6

              @isan How can I fix it?

              MainWindow* demo=new MainWindow(port, config, model);
              

              Don't forget to add this in the MainWindow constructor:

              setAttribute(Qt::WA_DeleteOnClose);
              

              otherwise you will have a memory leak.

              mrjjM 1 Reply Last reply
              4
              • M mpergand

                @isan How can I fix it?

                MainWindow* demo=new MainWindow(port, config, model);
                

                Don't forget to add this in the MainWindow constructor:

                setAttribute(Qt::WA_DeleteOnClose);
                

                otherwise you will have a memory leak.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @mpergand
                did we miss a new in the code ?

                M 1 Reply Last reply
                1
                • mrjjM mrjj

                  @mpergand
                  did we miss a new in the code ?

                  M Offline
                  M Offline
                  mpergand
                  wrote on last edited by mpergand
                  #8

                  @mrjj said in How to open MainWindow with signal:

                  @mpergand
                  did we miss a new in the code ?

                  Yes indeed :)

                  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