I'm currently working on a Qt soft and to make things properly I choose to create some "custom" widgets (which are not). My "custom widget" is usually just a simple class with a lot of widgets in it. I did that because I didn't want to store all of this shitty code lines (to init widgets) in my main window.
The problem is : When I init widget in an other class they don't show up in my main window. I give to my other classes a reference to the main window layout (so I can modify it)
Here is an example :
#ifndef VIDEOCONTROLLERS_H
#define VIDEOCONTROLLERS_H
#include <QGroupBox>
#include <QLayout>
#include <QPushButton>
class VideoControllers : QWidget
{
public:
VideoControllers(QVBoxLayout &layout);
private:
QGroupBox _vBox;
QPushButton _vSpeedDownButton;
QPushButton _vPrevFrameButton;
QPushButton _vPlayButton;
QPushButton _vNextFrameButton;
QPushButton _vSpeedUpButton;
signals:
public slots:
};
#endif // VIDEOCONTROLLERS_H
Here is the cpp
#include "VideoControllers.hpp"
VideoControllers::VideoControllers(QVBoxLayout& layout) :
_vSpeedDownButton("s-"),
_vPrevFrameButton("f-"),
_vPlayButton("p"),
_vNextFrameButton("f+"),
_vSpeedUpButton("s+")
{
_vBox.setTitle("Video nav");
QHBoxLayout *globalLayout = new QHBoxLayout;
globalLayout->addWidget(&_vSpeedDownButton);
globalLayout->addWidget(&_vPrevFrameButton);
globalLayout->addWidget(&_vPlayButton);
globalLayout->addWidget(&_vNextFrameButton);
globalLayout->addWidget(&_vSpeedUpButton);
_vBox.setLayout(globalLayout);
layout.addWidget(&_vBox);
}
The weird thing about this is that when I'm not using a member data widget, all works just fine.
QGroupBox *vBox = new vBox("Video nav");
layout.addWidget(vBox);
This code display me the GroupBox but not the elements that are in it while the other isn't.
I'm a newbie at this but I can't see why my objetcs aren't displayed.
Also please tell me if there are others posts like mine, I didn't know what to search ... :/
Sorry for the english
SOLUTION I FOUND:
I was initializing my controller like that :
VideoControllers vControls(*controlPanelLayout);
Using pointer solved the problem:
VideoControllers *vControls = new VideoControllers(*controlPanelLayout);
VideoControllers
that ceased to exist as soon as the scope was exited. Most likely you don't need the explicit heap allocation either, but you're not showing enough code for us to see how to fix that. Most likely you wantVideoControllers
as a member in some class, or perhaps an automatic variable insidemain()
. – Kuba hasn't forgotten Monica