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. Adding Custom QWidget to QMainWindow

Adding Custom QWidget to QMainWindow

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 3.8k 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.
  • A Offline
    A Offline
    Alatriste
    wrote on last edited by Alatriste
    #1

    Hello.

    I have QMainWindow based class GWindow with *.ui and also have custom QWidget based class Frame and I want to add Frame object to GWindow.

    GWindow ::GWindow (QWidget *parent)
        : QMainWindow(parent)
    {
        ui.setupUi(this);
        setWindowFlags(Qt::FramelessWindowHint);
      
        FrameObject.show();
        this->setCentralWidget(&FrameObject);
    }
    

    and Frame class . I added QPushButton for Frame UI in Qt Designer

    class Frame : public QWidget
    {
        Q_OBJECT
    
    public:
        Frame (QWidget *parent = 0);
        ~Frame ();
    
    private:
        Ui::Frame ui;
    };
    
    Frame ::Frame (QWidget *parent)
        : QWidget(parent)
    {
      ui.setupUi(this);
    }
    
    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      what is FrameObject?
      Where and how is it defined?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      A 1 Reply Last reply
      0
      • raven-worxR raven-worx

        what is FrameObject?
        Where and how is it defined?

        A Offline
        A Offline
        Alatriste
        wrote on last edited by
        #3

        @raven-worx said:

        is FrameObject?
        Where and how is it defined?

        into GWindow header

        class GWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            GWindow (QWidget *parent = 0);
            ~GWindow ();
        
        private:
            Ui::SandBoxClass ui;
            Frame FrameObject;    
        };
        
        raven-worxR 1 Reply Last reply
        0
        • A Alatriste

          @raven-worx said:

          is FrameObject?
          Where and how is it defined?

          into GWindow header

          class GWindow : public QMainWindow
          {
              Q_OBJECT
          
          public:
              GWindow (QWidget *parent = 0);
              ~GWindow ();
          
          private:
              Ui::SandBoxClass ui;
              Frame FrameObject;    
          };
          
          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @Alatriste
          the issue has to be somewhere in the code you haven't posted yet. :)

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          A 1 Reply Last reply
          0
          • raven-worxR raven-worx

            @Alatriste
            the issue has to be somewhere in the code you haven't posted yet. :)

            A Offline
            A Offline
            Alatriste
            wrote on last edited by Alatriste
            #5

            @raven-worx

            Now WindowTitle instead of Frame and Sandbox intead of GWindow

            class WindowTitle : public QWidget
            {
                Q_OBJECT
            
            public:
                WindowTitle(QWidget *parent = 0);
                ~WindowTitle();
            
            private:
                Ui::WindowTitle ui;
            };
            
            
            WindowTitle::WindowTitle(QWidget *parent)
                : QWidget(parent)
            {
              ui.setupUi(this);
            }
            
            WindowTitle::~WindowTitle()
            {
            
            }
            
            #include <QtWidgets/QMainWindow>
            #include "WindowTitle.h"
            #include <QGridLayout>
            #include "ui_SandBox.h"
            
            class SandBox : public QMainWindow
            {
                Q_OBJECT
            
            public:
                SandBox(QWidget *parent = 0);
                ~SandBox();
            
            private:
                Ui::SandBoxClass ui;
                
            };
            
            #include "SandBox.h"
            
            SandBox::SandBox(QWidget *parent)
                : QMainWindow(parent)
            {
                ui.setupUi(this);
              
                setWindowFlags(Qt::FramelessWindowHint);
            
                QWidget * widget = new QWidget;
                QGridLayout * layout = new QGridLayout;
                WindowTitle * w = new WindowTitle;
                
                w->show();
                layout->addWidget(w,0,0,1,1);
                widget->setLayout(layout);
                setCentralWidget(widget);
            }
            
            
            1 Reply Last reply
            0
            • kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by kshegunov
              #6

              @Alatriste
              Hello,

              What about:

              SandBox::SandBox(QWidget * parent)
                  :  QMainWindow(parent, Qt::FramelessWindowHint)
              {
                  ui.setupUi(this);
              
                  Ui::WindowTitle titleUi;  //< This could be put as a class member as well
                  titleUi.setupUi(centralWidget());
              }
              

              You don't seem to be doing anything special with your WindowTitle class, so no need to derive QWidget only to set up a form.

              Kind regards.

              Read and abide by the Qt Code of Conduct

              A 1 Reply Last reply
              0
              • kshegunovK kshegunov

                @Alatriste
                Hello,

                What about:

                SandBox::SandBox(QWidget * parent)
                    :  QMainWindow(parent, Qt::FramelessWindowHint)
                {
                    ui.setupUi(this);
                
                    Ui::WindowTitle titleUi;  //< This could be put as a class member as well
                    titleUi.setupUi(centralWidget());
                }
                

                You don't seem to be doing anything special with your WindowTitle class, so no need to derive QWidget only to set up a form.

                Kind regards.

                A Offline
                A Offline
                Alatriste
                wrote on last edited by Alatriste
                #7

                @kshegunov

                But If I want use not fixed size widgets? How can I resize Ui::WindowTitle titleUi; custom widget?

                I want to set size dynamically, for example:

                mywidget->setGeometry(0,0,  parentWidget.widht(), 100);
                
                kshegunovK 1 Reply Last reply
                0
                • A Alatriste

                  @kshegunov

                  But If I want use not fixed size widgets? How can I resize Ui::WindowTitle titleUi; custom widget?

                  I want to set size dynamically, for example:

                  mywidget->setGeometry(0,0,  parentWidget.widht(), 100);
                  
                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @kshegunov
                  Hello,
                  Ui::WindowTitle is your form class and not a widget. The widget I'm suggesting you initialize with it is the central widget of the main window, and the central widget will be resized automatically to fit the client area of the window.

                  mywidget->setGeometry(0,0, parentWidget.widht(), 100);

                  You shouldn't be wanting to do that. Just put the appropriate layout and setup your widgets in the designer. If any of them is fixed size put that in the appropriate properties of the object (the right side of the designer, after clicking on an object).

                  Kind regards.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0

                  • Login

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