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. showing custom widget inside the mainWindow

showing custom widget inside the mainWindow

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 7 Posters 3.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.
  • S Offline
    S Offline
    Swati777999
    wrote on 21 Dec 2021, 04:31 last edited by
    #1

    Hi All,

    I have a custom widget of QWidget type and I want to show it inside the main window. How can I do that?

    When I run the code, I get two windows; a blank window MainWindow whereas the widget is shown in another window.
    How can I get the contents of Widgets displayed on the blank MainWindow?

    Please suggest me some ways to achieve that.

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    J J 2 Replies Last reply 21 Dec 2021, 05:45
    1
    • S Swati777999
      21 Dec 2021, 04:31

      Hi All,

      I have a custom widget of QWidget type and I want to show it inside the main window. How can I do that?

      When I run the code, I get two windows; a blank window MainWindow whereas the widget is shown in another window.
      How can I get the contents of Widgets displayed on the blank MainWindow?

      Please suggest me some ways to achieve that.

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 21 Dec 2021, 05:45 last edited by
      #2

      @Swati777999 said in showing custom widget inside the mainWindow:

      I have a custom widget of QWidget type and I want to show it inside the main window. How can I do that?

      You already did that at https://forum.qt.io/topic/132782/syntax-for-vector-of-qpushbuttons-added-to-flowlayout with your ScrollAreaApp, right?

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      S 1 Reply Last reply 22 Dec 2021, 04:22
      2
      • S Swati777999
        21 Dec 2021, 04:31

        Hi All,

        I have a custom widget of QWidget type and I want to show it inside the main window. How can I do that?

        When I run the code, I get two windows; a blank window MainWindow whereas the widget is shown in another window.
        How can I get the contents of Widgets displayed on the blank MainWindow?

        Please suggest me some ways to achieve that.

        J Online
        J Online
        JonB
        wrote on 21 Dec 2021, 08:12 last edited by
        #3

        @Swati777999 said in showing custom widget inside the mainWindow:

        When I run the code, I get two windows; a blank window MainWindow whereas the widget is shown in another window.

        Apart from what @JKSH says. If this is the case you are not setting the parent of your widget to the main window. You need that for it to be a child widget shown on the parent; a widget with no parent is shown as its own (top-level) window. The main window has no parent, that is why it is shown as its own window.

        1 Reply Last reply
        0
        • J JKSH
          21 Dec 2021, 05:45

          @Swati777999 said in showing custom widget inside the mainWindow:

          I have a custom widget of QWidget type and I want to show it inside the main window. How can I do that?

          You already did that at https://forum.qt.io/topic/132782/syntax-for-vector-of-qpushbuttons-added-to-flowlayout with your ScrollAreaApp, right?

          S Offline
          S Offline
          Swati777999
          wrote on 22 Dec 2021, 04:22 last edited by
          #4

          @JKSH said in showing custom widget inside the mainWindow:

          @Swati777999 said in showing custom widget inside the mainWindow:

          I have a custom widget of QWidget type and I want to show it inside the main window. How can I do that?

          You already did that at https://forum.qt.io/topic/132782/syntax-for-vector-of-qpushbuttons-added-to-flowlayout with your ScrollAreaApp, right?

          Yes, I had done the first part of it but my whole design should be a WIDGET with

          • a heading [QLabel]

          • an array[QVector] of buttons [QPushButtons] in a flow layout

          • above to items arranged in a QVBoxLayout

          • scrolling feature to the window.

          I've achieved this design but I created it in a class which is a subclass of QMainWindow whereas I now realise I've to use this design as a QWidget.

          “ In order to be irreplaceable, one must always be different” – Coco Chanel

          J E 2 Replies Last reply 22 Dec 2021, 06:28
          0
          • S Swati777999
            22 Dec 2021, 04:22

            @JKSH said in showing custom widget inside the mainWindow:

            @Swati777999 said in showing custom widget inside the mainWindow:

            I have a custom widget of QWidget type and I want to show it inside the main window. How can I do that?

            You already did that at https://forum.qt.io/topic/132782/syntax-for-vector-of-qpushbuttons-added-to-flowlayout with your ScrollAreaApp, right?

            Yes, I had done the first part of it but my whole design should be a WIDGET with

            • a heading [QLabel]

            • an array[QVector] of buttons [QPushButtons] in a flow layout

            • above to items arranged in a QVBoxLayout

            • scrolling feature to the window.

            I've achieved this design but I created it in a class which is a subclass of QMainWindow whereas I now realise I've to use this design as a QWidget.

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 22 Dec 2021, 06:28 last edited by
            #5

            @Swati777999 said in showing custom widget inside the mainWindow:

            which is a subclass of QMainWindow whereas I now realise I've to use this design as a QWidget

            Then subclass QWidget instead of QMainWindow...

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

            S 1 Reply Last reply 22 Dec 2021, 08:53
            0
            • S Swati777999
              22 Dec 2021, 04:22

              @JKSH said in showing custom widget inside the mainWindow:

              @Swati777999 said in showing custom widget inside the mainWindow:

              I have a custom widget of QWidget type and I want to show it inside the main window. How can I do that?

              You already did that at https://forum.qt.io/topic/132782/syntax-for-vector-of-qpushbuttons-added-to-flowlayout with your ScrollAreaApp, right?

              Yes, I had done the first part of it but my whole design should be a WIDGET with

              • a heading [QLabel]

              • an array[QVector] of buttons [QPushButtons] in a flow layout

              • above to items arranged in a QVBoxLayout

              • scrolling feature to the window.

              I've achieved this design but I created it in a class which is a subclass of QMainWindow whereas I now realise I've to use this design as a QWidget.

              E Offline
              E Offline
              Ewan Green
              wrote on 22 Dec 2021, 06:45 last edited by
              #6

              @Swati777999 You need to create a layout to store your custom widget. The layout will belong to the main window, and the custom widget will belong to the layout.

              In order to do this in C++, it would look like this:

              in a method of your main window:

              CustomWidget *widget = new CustomWidget(...);/ // i don't know your custom widget constructor
              QVBoxLayout *layout = new QVBoxLayout; //vbox is example; you can use hbox, form, grid, etc. .
              layout->addWidget(widget);
              this->setLayout(layout);
              

              This will show your widget as a child of the main window.

              From the designer, you have to drop a plain QWidget where you want your custom widget to reside in your main window UI file.
              Then, you have to promote your custom widget via. right click, setting the header file & class name parameters.
              Then you can use it as if it were any other widget in the designer, given it's properly promoted

              Ewan Green

              1 Reply Last reply
              2
              • J jsulm
                22 Dec 2021, 06:28

                @Swati777999 said in showing custom widget inside the mainWindow:

                which is a subclass of QMainWindow whereas I now realise I've to use this design as a QWidget

                Then subclass QWidget instead of QMainWindow...

                S Offline
                S Offline
                Swati777999
                wrote on 22 Dec 2021, 08:53 last edited by
                #7

                @jsulm said in showing custom widget inside the mainWindow:

                @Swati777999 said in showing custom widget inside the mainWindow:

                which is a subclass of QMainWindow whereas I now realise I've to use this design as a QWidget

                Then subclass QWidget instead of QMainWindow...

                Yes, that's where I am stuck . Refer to the last message of this post - Vector of QPushbuttons added to a flowlayout

                In this I get 2 windows, one blank window (probably default window of the widget ) and other one is the Main Window.

                #include "scrollareaapp.h"
                
                ScrollAreaApp::ScrollAreaApp(QWidget *parent)
                    : QWidget(parent)
                {  QMainWindow *main = new QMainWindow();
                
                    QWidget *flowWidget = new QWidget(main);
                    FlowLayout *flowLayout = new FlowLayout();
                    flowWidget->setLayout(flowLayout);
                    flowWidget ->setMinimumSize(1200,1200);
                    int n=20;
                    QVector <QPushButton *> buttons(n);
                
                    for (int ii=0;ii<n;ii++)
                    {
                        QPushButton * pb = new QPushButton(); // creating buttons
                
                           pb->setMinimumSize(200,200);
                           buttons.push_back(pb); // adding buttons to qvector
                
                           flowLayout->addWidget(pb);
                
                    }
                
                    QScrollArea *scroll =new QScrollArea();
                
                    QWidget *WindowWidget= new QWidget(this);
                    WindowWidget->setMinimumSize(1000,1000);
                
                    QVBoxLayout *WindowLayout = new QVBoxLayout(WindowWidget);
                
                    QLabel *WindowHeading = new QLabel("Experimenting with Scrollbar in Flow Layout");
                
                    WindowLayout->addWidget(WindowHeading);
                    WindowLayout->setAlignment(WindowHeading,Qt::AlignLeft);
                    WindowLayout->addWidget(flowWidget);
                
                    scroll->setWidget(WindowWidget);
                    main->setLayout(WindowLayout);
                    main->setCentralWidget(scroll);
                
                    main->show();
                    main->setWindowTitle("Main Window");
                }
                

                Can someone tell me how to get a single window(hide widget's plain window, in this case)?

                “ In order to be irreplaceable, one must always be different” – Coco Chanel

                J J 2 Replies Last reply 22 Dec 2021, 08:58
                0
                • C Online
                  C Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 22 Dec 2021, 08:56 last edited by
                  #8

                  Already answered here: https://forum.qt.io/topic/132782/syntax-for-vector-of-qpushbuttons-added-to-flowlayout/47

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0
                  • S Swati777999
                    22 Dec 2021, 08:53

                    @jsulm said in showing custom widget inside the mainWindow:

                    @Swati777999 said in showing custom widget inside the mainWindow:

                    which is a subclass of QMainWindow whereas I now realise I've to use this design as a QWidget

                    Then subclass QWidget instead of QMainWindow...

                    Yes, that's where I am stuck . Refer to the last message of this post - Vector of QPushbuttons added to a flowlayout

                    In this I get 2 windows, one blank window (probably default window of the widget ) and other one is the Main Window.

                    #include "scrollareaapp.h"
                    
                    ScrollAreaApp::ScrollAreaApp(QWidget *parent)
                        : QWidget(parent)
                    {  QMainWindow *main = new QMainWindow();
                    
                        QWidget *flowWidget = new QWidget(main);
                        FlowLayout *flowLayout = new FlowLayout();
                        flowWidget->setLayout(flowLayout);
                        flowWidget ->setMinimumSize(1200,1200);
                        int n=20;
                        QVector <QPushButton *> buttons(n);
                    
                        for (int ii=0;ii<n;ii++)
                        {
                            QPushButton * pb = new QPushButton(); // creating buttons
                    
                               pb->setMinimumSize(200,200);
                               buttons.push_back(pb); // adding buttons to qvector
                    
                               flowLayout->addWidget(pb);
                    
                        }
                    
                        QScrollArea *scroll =new QScrollArea();
                    
                        QWidget *WindowWidget= new QWidget(this);
                        WindowWidget->setMinimumSize(1000,1000);
                    
                        QVBoxLayout *WindowLayout = new QVBoxLayout(WindowWidget);
                    
                        QLabel *WindowHeading = new QLabel("Experimenting with Scrollbar in Flow Layout");
                    
                        WindowLayout->addWidget(WindowHeading);
                        WindowLayout->setAlignment(WindowHeading,Qt::AlignLeft);
                        WindowLayout->addWidget(flowWidget);
                    
                        scroll->setWidget(WindowWidget);
                        main->setLayout(WindowLayout);
                        main->setCentralWidget(scroll);
                    
                        main->show();
                        main->setWindowTitle("Main Window");
                    }
                    

                    Can someone tell me how to get a single window(hide widget's plain window, in this case)?

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 22 Dec 2021, 08:58 last edited by
                    #9

                    @Swati777999 said in showing custom widget inside the mainWindow:

                    Yes, that's where I am stuck

                    In what way stuck?
                    I'm really wondering how often we have to repeat same stuff:

                    1. Do not create a new QMainWindow in your ScrollAreaApp
                    2. Subclass QMainWindow instead of QWidget

                    I give up...

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

                    S 1 Reply Last reply 23 Dec 2021, 02:59
                    1
                    • S Swati777999
                      22 Dec 2021, 08:53

                      @jsulm said in showing custom widget inside the mainWindow:

                      @Swati777999 said in showing custom widget inside the mainWindow:

                      which is a subclass of QMainWindow whereas I now realise I've to use this design as a QWidget

                      Then subclass QWidget instead of QMainWindow...

                      Yes, that's where I am stuck . Refer to the last message of this post - Vector of QPushbuttons added to a flowlayout

                      In this I get 2 windows, one blank window (probably default window of the widget ) and other one is the Main Window.

                      #include "scrollareaapp.h"
                      
                      ScrollAreaApp::ScrollAreaApp(QWidget *parent)
                          : QWidget(parent)
                      {  QMainWindow *main = new QMainWindow();
                      
                          QWidget *flowWidget = new QWidget(main);
                          FlowLayout *flowLayout = new FlowLayout();
                          flowWidget->setLayout(flowLayout);
                          flowWidget ->setMinimumSize(1200,1200);
                          int n=20;
                          QVector <QPushButton *> buttons(n);
                      
                          for (int ii=0;ii<n;ii++)
                          {
                              QPushButton * pb = new QPushButton(); // creating buttons
                      
                                 pb->setMinimumSize(200,200);
                                 buttons.push_back(pb); // adding buttons to qvector
                      
                                 flowLayout->addWidget(pb);
                      
                          }
                      
                          QScrollArea *scroll =new QScrollArea();
                      
                          QWidget *WindowWidget= new QWidget(this);
                          WindowWidget->setMinimumSize(1000,1000);
                      
                          QVBoxLayout *WindowLayout = new QVBoxLayout(WindowWidget);
                      
                          QLabel *WindowHeading = new QLabel("Experimenting with Scrollbar in Flow Layout");
                      
                          WindowLayout->addWidget(WindowHeading);
                          WindowLayout->setAlignment(WindowHeading,Qt::AlignLeft);
                          WindowLayout->addWidget(flowWidget);
                      
                          scroll->setWidget(WindowWidget);
                          main->setLayout(WindowLayout);
                          main->setCentralWidget(scroll);
                      
                          main->show();
                          main->setWindowTitle("Main Window");
                      }
                      

                      Can someone tell me how to get a single window(hide widget's plain window, in this case)?

                      J Offline
                      J Offline
                      JKSH
                      Moderators
                      wrote on 22 Dec 2021, 12:38 last edited by
                      #10

                      @Swati777999 said in showing custom widget inside the mainWindow:

                      In this I get 2 windows, one blank window (probably default window of the widget ) and other one is the Main Window.

                      You said you got your desired results at https://forum.qt.io/post/695547

                      Now, your new code doesn't have the desired results. So, tell us: What changes did you make?

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      S 1 Reply Last reply 23 Dec 2021, 02:58
                      1
                      • J JKSH
                        22 Dec 2021, 12:38

                        @Swati777999 said in showing custom widget inside the mainWindow:

                        In this I get 2 windows, one blank window (probably default window of the widget ) and other one is the Main Window.

                        You said you got your desired results at https://forum.qt.io/post/695547

                        Now, your new code doesn't have the desired results. So, tell us: What changes did you make?

                        S Offline
                        S Offline
                        Swati777999
                        wrote on 23 Dec 2021, 02:58 last edited by
                        #11

                        @JKSH said in showing custom widget inside the mainWindow:

                        @Swati777999 said in showing custom widget inside the mainWindow:

                        In this I get 2 windows, one blank window (probably default window of the widget ) and other one is the Main Window.

                        You said you got your desired results at https://forum.qt.io/post/695547

                        Now, your new code doesn't have the desired results. So, tell us: What changes did you make?

                        Thanks for the patience @JKSH. I appreciate that.

                        Actually ,the entire widget [window with a heading and array of buttons in a vertical layout with scrolling feature]that you see, is a part of another application which is to be imported as a widget.

                        Now , I'm trying to do something like this :

                        scrollareaapp.h

                        class ScrollAreaApp : public QWidget
                        {
                            Q_OBJECT
                        
                        public:
                            ScrollAreaApp(QWidget *parent = nullptr);
                            ~ScrollAreaApp();
                        };
                        
                        

                        scrollareaapp.cpp

                        ScrollAreaApp::ScrollAreaApp(QWidget *parent)
                            : QWidget(parent)
                        {  QMainWindow *main = new QMainWindow();
                        
                        
                            QWidget *flowWidget = new QWidget(main);
                            FlowLayout *flowLayout = new FlowLayout();
                            flowWidget->setLayout(flowLayout);
                            flowWidget ->setMinimumSize(1200,1200);
                            int n=20;
                            QVector <QPushButton *> buttons(n);
                        
                            for (int ii=0;ii<n;ii++)
                            {
                                QPushButton * pb = new QPushButton(); // creating buttons
                        
                                   pb->setMinimumSize(200,200);
                                   buttons.push_back(pb); // adding buttons to qvector
                        
                                   flowLayout->addWidget(pb);
                        
                            }
                        
                            QScrollArea *scroll =new QScrollArea();
                        
                            QWidget *WindowWidget= new QWidget(this);
                            WindowWidget->setMinimumSize(1000,1000);
                        
                            QVBoxLayout *WindowLayout = new QVBoxLayout(WindowWidget);
                        
                            QLabel *WindowHeading = new QLabel("Experimenting with Scrollbar in Flow Layout");
                        
                            WindowLayout->addWidget(WindowHeading);
                            WindowLayout->setAlignment(WindowHeading,Qt::AlignLeft);
                            WindowLayout->addWidget(flowWidget);
                        
                            scroll->setWidget(WindowWidget);
                            main->setLayout(WindowLayout);
                            main->setCentralWidget(scroll);
                        
                            main->show();
                            main->setWindowTitle("Main Window");
                        }
                        

                        So this entire widget is to be imported as a widget which fits into the centralWidget of another application.
                        I hope I made my point clearly.

                        “ In order to be irreplaceable, one must always be different” – Coco Chanel

                        Pl45m4P 1 Reply Last reply 23 Dec 2021, 03:40
                        0
                        • J jsulm
                          22 Dec 2021, 08:58

                          @Swati777999 said in showing custom widget inside the mainWindow:

                          Yes, that's where I am stuck

                          In what way stuck?
                          I'm really wondering how often we have to repeat same stuff:

                          1. Do not create a new QMainWindow in your ScrollAreaApp
                          2. Subclass QMainWindow instead of QWidget

                          I give up...

                          S Offline
                          S Offline
                          Swati777999
                          wrote on 23 Dec 2021, 02:59 last edited by
                          #12

                          @jsulm
                          Thanks for all help. Please ignore my doubts if you find them silly.

                          “ In order to be irreplaceable, one must always be different” – Coco Chanel

                          1 Reply Last reply
                          0
                          • S Swati777999
                            23 Dec 2021, 02:58

                            @JKSH said in showing custom widget inside the mainWindow:

                            @Swati777999 said in showing custom widget inside the mainWindow:

                            In this I get 2 windows, one blank window (probably default window of the widget ) and other one is the Main Window.

                            You said you got your desired results at https://forum.qt.io/post/695547

                            Now, your new code doesn't have the desired results. So, tell us: What changes did you make?

                            Thanks for the patience @JKSH. I appreciate that.

                            Actually ,the entire widget [window with a heading and array of buttons in a vertical layout with scrolling feature]that you see, is a part of another application which is to be imported as a widget.

                            Now , I'm trying to do something like this :

                            scrollareaapp.h

                            class ScrollAreaApp : public QWidget
                            {
                                Q_OBJECT
                            
                            public:
                                ScrollAreaApp(QWidget *parent = nullptr);
                                ~ScrollAreaApp();
                            };
                            
                            

                            scrollareaapp.cpp

                            ScrollAreaApp::ScrollAreaApp(QWidget *parent)
                                : QWidget(parent)
                            {  QMainWindow *main = new QMainWindow();
                            
                            
                                QWidget *flowWidget = new QWidget(main);
                                FlowLayout *flowLayout = new FlowLayout();
                                flowWidget->setLayout(flowLayout);
                                flowWidget ->setMinimumSize(1200,1200);
                                int n=20;
                                QVector <QPushButton *> buttons(n);
                            
                                for (int ii=0;ii<n;ii++)
                                {
                                    QPushButton * pb = new QPushButton(); // creating buttons
                            
                                       pb->setMinimumSize(200,200);
                                       buttons.push_back(pb); // adding buttons to qvector
                            
                                       flowLayout->addWidget(pb);
                            
                                }
                            
                                QScrollArea *scroll =new QScrollArea();
                            
                                QWidget *WindowWidget= new QWidget(this);
                                WindowWidget->setMinimumSize(1000,1000);
                            
                                QVBoxLayout *WindowLayout = new QVBoxLayout(WindowWidget);
                            
                                QLabel *WindowHeading = new QLabel("Experimenting with Scrollbar in Flow Layout");
                            
                                WindowLayout->addWidget(WindowHeading);
                                WindowLayout->setAlignment(WindowHeading,Qt::AlignLeft);
                                WindowLayout->addWidget(flowWidget);
                            
                                scroll->setWidget(WindowWidget);
                                main->setLayout(WindowLayout);
                                main->setCentralWidget(scroll);
                            
                                main->show();
                                main->setWindowTitle("Main Window");
                            }
                            

                            So this entire widget is to be imported as a widget which fits into the centralWidget of another application.
                            I hope I made my point clearly.

                            Pl45m4P Offline
                            Pl45m4P Offline
                            Pl45m4
                            wrote on 23 Dec 2021, 03:40 last edited by Pl45m4
                            #13

                            @Swati777999 said in showing custom widget inside the mainWindow:

                            So this entire widget is to be imported as a widget which fits into the centralWidget of another application

                            Why you still create a QMainWindow inside your QWidget? You always add one layer which is not necessary.
                            i believe the task as such is not complicated but the amount of cascading widgets and layouts makes it harder to read.

                            If your widget needs to be part of another (already existing) widget or QMainWindow, then you dont need so many widgets and layouts in your widget.
                            Create a widget, assign a main layout, put other content and maybe other layouts with content in, done.
                            Not every app needs a QMainWindow. It's recommended for stand-alone apps, because QMainWindow provides some nice features like for example the integrated statusBar, but in the end, QMainWindow is just a regular QWidget.
                            So it is weird to create a QWidget, then create a parentless QMainWindow inside and then show it.

                            Edit:
                            Also, AFAICS your main mainWindow "lives" in your widget's constructor only... It will get destroyed if you close your other widget, no matter if the mainWindow is still visible or not.


                            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                            ~E. W. Dijkstra

                            S 1 Reply Last reply 23 Dec 2021, 06:20
                            4
                            • Pl45m4P Pl45m4
                              23 Dec 2021, 03:40

                              @Swati777999 said in showing custom widget inside the mainWindow:

                              So this entire widget is to be imported as a widget which fits into the centralWidget of another application

                              Why you still create a QMainWindow inside your QWidget? You always add one layer which is not necessary.
                              i believe the task as such is not complicated but the amount of cascading widgets and layouts makes it harder to read.

                              If your widget needs to be part of another (already existing) widget or QMainWindow, then you dont need so many widgets and layouts in your widget.
                              Create a widget, assign a main layout, put other content and maybe other layouts with content in, done.
                              Not every app needs a QMainWindow. It's recommended for stand-alone apps, because QMainWindow provides some nice features like for example the integrated statusBar, but in the end, QMainWindow is just a regular QWidget.
                              So it is weird to create a QWidget, then create a parentless QMainWindow inside and then show it.

                              Edit:
                              Also, AFAICS your main mainWindow "lives" in your widget's constructor only... It will get destroyed if you close your other widget, no matter if the mainWindow is still visible or not.

                              S Offline
                              S Offline
                              Swati777999
                              wrote on 23 Dec 2021, 06:20 last edited by
                              #14

                              @Pl45m4 said in showing custom widget inside the mainWindow:

                              @Swati777999 said in showing custom widget inside the mainWindow:

                              So this entire widget is to be imported as a widget which fits into the centralWidget of another application

                              Why you still create a QMainWindow inside your QWidget? You always add one layer which is not necessary.
                              i believe the task as such is not complicated but the amount of cascading widgets and layouts makes it harder to read.

                              If you widgets needs to be part of another (already existing) widget or QMainWindow, then you dont need so many widgets and layouts in your widget.
                              Create a widget, assign a main layout, put other content and maybe other layouts with content in, done.
                              Not every app needs a QMainWindow. It's recommended for stand-alone apps, because QMainWindow provides some nice features like for example the integrated statusBar, but in the end, QMainWindow is just a regular QWidget.
                              So it is weird to create a QWidget, then create a parentless QMainWindow inside and then show it.

                              Edit:
                              Also, AFAICS your main mainWindow "lives" in your widget's constructor only... It will get destroyed if you close your other widget, no matter if the mainWindow is still visible or not.

                              My point is there's another application(let's call it MyApp) which has got a design.
                              My app has got some pushbuttons on the left side of the mainwindow. On clicking pushbutton A , I should be getting the result of the scrollareaApp in the central widget of MyApp.

                              What you see below is MyApp and I've written the program above for scrollAreaapp
                              widget_inside_MainWindow.PNG

                              “ In order to be irreplaceable, one must always be different” – Coco Chanel

                              J 1 Reply Last reply 23 Dec 2021, 08:51
                              0
                              • S Swati777999
                                23 Dec 2021, 06:20

                                @Pl45m4 said in showing custom widget inside the mainWindow:

                                @Swati777999 said in showing custom widget inside the mainWindow:

                                So this entire widget is to be imported as a widget which fits into the centralWidget of another application

                                Why you still create a QMainWindow inside your QWidget? You always add one layer which is not necessary.
                                i believe the task as such is not complicated but the amount of cascading widgets and layouts makes it harder to read.

                                If you widgets needs to be part of another (already existing) widget or QMainWindow, then you dont need so many widgets and layouts in your widget.
                                Create a widget, assign a main layout, put other content and maybe other layouts with content in, done.
                                Not every app needs a QMainWindow. It's recommended for stand-alone apps, because QMainWindow provides some nice features like for example the integrated statusBar, but in the end, QMainWindow is just a regular QWidget.
                                So it is weird to create a QWidget, then create a parentless QMainWindow inside and then show it.

                                Edit:
                                Also, AFAICS your main mainWindow "lives" in your widget's constructor only... It will get destroyed if you close your other widget, no matter if the mainWindow is still visible or not.

                                My point is there's another application(let's call it MyApp) which has got a design.
                                My app has got some pushbuttons on the left side of the mainwindow. On clicking pushbutton A , I should be getting the result of the scrollareaApp in the central widget of MyApp.

                                What you see below is MyApp and I've written the program above for scrollAreaapp
                                widget_inside_MainWindow.PNG

                                J Offline
                                J Offline
                                JKSH
                                Moderators
                                wrote on 23 Dec 2021, 08:51 last edited by
                                #15

                                @Swati777999 said in showing custom widget inside the mainWindow:

                                My point is there's another application(let's call it MyApp) which has got a design.

                                I believe you have missed @Pl45m4's point. He is asking you to explain: What do you expect this line to do?: QMainWindow *main = new QMainWindow();

                                So this entire widget is to be imported as a widget which fits into the centralWidget of another application.

                                Well, your ScrollAreaApp should not be the one that tries to import itself to the other application.

                                1. You should put code inside the ScrollAreaApp that determines how it looks, all by itself.
                                2. Your other application should contain the code that "fits [the ScrollAreaApp] to the other application".

                                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                S 1 Reply Last reply 23 Dec 2021, 09:13
                                2
                                • J JKSH
                                  23 Dec 2021, 08:51

                                  @Swati777999 said in showing custom widget inside the mainWindow:

                                  My point is there's another application(let's call it MyApp) which has got a design.

                                  I believe you have missed @Pl45m4's point. He is asking you to explain: What do you expect this line to do?: QMainWindow *main = new QMainWindow();

                                  So this entire widget is to be imported as a widget which fits into the centralWidget of another application.

                                  Well, your ScrollAreaApp should not be the one that tries to import itself to the other application.

                                  1. You should put code inside the ScrollAreaApp that determines how it looks, all by itself.
                                  2. Your other application should contain the code that "fits [the ScrollAreaApp] to the other application".
                                  S Offline
                                  S Offline
                                  Swati777999
                                  wrote on 23 Dec 2021, 09:13 last edited by
                                  #16

                                  @JKSH said in showing custom widget inside the mainWindow:
                                  I believe you have missed @Pl45m4's point. He is asking you to explain: What do you expect this line to do?: QMainWindow *main = new QMainWindow();
                                  Actually, I was trying to write some codes after that [like setting a layout or so]so as to I can add WindowWidget to it, something like that.

                                  Well, your ScrollAreaApp should not be the one that tries to import itself to the other application.

                                  1. You should put code inside the ScrollAreaApp that determines how it looks, all by itself.

                                  2. Your other application should contain the code that "fits [the ScrollAreaApp] to the other application".
                                  Yes, my other application has code for it and I should be writing this code there while trying but I chose to create another project file for experimenting.

                                  “ In order to be irreplaceable, one must always be different” – Coco Chanel

                                  J 1 Reply Last reply 23 Dec 2021, 13:30
                                  0
                                  • S Swati777999
                                    23 Dec 2021, 09:13

                                    @JKSH said in showing custom widget inside the mainWindow:
                                    I believe you have missed @Pl45m4's point. He is asking you to explain: What do you expect this line to do?: QMainWindow *main = new QMainWindow();
                                    Actually, I was trying to write some codes after that [like setting a layout or so]so as to I can add WindowWidget to it, something like that.

                                    Well, your ScrollAreaApp should not be the one that tries to import itself to the other application.

                                    1. You should put code inside the ScrollAreaApp that determines how it looks, all by itself.

                                    2. Your other application should contain the code that "fits [the ScrollAreaApp] to the other application".
                                    Yes, my other application has code for it and I should be writing this code there while trying but I chose to create another project file for experimenting.

                                    J Offline
                                    J Offline
                                    JKSH
                                    Moderators
                                    wrote on 23 Dec 2021, 13:30 last edited by JKSH
                                    #17

                                    @Swati777999 said in showing custom widget inside the mainWindow:

                                    @JKSH said in showing custom widget inside the mainWindow:
                                    I believe you have missed @Pl45m4's point. He is asking you to explain: What do you expect this line to do?: QMainWindow *main = new QMainWindow();
                                    Actually, I was trying to write some codes after that [like setting a layout or so]so as to I can add WindowWidget to it, something like that.

                                    You should add your WindowWidget to your ScrollAreaApp (which is this in your ScrollAreaApp's constructor). You should not be adding it to some extra QMainWindow.

                                    Yes, my other application has code for it and I should be writing this code there while trying but I chose to create another project file for experimenting.

                                    Unfortunately, the way that you're carrying out these experiments is making it harder for you to understand the important basics of widget parent-child relationships and layout management.

                                    In summary: It is completely wrong to create a new QMainWindow() in your custom widget's constructor. Don't do it, ever... not even for experimenting. This is because the ScrollAreaApp's constructor should only contain things that are placed inside the ScrollAreaApp. (Does this make sense? If not, please ask)

                                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                    S 1 Reply Last reply 24 Dec 2021, 01:34
                                    4
                                    • J JKSH
                                      23 Dec 2021, 13:30

                                      @Swati777999 said in showing custom widget inside the mainWindow:

                                      @JKSH said in showing custom widget inside the mainWindow:
                                      I believe you have missed @Pl45m4's point. He is asking you to explain: What do you expect this line to do?: QMainWindow *main = new QMainWindow();
                                      Actually, I was trying to write some codes after that [like setting a layout or so]so as to I can add WindowWidget to it, something like that.

                                      You should add your WindowWidget to your ScrollAreaApp (which is this in your ScrollAreaApp's constructor). You should not be adding it to some extra QMainWindow.

                                      Yes, my other application has code for it and I should be writing this code there while trying but I chose to create another project file for experimenting.

                                      Unfortunately, the way that you're carrying out these experiments is making it harder for you to understand the important basics of widget parent-child relationships and layout management.

                                      In summary: It is completely wrong to create a new QMainWindow() in your custom widget's constructor. Don't do it, ever... not even for experimenting. This is because the ScrollAreaApp's constructor should only contain things that are placed inside the ScrollAreaApp. (Does this make sense? If not, please ask)

                                      S Offline
                                      S Offline
                                      Swati777999
                                      wrote on 24 Dec 2021, 01:34 last edited by
                                      #18

                                      @JKSH said in showing custom widget inside the mainWindow:

                                      @Swati777999 said in showing custom widget inside the mainWindow:

                                      @JKSH said in showing custom widget inside the mainWindow:
                                      I believe you have missed @Pl45m4's point. He is asking you to explain: What do you expect this line to do?: QMainWindow *main = new QMainWindow();
                                      Actually, I was trying to write some codes after that [like setting a layout or so]so as to I can add WindowWidget to it, something like that.

                                      You should add your WindowWidget to your ScrollAreaApp (which is this in your ScrollAreaApp's constructor). You should not be adding it to some extra QMainWindow.

                                      Yes, my other application has code for it and I should be writing this code there while trying but I chose to create another project file for experimenting.

                                      Unfortunately, the way that you're carrying out these experiments is making it harder for you to understand the important basics of widget parent-child relationships and layout management.

                                      In summary: It is completely wrong to create a new QMainWindow() in your custom widget's constructor. Don't do it, ever... not even for experimenting. This is because the ScrollAreaApp's constructor should only contain things that are placed inside the ScrollAreaApp. (Does this make sense? If not, please ask)

                                      I completely understand what you want to convey. So, basically it is preferred to experiment in the same location of the complete application rather than writing a part of code in different project file.

                                      “ In order to be irreplaceable, one must always be different” – Coco Chanel

                                      1 Reply Last reply
                                      0

                                      4/18

                                      22 Dec 2021, 04:22

                                      topic:navigator.unread, 14
                                      • Login

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