PDA

View Full Version : centering combobox text



illuzioner
29th March 2006, 17:17
can entries in a combobox be aligned right or centered?

thanks!
lou

jpn
29th March 2006, 20:53
Seems like it's not so straigthforward as one could imagine.. :)



#include <QtGui>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

// create an example combo
QComboBox combo;
QStringList items;
for (int i = 0; i < 10; ++i)
items << QString::number(i);
combo.addItems(items);

// align the line edit
combo.setEditable(true);
combo.lineEdit()->setAlignment(Qt::AlignHCenter);
// combo.lineEdit()->setReadOnly(true);

// align items
for (int i = 0; i < combo.count(); ++i)
{
combo.setItemData(i, Qt::AlignHCenter, Qt::TextAlignmentRole);
}
combo.show();

a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();
}

illuzioner
30th March 2006, 00:52
thank you very much, just what i need!

thanks!

lou

Bookmarc
30th May 2007, 14:01
Hi,
I have a similar problem, but with a non editable combobox. The list of items is correclty centered, but since there is no lineEdit in a non editable combo box, I can't recenter the currently selected item. Any idea ? Thx !

Bookmarc
30th May 2007, 14:06
Ok, just found the solution. Like jpn posted above, you _must_ have your combobox configured with setEditable(true), but you then you have to disable user input by setting the lineEdit to read-only. Thx jpn !