Results 1 to 10 of 10

Thread: QCombBox with right-justified text

  1. #1
    Join Date
    Oct 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default QCombBox with right-justified text

    I'd like to make QComboBoxes with right-justified text. The following does most of what I want to an existing box
    Qt Code:
    1. #include <QComboBox>
    2. #include <QLineEdit>
    3. void rightJustCombo(QComboBox *combo)
    4. {
    5. combo->setEditable(true);
    6. combo->lineEdit()->setAlignment(Qt::AlignRight);
    7. for (int i=0; i<combo->count(); i++)
    8. combo->setItemData(i, Qt::AlignRight, Qt::TextAlignmentRole);
    9. combo->lineEdit()->setReadOnly(true);
    10. }
    To copy to clipboard, switch view to plain text mode 
    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QCombBox with right-justified text

    I guess the easiest way is to use a stylesheet:
    http://doc.qt.nokia.com/4.6/styleshe...zing-qcombobox

  3. #3
    Join Date
    Oct 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QCombBox with right-justified text

    I agree that a stylesheet seems like the way it should be done, but I haven't found the right parameters. For example
    Qt Code:
    1. combo->setStyleSheet(QString::fromAscii("QComboBox:drop-down {padding-right: 100px;}"));
    To copy to clipboard, switch view to plain text mode 
    doesn't change the display at all.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QCombBox with right-justified text

    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).

  5. #5
    Join Date
    Oct 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QCombBox with right-justified text

    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.

  6. #6
    Join Date
    Oct 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QCombBox with right-justified text

    Qt Code:
    1. combo->setStyleSheet(QString::fromAscii("QComboBox QListView {padding-right: 20px;}"));
    To copy to clipboard, switch view to plain text mode 
    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QCombBox with right-justified text

    You can always create your own style of course.

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QCombBox with right-justified text

    Quote Originally Posted by wssddc View Post
    Qt Code:
    1. combo->setStyleSheet(QString::fromAscii("QComboBox QListView {padding-right: 20px;}"));
    To copy to clipboard, switch view to plain text mode 
    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()

  9. #9
    Join Date
    Oct 2010
    Posts
    1
    Qt products
    Platforms
    Unix/X11

    Default Re: QCombBox with right-justified text

    This is more simple than you think. My solution is in Python and PyQt, but you should be able to see the similarities.

    Qt Code:
    1. comboBox = QComboBox()
    2. view = comboBox.view()
    3. view.setLayoutDirection(Qt.RightToLeft)
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Oct 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QCombBox with right-justified text

    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.

Similar Threads

  1. Replies: 0
    Last Post: 2nd August 2010, 09:37
  2. Replies: 3
    Last Post: 3rd May 2009, 08:58
  3. Replies: 1
    Last Post: 3rd September 2008, 14:16
  4. Qt class showing text area/text edit
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 21st August 2008, 12:15
  5. QTextEdit + paste rich text as plain text only
    By Yong in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 16:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.