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
Forum Updated to NodeBB v4.3 + New Features

showing custom widget inside the mainWindow

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 7 Posters 3.1k 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 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
    • Swati777999S Swati777999

      @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)?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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

      Swati777999S 1 Reply Last reply
      1
      • Swati777999S Swati777999

        @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)?

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on 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

        Swati777999S 1 Reply Last reply
        1
        • JKSHJ JKSH

          @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?

          Swati777999S Offline
          Swati777999S Offline
          Swati777999
          wrote on 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
          0
          • jsulmJ jsulm

            @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...

            Swati777999S Offline
            Swati777999S Offline
            Swati777999
            wrote on 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
            • Swati777999S Swati777999

              @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 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

              Swati777999S 1 Reply Last reply
              4
              • Pl45m4P Pl45m4

                @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.

                Swati777999S Offline
                Swati777999S Offline
                Swati777999
                wrote on 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

                JKSHJ 1 Reply Last reply
                0
                • Swati777999S Swati777999

                  @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

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on 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

                  Swati777999S 1 Reply Last reply
                  2
                  • JKSHJ JKSH

                    @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".
                    Swati777999S Offline
                    Swati777999S Offline
                    Swati777999
                    wrote on 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

                    JKSHJ 1 Reply Last reply
                    0
                    • Swati777999S Swati777999

                      @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.

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on 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

                      Swati777999S 1 Reply Last reply
                      4
                      • JKSHJ JKSH

                        @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)

                        Swati777999S Offline
                        Swati777999S Offline
                        Swati777999
                        wrote on 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

                        • Login

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