{
public:
DropdownOption
(std
::string aText,
QWidget *aParent
);
};
DropdownOption
::DropdownOption(std
::string aText,
QWidget *aParent
):{
this->setFixedHeight(30);
this->setCursor(Qt::PointingHandCursor);
this->setCheckable(false);
this->setStyleSheet("background: rgb(74,89,98); color: black; border-radius: 0px; text-align: left; padding-left: 5px; border-bottom: 1px solid black;");
}
//parent is the widget with all the options which will roll down when the dropdown widget showing the current value is clicked
int width = 120;
parent->setParent(fParent);
//this width will be flexible, here I am setting to 160 for checking purpose
parent->setGeometry(40,40,width,160);
parent->setStyleSheet("border-radius: 5px; background:red;");
actualDropDown->setFixedWidth(width-30);
actualDropDown->setStyleSheet("background: blue;");
actualDropDown->setLayout(layout);
for (int i = 0; i < fValues.size(); i++)
{
DropdownOption *button = new DropdownOption(fValues[i],actualDropDown);
layout->addWidget(button);
}
scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setWidget(actualDropDown);
//20 px less than parent, to ensure that the options don't clip parent's border radius when scrolling up or down past the ends
scroll->setMaximumHeight(140);
scroll->move(0,10);
scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
class DropdownOption: public QPushButton
{
public:
DropdownOption(std::string aText, QWidget *aParent);
};
DropdownOption::DropdownOption(std::string aText, QWidget *aParent):
QPushButton(QString::fromStdString(aText),aParent)
{
this->setFixedHeight(30);
this->setCursor(Qt::PointingHandCursor);
this->setCheckable(false);
this->setStyleSheet("background: rgb(74,89,98); color: black; border-radius: 0px; text-align: left; padding-left: 5px; border-bottom: 1px solid black;");
}
//parent is the widget with all the options which will roll down when the dropdown widget showing the current value is clicked
QFrame *parent = new QFrame(this);
int width = 120;
parent->setParent(fParent);
//this width will be flexible, here I am setting to 160 for checking purpose
parent->setGeometry(40,40,width,160);
parent->setStyleSheet("border-radius: 5px; background:red;");
QFrame *actualDropDown = new QFrame(parent);
actualDropDown->setFixedWidth(width-30);
actualDropDown->setStyleSheet("background: blue;");
QVBoxLayout *layout = new QVBoxLayout();
actualDropDown->setLayout(layout);
for (int i = 0; i < fValues.size(); i++)
{
DropdownOption *button = new DropdownOption(fValues[i],actualDropDown);
layout->addWidget(button);
}
QScrollArea *scroll = new QScrollArea(this);
scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setWidget(actualDropDown);
//20 px less than parent, to ensure that the options don't clip parent's border radius when scrolling up or down past the ends
scroll->setMaximumHeight(140);
scroll->move(0,10);
scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
To copy to clipboard, switch view to plain text mode
Bookmarks