Results 1 to 5 of 5

Thread: centering combobox text

  1. #1
    Join Date
    Feb 2006
    Posts
    29
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Windows

    Default centering combobox text

    can entries in a combobox be aligned right or centered?

    thanks!
    lou

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: centering combobox text

    Seems like it's not so straigthforward as one could imagine..

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6.  
    7. // create an example combo
    8. QComboBox combo;
    9. QStringList items;
    10. for (int i = 0; i < 10; ++i)
    11. items << QString::number(i);
    12. combo.addItems(items);
    13.  
    14. // align the line edit
    15. combo.setEditable(true);
    16. combo.lineEdit()->setAlignment(Qt::AlignHCenter);
    17. // combo.lineEdit()->setReadOnly(true);
    18.  
    19. // align items
    20. for (int i = 0; i < combo.count(); ++i)
    21. {
    22. combo.setItemData(i, Qt::AlignHCenter, Qt::TextAlignmentRole);
    23. }
    24. combo.show();
    25.  
    26. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    27. return a.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 

  3. The following 3 users say thank you to jpn for this useful post:

    illuzioner (30th March 2006), Mesozoic (16th August 2012), user_mail07 (2nd May 2009)

  4. #3
    Join Date
    Feb 2006
    Posts
    29
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: centering combobox text

    thank you very much, just what i need!

    thanks!

    lou
    Last edited by illuzioner; 30th March 2006 at 01:13.

  5. #4
    Join Date
    May 2007
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: centering combobox text

    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 !

  6. #5
    Join Date
    May 2007
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: centering combobox text

    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 !

  7. The following user says thank you to Bookmarc for this useful post:

    Mesozoic (16th August 2012)

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Text block centering in Qt4.4 QTextEdit
    By mla1290 in forum Qt Programming
    Replies: 3
    Last Post: 10th July 2008, 17:46
  3. Match the text beetween two string
    By dreamer in forum Qt Programming
    Replies: 4
    Last Post: 20th May 2008, 14:48
  4. Replies: 8
    Last Post: 15th May 2007, 09:21
  5. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30

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.