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.

New Moon
  • 787
  • 6
  • 21
  • 35
  • 7
    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.* – Dimitry Ernot Nov 20 '13 at 10:45
  • 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. – Marcus Mar 24 '16 at 17:11

1 Answers1

3

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

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416