QComboBox customize items
I want to achieve the same effect as designed by our artist
From the help of some good souls in here, I know how to do this in QML, but I overlooked the application and it doesnt uses QML but straight up QComboBox widget in Qt designer styled in style sheet.
How can I achieve such look for each item in combobox?
You may wanna look into Qt Style Sheets Examples. The thing with QComboBox
is that you have to separately style its contents. The contents are displayed in a QListView
. You can also style the combo box items one by one.
Here's a snippet of code that I did in Qt Creator and was able to have some success. You have to dig more into the documentation to achieve your desired result. It's very much possible that you may not have the same flexibility as you have in QML
. You may be able to achieve a better result if you subclass QComboBox
and override its paintEvent
, but you will have to do everything programmatically.
With stylesheets you will get results faster.
Here goes the snippet:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->comboBox->setStyleSheet(ui->comboBox->styleSheet() +
"QListView {"
"brackground: transparent;"
"border: 3px solid black;"
"border-radius: 18px;"
"padding: 1px 1px 1px 1px;}");
}
I just started a Qt Widgets application in Qt Creator and then using the Qt Designer drew a combo box and it was named comboBox
.
I am also including the screenshot from the app. Hope this was helpful.