×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    272

    Question How to align text in QComboBox and change its title?

    In QComboBox, how can I align the text to centre? Currently (in Maemo 5 at least) the text is aligned left.

    Also (again in Maemo 5) there is this "title" when it's open. How can I change that? Currently it's showing the app name only.

    Cheers

  2. #2
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: How to align text in QComboBox and change its title?

    You can do something like this:
    Code:
    #include <QApplication>
    #include <QMainWindow>
    #include <QComboBox>
    #include <QAbstractItemView>
    #include <QDebug>
    
    int main(int argc, char* argv[])
    {
        QApplication app(argc,argv);
    
        QComboBox *combo = new QComboBox;
        combo->addItem("item1");
        combo->addItem("item2");
        combo->addItem("item3");
        combo->addItem("item4");
        QAbstractItemView *view = combo->view();
        QAbstractItemModel *model = view->model();
        model->setData(model->index(0, 0), QVariant(Qt::AlignCenter), Qt::TextAlignmentRole);
    
        QMainWindow w;
        w.setCentralWidget(combo);
        w.show();
    
        return app.exec();
    }

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    22

    Re: How to align text in QComboBox and change its title?

    Quote Originally Posted by divanov View Post
    You can do something like this:
    Code:
    #include <QApplication>
    #include <QMainWindow>
    #include <QComboBox>
    #include <QAbstractItemView>
    #include <QDebug>
    
    int main(int argc, char* argv[])
    {
        QApplication app(argc,argv);
    
        QComboBox *combo = new QComboBox;
        combo->addItem("item1");
        combo->addItem("item2");
        combo->addItem("item3");
        combo->addItem("item4");
        QAbstractItemView *view = combo->view();
        QAbstractItemModel *model = view->model();
        model->setData(model->index(0, 0), QVariant(Qt::AlignCenter), Qt::TextAlignmentRole);
    
        QMainWindow w;
        w.setCentralWidget(combo);
        w.show();
    
        return app.exec();
    }
    Hi Daniil,

    I tried your code in Qt4.7, and it seems not to work . The alignment is still "left" for all items in combobox. Actually I'm meeting the same problem when I try to modify the text alignment of combobox on N900 with maemo5 firmware 1.3. So do you have any other idea for solving this problem?

  4. #4
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: How to align text in QComboBox and change its title?

    It works perfectly for me. Note that PR1.3 contains Qt 4.7, while Nokia Qt SDK 1.0.1 still has Qt 4.6. This mismatch can cause any deviations.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×