Hallo,
Gibt es eine Möglichkeit (z.B. mit Style Sheets) den Text in der QComboBox zentriert auszurichten? Also ich meine nicht den Text in der Popup Box, sondern den aktuellen.
Danke!
QComboBox und Alignment
Re: QComboBox und Alignment
Diese Methode hat den Effekt, dass die Combobox trotzdem als eine Editierbare ausschaut.
Es gibt noch folgende Möglichkeit:
Es gibt noch folgende Möglichkeit:
Code: Alles auswählen
class MyCombo : public QComboBox
{
public:
MyCombo(QWidget *parent) : QComboBox(parent), m_arrowWidth(18)
{
}
void MyCombo::paintEvent ( QPaintEvent * event )
{
//return QComboBox::paintEvent(event);
QStylePainter painter(this);
painter.setPen(palette().color(QPalette::Text));
QStyleOptionComboBox opt;
initStyleOption(&opt);
painter.drawComplexControl(QStyle::CC_ComboBox, opt);
opt.direction = Qt::RightToLeft;
if( style() )
{
QRect rc = style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxArrow, this);
m_arrowWidth = rc.width();
}
opt.rect.setRight(opt.rect.right() - m_arrowWidth);
painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
}
private:
int m_arrowWidth;
};
class MyDelegate : public QItemDelegate
{
public:
MyDelegate(QWidget *parent): QItemDelegate(parent)
{}
void paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
QStyleOptionViewItem myOption = option;
myOption.rect.setRight(option.rect.right() - 18); //@TODO breite des pfeiles
QItemDelegate::paint(painter, myOption, index);
}
};
MyCombo *mComboBox3 = new MyCombo(dlg1);
mComboBox3->setItemDelegate(new MyDelegate(mComboBox3));
mComboBox3->addItem("1.000");
mComboBox3->addItem("2.000");
mComboBox3->addItem("345.000");
mComboBox3->addItem("9.000");
for(int i = 0; i < mComboBox3->count(); i++)
mComboBox3->setItemData(i, Qt::AlignRight, Qt::TextAlignmentRole);