简体   繁体   中英

QmodelIndex and header data

I have a 4 tableviews in my ui screen .In my header data function for 4 views , I just need one 1 row name- temperature for view 1 and 3 and four rows with name field 1x,field 4x, field 10x, field 40x respectively. My function is

virtual QVariant headerData(int section,Qt::Orientation orientation,
                int role = Qt::DisplayRole) const
    {
        switch(role)
        {
        case Qt::DisplayRole:
            switch (orientation)
            {
            case Qt::Vertical:
 switch (m_channel)
                    {
                    case 0:
                        switch (section)    // Range
                        {
                        case 0:
                            return "Temperature1";
                        }
                    case 1:
                        switch (section)    // Range
                        {
                        case 0:
                            return "Field 1x range";
                        case 1:
                            return "Field 4x range";
                        case 2:
                            return "Field 10x range";
                        case 3:
                            return "Field 40x range";
                        }
                    case 2:
                        switch (section)    // Range
                        {
                        case 0:
                            return "Temperature2";
                        }
                    case 3:
                        switch (section)    // Range
                        {
                        case 0:
                            return "Field 1x range";
                        case 1:
                            return "Field 4x range";
                        case 2:
                            return "Field 10x range";
                        case 3:
                            return "Field 40x range";
                        }

But, the screen when compiled shows temperature,field 4x, field 10x, field 40x for views 1 and view 3, which I don't wont

Please help

You are missing breaks in your switch statement. For example:

                    switch (m_channel)
                    {
                    case 0:
                        switch (section)    // Range
                        {
                        case 0:
                            return "Temperature1";
                        }
                        break; // <-- You need this.
                    case 1:
                        ...

It's also generally a good idea to provide a default label for switch statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM