How to set a Header to a QListView

New Moon picture New Moon · Nov 20, 2013 · Viewed 8.4k times · Source

I am having a QListView which has 5 items in it. say(America, Canada, Denmark, Egypt, Finland) in my QListView. Now i want to set a common Header for all this items as COUNTRIES. How can i set a Header to a QListView. Please Help.

Answer

john whitegun picture john whitegun · Feb 13, 2017

Well, you can not directly but, you can do this.

    mHb = new QHBoxLayout;
    {

        QVBoxLayout *tInnerVB = new QVBoxLayout;
        {
            QLabel *tHeader = new QLabel("Team List");
            tHeader->setFont(QFont("FontAwesome"));
            tInnerVB->addWidget(tHeader);

            mTeamViewModel->setStringList(BDatabase::instance()->getTeamList());
            mLeTeamList->setModel(mTeamViewModel);
            tInnerVB->addWidget(mLeTeamList);

        }
        mHb->addLayout(tInnerVB);

        tInnerVB = new QVBoxLayout;
        {
            QLabel *tHeader = new QLabel("Team Members");
            tHeader->setFont(QFont("FontAwesome"));
            tInnerVB->addWidget(tHeader);

            mMembersViewModel->setStringList(QStringList());
            mLeTeamMembers->setModel(mMembersViewModel);
            tInnerVB->addWidget(mLeTeamMembers);

        }
        mHb->addLayout(tInnerVB);


    }

The output would be like:

enter image description here

Moreover, you can change orientation, alignment and other things by the label. Good coding :)