Results 1 to 3 of 3

Thread: QComboBox text misaligned

  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QComboBox text misaligned

    Hi, I just noticed what I consider a sort of bug. I attached an image showing the problem: a label beside a highlighted combobox. As you can see the two text elements aren't aligned, in particular combobox' text is few pixels above the correct position. I'm using qt 4.8.4 on linux.

    Can I fix this?
    Attached Images Attached Images
    Giuseppe CalÃ

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QComboBox text misaligned

    That would only be a bug if anybody promised that the fine details of unrelated widgets would line up. Qt doesn't make any such promise that I am aware of. It's a mostly impossible promise in the face of user selectable themes and arbitrary programmer style sheets.

    You should be able to adjust the location of text inside the label by playing with the top/bottom padding in the style sheet for that widget. You could move the whole label up with bottom margin. You should also check what alignment the model underlying the combo box is returning. It looks like align top left and you might want to try align vcenter left.

  3. #3
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QComboBox text misaligned

    I've solved subclassing QComboBox and reimplementing paintEvent():

    Qt Code:
    1. void ComboBox::paintEvent(QPaintEvent *e)
    2. {
    3. QVariant itemData = this->itemData( this->currentIndex(), Qt::DisplayRole);
    4.  
    5. if(itemData.isValid()) {
    6. QStylePainter p(this);
    7. p.setPen(palette().color(QPalette::Text));
    8.  
    9. initStyleOption(&opt);
    10. p.drawComplexControl(QStyle::CC_ComboBox, opt);
    11.  
    12. QPainter painter(this);
    13. painter.save();
    14. QRect rect = this->rect();
    15. rect.adjust(5,0,-5,0);
    16.  
    17. painter.drawText(rect, Qt::AlignLeft | Qt::AlignVCenter, itemData.toString());
    18. painter.restore();
    19. } else {
    20. QComboBox::paintEvent(e);
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 
    Giuseppe CalÃ

Similar Threads

  1. Qcombobox Binding With text and id ???
    By sai_3289 in forum Qt Programming
    Replies: 4
    Last Post: 18th December 2012, 06:45
  2. Misaligned stack error...
    By mtnbiker66 in forum Qt Programming
    Replies: 3
    Last Post: 4th July 2012, 00:53
  3. QComboBox text alignment
    By mentalmushroom in forum Qt Programming
    Replies: 1
    Last Post: 31st January 2012, 21:33
  4. Get text from QCombobox.
    By dragon in forum Newbie
    Replies: 13
    Last Post: 31st January 2012, 20:50
  5. How to use rich text in a QComboBox?
    By aarunt1 in forum Qt Programming
    Replies: 3
    Last Post: 19th March 2010, 20:20

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.