PDA

View Full Version : Class that inherits QWidget to create container of QPushbuttons - doing it correctly?



ShamusVW
4th May 2016, 08:16
I have created a class that I am calling from another class that deals with the user interface for my program.
The called class sets up a scrollable area that contains 2 QPushbuttons at this point to test it, and it works fine, however I am uneasy that what I have done is not quite correct, and I was wondering if someone could give me feedback on if it is correct, and how to improve it.
At this point I have made the 2 buttons very large (in height) only so that I can see that the effect of the scrolling action is working, which it is.
My concern is that I get the feeling that within the class, I am creating a widget again, even though it inherits a QWidget already, and so this shouldn't be needed.
Note that my code is based on code that I found online, and modified.


#include "stoppagereasons.h"

#include <QPushButton>
#include <QGridLayout>
#include <QScrollBar>
#include <QVBoxLayout>
#include <QScrollArea>

StoppageReasons::StoppageReasons(QWidget *parent)
: QWidget(parent)
{
QWidget *widget = new QWidget;
QVBoxLayout *layout = new QVBoxLayout();
QPushButton *btn = new QPushButton("test");
btn->setFixedSize(100,500);
QPushButton *btn2 = new QPushButton("test2");
btn2->setFixedSize(100,500);

layout->addWidget(btn);
layout->addWidget(btn2);
widget->setLayout(layout);

QScrollArea *scroll = new QScrollArea;
scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
scroll->setWidgetResizable(false);
scroll->setWidget(widget);

QVBoxLayout *vLayout = new QVBoxLayout();
vLayout->addWidget(scroll);
setLayout(vLayout);
}

anda_skoa
4th May 2016, 09:09
That looks ok.

Your widget contains a QScrollArea and it needs a widget as its content.

The only thing you could change is to make your widget a subclass of QScrollArea instead of having a single QScrollArea child

Cheers,
_

ShamusVW
4th May 2016, 10:29
Thanks anda_skoa

Would you mind giving me some code? I don't know how to do that.

anda_skoa
4th May 2016, 13:28
I am afraid I don't understand.

You obviously know how to create a subclass, so instead of deriving from QWidget you just derive from QScrollArea.

Cheers,
_

ShamusVW
4th May 2016, 14:50
Sorry, I didn't understand you correctly, so this is what I have done now.
Is this an improvement over the original code? Any other improvements I could do?
The code is definitely shorter, so I like that.


StoppageReasons::StoppageReasons(QScrollArea *parent)
: QScrollArea(parent)
{
QPushButton *btn = new QPushButton("test");
btn->setFixedSize(400,350);
QPushButton *btn2 = new QPushButton("test2");
btn2->setFixedSize(400,350);
QPushButton *btn3 = new QPushButton("test3");
btn3->setFixedSize(400,350);

QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(btn);
layout->addWidget(btn2);
layout->addWidget(btn3);

QWidget *widget = new QWidget;
widget->setLayout(layout);

setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
setWidgetResizable(false);
setWidget(widget);
}

anda_skoa
4th May 2016, 16:18
Yes, that's what I meant.

Looks good to me, the rest is a matter of personal preference.

E.g. I would first create the "widget" object, then its layout and then add the children as I go along.

Cheers,
_

ShamusVW
5th May 2016, 05:57
Thanks anda_skoa, I appreciate your input.