QWidget empty despite having children
-
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
-
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
Don't call hide() in the ctor. Also there is no need to delete the widgets in the dtor - it's done automatically.
-
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
@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.
-
Don't call hide() in the ctor. Also there is no need to delete the widgets in the dtor - it's done automatically.
@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-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.
@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