PDA

View Full Version : QCombBox with right-justified text



wssddc
21st September 2010, 05:08
I'd like to make QComboBoxes with right-justified text. The following does most of what I want to an existing box

#include <QComboBox>
#include <QLineEdit>
void rightJustCombo(QComboBox *combo)
{
combo->setEditable(true);
combo->lineEdit()->setAlignment(Qt::AlignRight);
for (int i=0; i<combo->count(); i++)
combo->setItemData(i, Qt::AlignRight, Qt::TextAlignmentRole);
combo->lineEdit()->setReadOnly(true);
}

What's not quite right is that the pulldown text is farther right than the lineEdit text by the width of the pulldown arrow. Is there a way to line up the text?

tbscope
21st September 2010, 05:17
I guess the easiest way is to use a stylesheet:
http://doc.qt.nokia.com/4.6/stylesheet-examples.html#customizing-qcombobox

wssddc
21st September 2010, 05:38
I agree that a stylesheet seems like the way it should be done, but I haven't found the right parameters. For example

combo->setStyleSheet(QString::fromAscii("QComboBox:drop-down {padding-right: 100px;}"));
doesn't change the display at all.

Lykurg
21st September 2010, 06:20
I don't know about style sheet, but you can use a custom delegate which align the items with a padding (get the needed size from the current style). Or you can set a left to right language layout on the box, thus the down arrow should go on the left side (not tested).

wssddc
21st September 2010, 06:35
Changing the layout direction does move the down arrow to the left, but that's not where I expect it to be. I will look at the custom delegate solution tomorrow.

wssddc
23rd September 2010, 03:28
combo->setStyleSheet(QString::fromAscii("QComboBox QListView {padding-right: 20px;}"));
gives is close to what I want. (QListView could be QAbstractItemView and padding could be margin with the same visual effect.) The 20px width should be derived from combo somehow, not pulled from thin air. It would also look better if the pulldown did not extend under the arrow.

tbscope
23rd September 2010, 05:25
You can always create your own style of course.

Lykurg
23rd September 2010, 06:29
combo->setStyleSheet(QString::fromAscii("QComboBox QListView {padding-right: 20px;}"));
Better use QLatin1String since it is faster.


The 20px width should be derived from combo somehow, not pulled from thin air
As told, look at QStyle. QStyle::subControlRect()

kreno
21st October 2010, 21:17
This is more simple than you think. My solution is in Python and PyQt, but you should be able to see the similarities.



comboBox = QComboBox()
view = comboBox.view()
view.setLayoutDirection(Qt.RightToLeft)

wssddc
22nd October 2010, 04:44
Setting the layout direction to right-to-left moves the text in the pulldown further to the right than I want. What I would like is for the pulldown to not extend under the arrow. What's happened since my original posting is I said I would release my application when Qt 4.7 was ready. Well 4.7 was released a few days after my post, and I'm still not quite ready to release. I've stopped working on this relatively minor cosmetic issue for, as Qt bug reports often say, some future release of my program.