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. I want to open a new window when a push button is clicked but it gets it on the main window
Forum Updated to NodeBB v4.3 + New Features

I want to open a new window when a push button is clicked but it gets it on the main window

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 7.0k 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.
  • R Offline
    R Offline
    Rahma
    wrote on last edited by
    #1

    void MainWindow::on_addhabit_clicked()
    {

    addhabit *habitwindow=new addhabit(this,this);
    habitwindow->show();
    }
    in the main window when addhabit button is clicked it should open a new different window but instead it open it on main window that both there items appear together

    jsulmJ 1 Reply Last reply
    0
    • R Rahma

      void MainWindow::on_addhabit_clicked()
      {

      addhabit *habitwindow=new addhabit(this,this);
      habitwindow->show();
      }
      in the main window when addhabit button is clicked it should open a new different window but instead it open it on main window that both there items appear together

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @Rahma said in I want to open a new window when a push button is clicked but it gets it on the main window:

      addhabit *habitwindow=new addhabit(this,this);

      I guess addhabit is a QWidget?
      Simply do not pass parent (this) to it then it will be a window.
      See https://doc.qt.io/qt-5/qwidget.html#QWidget :
      "If parent is 0, the new widget becomes a window. If parent is another widget, this widget becomes a child window inside parent."

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      R 1 Reply Last reply
      5
      • jsulmJ jsulm

        @Rahma said in I want to open a new window when a push button is clicked but it gets it on the main window:

        addhabit *habitwindow=new addhabit(this,this);

        I guess addhabit is a QWidget?
        Simply do not pass parent (this) to it then it will be a window.
        See https://doc.qt.io/qt-5/qwidget.html#QWidget :
        "If parent is 0, the new widget becomes a window. If parent is another widget, this widget becomes a child window inside parent."

        R Offline
        R Offline
        Rahma
        wrote on last edited by
        #3

        @jsulm
        ohhhhh i get it now ...... thanks

        Pablo J. RoginaP 1 Reply Last reply
        0
        • R Rahma

          @jsulm
          ohhhhh i get it now ...... thanks

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

          @Rahma

          addhabit *habitwindow=new addhabit(this,this);

          But if you don't pass a parent (this) please keep in mind that you'll need to manage your memory allocation (new) yourself...

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          R 1 Reply Last reply
          0
          • Pablo J. RoginaP Pablo J. Rogina

            @Rahma

            addhabit *habitwindow=new addhabit(this,this);

            But if you don't pass a parent (this) please keep in mind that you'll need to manage your memory allocation (new) yourself...

            R Offline
            R Offline
            Rahma
            wrote on last edited by
            #5
            This post is deleted!
            Pablo J. RoginaP 1 Reply Last reply
            0
            • R Rahma

              This post is deleted!

              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #6

              @Rahma you need to share more of your code, sharing just snippets is making others harder...

              As you posted before:

              void MainWindow::on_addhabit_clicked()
              {
              ...
              

              that will work if you have a QPushButton object called addhabit also defined in your MainWindow class

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              2
              • J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                The easiest and IMOH cleanest solution is to give the underlying QWidget Constructor the Window tag

                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                
                    QWidget w0;
                    w0.show();
                
                    QWidget *w = new QWidget(&w0, Qt::Window);
                    w->show();
                    w->resize(200,200);
                    w->raise();
                
                    return  a.exec();
                }
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved