PDA

View Full Version : QComboBox text alignment via CSS, and QComboBox ListItem Height



papercut87
7th May 2015, 11:32
Hi All,

I've two little trouble with my QComboBox that make me crazy.
The first one, is that i would like to right align the selected item text via CSS inside QComboBox.(like the behavior of the QSpinBox).
11166
I tried, more or less, everything inside my css but seems impossible to do. :(
This is my current CSS:


QComboBox {
border: 1px solid darkgray;
border-radius: 0px;
padding-right: 20px;
min-width: 6em;
color: black;
}

QComboBox:!editable, QComboBox::drop-down:editable {
selection-color: rgb(0, 150, 200);
background: white;
}

QComboBox::drop-down {
subcontrol-origin: padding;
subcontrol-position: top right;
width: 15px;
height: 30px;
border-width: 0px;
border-left-width: 1px;
border-left-color: darkgray;
border-left-style: solid; /* just a single line */
border-top-right-radius: 0px; /* same radius as the QComboBox */
border-bottom-right-radius: 0px;
}

QComboBox::down-arrow {
image: url(:/images/icons/caret-down_262626_16.png);
}

QComboBox::drop-down:on {
top: 0px;
left: 0px;
background: rgb(0, 150, 200);
}

QComboBox QAbstractItemView{
/* background-color: rgb(0, 150, 200);
border-radius: 0px;*/
selection-background-color:white ;
selection-color:rgb(0,150,200) ;
}


The second question is regarding the items in the QComboBox list. Is possible, still through CSS, to set height of those items?
More in detail, i would like to increase the space between the items. How you can see from the picture below, the height is the same of each item.
11167

Thanks a lot for any suggestion.

papercut87
20th May 2015, 12:21
Ok, i found an workaround(unfortunately seems there is no way to do this by css)... and it's not so bad.

I created a custom class that inherited from QComboBox.
Then in the constructor i set the QComboBox editable and put the related QLineEdit disabled.
Setting the QLineEdit property readOnly = true and the correct right alignment(Qt::AlignRight) did the trick.

Below is attached the code. (For someone, like me, that became crazy for this stupid behavior).


#include "customcombobox.h"
#include <QLineEdit>

CustomComboBox::CustomComboBox(QWidget *parent) : QComboBox(parent)
{
this->setEditable(true);
this->lineEdit()->setDisabled(true);
this->lineEdit()->setReadOnly(true);
this->lineEdit()->setAlignment(Qt::AlignRight);
}