How to set a Header to a QListView
Asked Answered
P

1

6

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.

Penoyer answered 20/11, 2013 at 10:40 Comment(2)
The QListView documentation says : This view does not display horizontal or vertical headers; to display a list of items with a horizontal header, use QTreeView instead.Benedikt
Firstly, if you are using QListView, you'll be using a model. If you are using a model, subclass it just overload the member function headerData ( int, Qt::Orientation, const QVariant &, int ) to give the header.Softy
P
4

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

Perceptible answered 13/2, 2017 at 12:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.