Class that inherits QWidget to create container of QPushbuttons - doing it correctly?
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.
Code:
#include "stoppagereasons.h"
#include <QPushButton>
#include <QGridLayout>
#include <QScrollBar>
#include <QVBoxLayout>
#include <QScrollArea>
StoppageReasons
::StoppageReasons(QWidget *parent
){
btn->setFixedSize(100,500);
btn2->setFixedSize(100,500);
layout->addWidget(btn);
layout->addWidget(btn2);
widget->setLayout(layout);
scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scroll->setWidgetResizable(false);
scroll->setWidget(widget);
vLayout->addWidget(scroll);
setLayout(vLayout);
}
Re: Class that inherits QWidget to create container of QPushbuttons - doing it correc
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,
_
Re: Class that inherits QWidget to create container of QPushbuttons - doing it correc
Thanks anda_skoa
Would you mind giving me some code? I don't know how to do that.
Re: Class that inherits QWidget to create container of QPushbuttons - doing it correc
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,
_
Re: Class that inherits QWidget to create container of QPushbuttons - doing it correc
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.
Code:
{
btn->setFixedSize(400,350);
btn2->setFixedSize(400,350);
btn3->setFixedSize(400,350);
layout->addWidget(btn);
layout->addWidget(btn2);
layout->addWidget(btn3);
widget->setLayout(layout);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setWidgetResizable(false);
setWidget(widget);
}
Re: Class that inherits QWidget to create container of QPushbuttons - doing it correc
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,
_
Re: Class that inherits QWidget to create container of QPushbuttons - doing it correc
Thanks anda_skoa, I appreciate your input.