2
votes

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);
1
I think u should propably use a subclass of QMainWindow as a base class and init all widgets with <code>This</code> as parent class and do all the init there so when u <code>.show()</code> this all the other widgets show with itMekacher Anis
I was doing this but this is doing some shitty code having all the modules initializations in the same subclass. It is way too big and not really maintainable. I found the solution to my problem.Nico
so what was the problem ? also I've learned from the Qt documentation to do it that wayMekacher Anis
I used constructor initialization which was a bad idea I guess ..Nico
"I used constructor initialization which was a bad idea I guess " There is no such thing. What you did was to create a short-lived automatic storage scope instance of 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 want VideoControllers as a member in some class, or perhaps an automatic variable inside main().Kuba hasn't forgotten Monica

1 Answers

1
votes

I think you need to pass a parent window to QWidget constructor. If you want your widget to show up in the MainWindow try:

VideoControllers(QWidget *parent, QVBoxLayout &layout) : QWidget(parent)
{
...
}