Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to set up headers orientation and delete check boxes?



  • Hi!

    When the table is displayed on MySQL, headers are appearing on top as well as on the left hand side. How do I remove the left side header and check boxes and leave just the headers on top?

    Github:
    https://github.com/dawids8/Projekt2019

    You can see this issue on the picture:
    0_1568151090371_Screenshot from 2019-09-10 23-23-16.png

    Also code:

    class PatientsViewModel : public QAbstractTableModel
    {
    private:
        const std::vector<Patient>& patients;
        static const int columnsCount = 8;
        const QString columnsNames[columnsCount];
        QVariant indexToData(const QModelIndex &index) const;
    
    public:
        PatientsViewModel(const std::vector<Patient> &patients)
            :patients(patients), columnsNames  {"ID","Name","Last Name","Date of birth","PESEL","Phone Number","E-mail","Gender"}
        {
    
        }
        int rowCount(const QModelIndex &parent) const
        {
            return patients.size();
        }
        int columnCount(const QModelIndex &parent) const
        {
            return 8;
        }
        QVariant headerData(int section, Qt::Orientation orientation, int role) const
        {
            if (role != Qt::DisplayRole)
                return QVariant();
            return columnsNames[section];
        }
        QVariant data(const QModelIndex &index, int role) const
        {
            return indexToData(index);
        }
    };
    
    QVariant PatientsViewModel::indexToData(const QModelIndex &index) const
    {
        Patient p = patients.at(index.row());
        switch (index.column())
        {
        case 0:
            return p.GetPatientId();
        case 1:
            return p.GetName();
        case 2:
            return p.GetLastName();
        case 3:
            return p.GetDateOfBirth();
        case 4:
            return p.GetPesel();
        case 5:
            return p.GetPhoneNumber();
        case 6:
            return p.GetEmail();
        case 7:
            return p.GetGender();
        }
    };
    


  • @vme8
    Not sure if this is the cause/relevant, but: your data() function does not respect the role parameter, and always returns some of your actual data?

    Meanwhile, if you're using a QTableView then setHorizontal/VerticalHeader determines what you get as the horizontal and/or vertical headers.



  • @jonb

    I added the loop if(orientation == Qt::Horizontal) inside headerData function and I fixed the first problem, but still have this check boxes.

        QVariant headerData(int section, Qt::Orientation orientation, int role) const
        {
            if(orientation == Qt::Horizontal)
            {
                if (role != Qt::DisplayRole)
                    return QVariant();
            }
            return columnsNames[section];
        }
    

  • Qt Champions 2019

    @vme8 said in How to set up headers orientation and delete check boxes?:

    but still have this check boxes.

    @JonB already told you what to do:

    "your data() function does not respect the role parameter" - see https://doc.qt.io/qt-5/qabstractitemmodel.html#data


Log in to reply