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. QWidget empty despite having children

QWidget empty despite having children

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 344 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.
  • D Offline
    D Offline
    DenizCaglar
    wrote on last edited by
    #1

    I'm trying to implement a side menu bar with QWidgets and I'm having trouble seeing my independent widget have any elements in it.

    I've create a custom class called SideBar which has the following header and source files.
    sidebarh.h:

    #ifndef SIDEBAR_H
    #define SIDEBAR_H
    
    #include <map>
    
    #include <QWidget>
    #include <QVBoxLayout>
    #include <QMenu>
    #include <QString>
    
    class SideBar : public QWidget
    {
        Q_OBJECT
    public:
        explicit SideBar(QWidget *parent = nullptr);
        ~SideBar();
    
    private:
        void setupSubmenus();
    
        QVBoxLayout *sidebar_layout;
        std::map<QString,QMenu*> sidebar_submenus;
    };
    
    Q_DECLARE_METATYPE(SideBar);
    
    #endif // SIDEBAR_H
    
    

    sidebar.cpp

    #include "sidebar.h"
    
    SideBar::SideBar(QWidget *parent)
        : QWidget{parent}
        , sidebar_layout(new QVBoxLayout(this))
    {
        qRegisterMetaType<SideBar>();
    
        hide();
    
        setupSubmenus();
    
        // add submenus to layout
        for (auto &submenu_pair : sidebar_submenus){
            //QMenu * submenu = submenu_pair.second;
            sidebar_layout->addWidget(submenu_pair.second);
        }
    
    }
    
    SideBar::~SideBar(){
        delete sidebar_layout;
    
        // delete each submenu
        for (auto &submenu_pair : sidebar_submenus) {
            delete submenu_pair.second;
        }
    }
    
    void SideBar::setupSubmenus(){
    
        // inserting the settings submenu
        QMenu *settings_submenu = new QMenu(this);
        QIcon settings_icon(":/icons/settings_menu_icon.svg");
        settings_submenu->setIcon(settings_icon);
        settings_submenu->setTitle(QString("Settings"));
    
        settings_submenu->addAction(QString("General"));
        settings_submenu->addAction(QString("Patients"));
        settings_submenu->addAction(QString("Billing"));
        sidebar_submenus["Settings"] = settings_submenu;
    
        return;
    }
    

    This sidebar gets shown as an independent window when a button is clicked on my tool bar with the following slot:

    void HamburgerMenu::hamburger_clicked(bool checked) {
    
        if (side_bar->isVisible())
            side_bar->hide();
        else {
            side_bar->show();
        }
        return;
    }
    
    

    However, when the second window pops up it always is just an empty white screen. I don't understand why this could happen since I initialize my QVBoxLayout properly and add widgets to it. These added widgets have their own text and icons but I only see a white screen.

    I would appreciate any idea as to what might be happening here. The next thing that I'm planning on trying out is just adding a huge image widget to see if that shows up. Thank you in advance

    Christian EhrlicherC M 2 Replies Last reply
    0
    • D DenizCaglar

      I'm trying to implement a side menu bar with QWidgets and I'm having trouble seeing my independent widget have any elements in it.

      I've create a custom class called SideBar which has the following header and source files.
      sidebarh.h:

      #ifndef SIDEBAR_H
      #define SIDEBAR_H
      
      #include <map>
      
      #include <QWidget>
      #include <QVBoxLayout>
      #include <QMenu>
      #include <QString>
      
      class SideBar : public QWidget
      {
          Q_OBJECT
      public:
          explicit SideBar(QWidget *parent = nullptr);
          ~SideBar();
      
      private:
          void setupSubmenus();
      
          QVBoxLayout *sidebar_layout;
          std::map<QString,QMenu*> sidebar_submenus;
      };
      
      Q_DECLARE_METATYPE(SideBar);
      
      #endif // SIDEBAR_H
      
      

      sidebar.cpp

      #include "sidebar.h"
      
      SideBar::SideBar(QWidget *parent)
          : QWidget{parent}
          , sidebar_layout(new QVBoxLayout(this))
      {
          qRegisterMetaType<SideBar>();
      
          hide();
      
          setupSubmenus();
      
          // add submenus to layout
          for (auto &submenu_pair : sidebar_submenus){
              //QMenu * submenu = submenu_pair.second;
              sidebar_layout->addWidget(submenu_pair.second);
          }
      
      }
      
      SideBar::~SideBar(){
          delete sidebar_layout;
      
          // delete each submenu
          for (auto &submenu_pair : sidebar_submenus) {
              delete submenu_pair.second;
          }
      }
      
      void SideBar::setupSubmenus(){
      
          // inserting the settings submenu
          QMenu *settings_submenu = new QMenu(this);
          QIcon settings_icon(":/icons/settings_menu_icon.svg");
          settings_submenu->setIcon(settings_icon);
          settings_submenu->setTitle(QString("Settings"));
      
          settings_submenu->addAction(QString("General"));
          settings_submenu->addAction(QString("Patients"));
          settings_submenu->addAction(QString("Billing"));
          sidebar_submenus["Settings"] = settings_submenu;
      
          return;
      }
      

      This sidebar gets shown as an independent window when a button is clicked on my tool bar with the following slot:

      void HamburgerMenu::hamburger_clicked(bool checked) {
      
          if (side_bar->isVisible())
              side_bar->hide();
          else {
              side_bar->show();
          }
          return;
      }
      
      

      However, when the second window pops up it always is just an empty white screen. I don't understand why this could happen since I initialize my QVBoxLayout properly and add widgets to it. These added widgets have their own text and icons but I only see a white screen.

      I would appreciate any idea as to what might be happening here. The next thing that I'm planning on trying out is just adding a huge image widget to see if that shows up. Thank you in advance

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Don't call hide() in the ctor. Also there is no need to delete the widgets in the dtor - it's done automatically.

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

      D 1 Reply Last reply
      2
      • D DenizCaglar

        I'm trying to implement a side menu bar with QWidgets and I'm having trouble seeing my independent widget have any elements in it.

        I've create a custom class called SideBar which has the following header and source files.
        sidebarh.h:

        #ifndef SIDEBAR_H
        #define SIDEBAR_H
        
        #include <map>
        
        #include <QWidget>
        #include <QVBoxLayout>
        #include <QMenu>
        #include <QString>
        
        class SideBar : public QWidget
        {
            Q_OBJECT
        public:
            explicit SideBar(QWidget *parent = nullptr);
            ~SideBar();
        
        private:
            void setupSubmenus();
        
            QVBoxLayout *sidebar_layout;
            std::map<QString,QMenu*> sidebar_submenus;
        };
        
        Q_DECLARE_METATYPE(SideBar);
        
        #endif // SIDEBAR_H
        
        

        sidebar.cpp

        #include "sidebar.h"
        
        SideBar::SideBar(QWidget *parent)
            : QWidget{parent}
            , sidebar_layout(new QVBoxLayout(this))
        {
            qRegisterMetaType<SideBar>();
        
            hide();
        
            setupSubmenus();
        
            // add submenus to layout
            for (auto &submenu_pair : sidebar_submenus){
                //QMenu * submenu = submenu_pair.second;
                sidebar_layout->addWidget(submenu_pair.second);
            }
        
        }
        
        SideBar::~SideBar(){
            delete sidebar_layout;
        
            // delete each submenu
            for (auto &submenu_pair : sidebar_submenus) {
                delete submenu_pair.second;
            }
        }
        
        void SideBar::setupSubmenus(){
        
            // inserting the settings submenu
            QMenu *settings_submenu = new QMenu(this);
            QIcon settings_icon(":/icons/settings_menu_icon.svg");
            settings_submenu->setIcon(settings_icon);
            settings_submenu->setTitle(QString("Settings"));
        
            settings_submenu->addAction(QString("General"));
            settings_submenu->addAction(QString("Patients"));
            settings_submenu->addAction(QString("Billing"));
            sidebar_submenus["Settings"] = settings_submenu;
        
            return;
        }
        

        This sidebar gets shown as an independent window when a button is clicked on my tool bar with the following slot:

        void HamburgerMenu::hamburger_clicked(bool checked) {
        
            if (side_bar->isVisible())
                side_bar->hide();
            else {
                side_bar->show();
            }
            return;
        }
        
        

        However, when the second window pops up it always is just an empty white screen. I don't understand why this could happen since I initialize my QVBoxLayout properly and add widgets to it. These added widgets have their own text and icons but I only see a white screen.

        I would appreciate any idea as to what might be happening here. The next thing that I'm planning on trying out is just adding a huge image widget to see if that shows up. Thank you in advance

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

        @DenizCaglar said in QWidget empty despite having children:

        However, when the second window pops up it always is just an empty white screen

        I think you need to call show() for the menu each time as well.

        1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          Don't call hide() in the ctor. Also there is no need to delete the widgets in the dtor - it's done automatically.

          D Offline
          D Offline
          DenizCaglar
          wrote on last edited by
          #4

          @Christian-Ehrlicher Thank you for your answer.

          Does this also cover theQMenu object?

          I've seen some parts of the Qt documentation mention that particular functions take ownership of their pointer arguments. I'm assuming not having to delete allocated objects has to do with this.

          Christian EhrlicherC 1 Reply Last reply
          0
          • D DenizCaglar

            @Christian-Ehrlicher Thank you for your answer.

            Does this also cover theQMenu object?

            I've seen some parts of the Qt documentation mention that particular functions take ownership of their pointer arguments. I'm assuming not having to delete allocated objects has to do with this.

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @DenizCaglar said in QWidget empty despite having children:

            I've seen some parts of the Qt documentation mention that particular functions take ownership of their pointer arguments

            This has nothing to do with my comment.

            I was refering to the parent-child relationship of QObjects

            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
            2

            • Login

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