Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Help with QTreeView
Forum Updated to NodeBB v4.3 + New Features

Help with QTreeView

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 2 Posters 443 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arturio955
    wrote on last edited by arturio955
    #1

    Can somebody please help. I have a problem opening the department tab in 1st company after adding employee in department, its crashing. It gives sigsegv signal.

    CompanyModel::CompanyModel(QObject *parent)
        : QAbstractItemModel(parent){}
    
    QModelIndex CompanyModel::index(int row, int column, const QModelIndex &parent) const {
        if (!hasIndex(row, column, parent))
            return QModelIndex();
    
        if (!parent.isValid()) {
            return createIndex(row, column, quintptr(-1));
        } else if (parent.internalId() == quintptr(-1)) {
            return createIndex(row, column, parent.row());
        } else {
            return createIndex(row, column, parent.internalId() * 1000 + parent.row());
        }
    }
    
    QModelIndex CompanyModel::parent(const QModelIndex &index) const {
        if (!index.isValid())
            return QModelIndex();
    
        quintptr internalId = index.internalId();
        if (internalId == quintptr(-1))
            return QModelIndex();
    
        if (internalId < 1000) {
            return createIndex(internalId, 0, quintptr(-1));
        } else {
            int companyIndex = internalId / 1000;
            int departmentIndex = internalId % 1000;
            return createIndex(departmentIndex, 0, companyIndex);
        }
    }
    
    int CompanyModel::rowCount(const QModelIndex &parent) const {
        if (!parent.isValid())
            return companies.size();
    
        if (parent.internalId() == -1) {
            return companies.at(parent.row()).departments.size();
        } else if (parent.internalId() >= 0 && parent.internalId() < companies.size()) {
            return companies.at(parent.internalId()).departments.at(parent.row()).employees.size();
        }
    
        return 0;
    }
    int CompanyModel::columnCount(const QModelIndex &parent) const {
        Q_UNUSED(parent);
        return 4;
    }
    
    QVariant CompanyModel::data(const QModelIndex &index, int role) const {
        if (!index.isValid())
            return QVariant();
    
        if (index.internalId() == quintptr(-1)) {
            const Company &company = companies.at(index.row());
            if (role == Qt::DisplayRole && index.column() == 0)
                return company.name;
            else if (role == Qt::DecorationRole && index.column() == 0)
                return company.logo.isNull() ? StaticIcons::companyIcon() : company.logo;
        } else if (index.internalId() < companies.size()) {
            const Department &department = companies.at(index.internalId()).departments.at(index.row());
            if (role == Qt::DisplayRole && index.column() == 0)
                return department.name;
            else if (role == Qt::DecorationRole && index.column() == 0)
                return StaticIcons::departmentIcon();
        } else {
            int companyIndex = index.internalId() / 1000;
            int departmentIndex = index.internalId() % 1000;
            const Employee &employee = companies.at(companyIndex).departments.at(departmentIndex).employees.at(index.row());
            if (role == Qt::DisplayRole) {
                switch (index.column()) {
                case 0: return employee.name;
                case 1: return employee.position;
                case 2: return employee.gender == Gender::Male ? "Мужской" : "Женский";
                }
            } else if (role == Qt::DecorationRole && index.column() == 0) {
                return employee.photo.isNull() ? StaticIcons::employeeIcon() : employee.photo;
            }
        }
        return QVariant();
    }
    
    QVariant CompanyModel::headerData(int section, Qt::Orientation orientation, int role) const {
        if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
            switch (section) {
            case 0:
                return QString("Name");
            case 1:
                return QString("Position");
            case 2:
                return QString("Gender");
            }
        }
        return QVariant();
    }
    
    bool CompanyModel::setData(const QModelIndex &index, const QVariant &value, int role) {
        if (!index.isValid() || role != Qt::EditRole) {
            return false;
        }
    
        if (index.internalId() == quintptr(-1)) {
            Company &company = companies[index.row()];
            if (index.column() == 0) {
                company.name = value.toString();
                emit dataChanged(index, index);
                return true;
            }
        } else if (index.internalId() < companies.size()) {
            Company &company = companies[index.internalId()];
            Department &department = company.departments[index.row()];
            if (index.column() == 0) {
                department.name = value.toString();
                emit dataChanged(index, index);
                return true;
            }
        } else {
            int companyIndex = index.internalId() / 1000;
            int departmentIndex = index.internalId() % 1000;
            Company &company = companies[companyIndex];
            Department &department = company.departments[departmentIndex];
            Employee &employee = department.employees[index.row()];
    
            switch (index.column()) {
            case 0:
                employee.name = value.toString();
                break;
            case 1:
                employee.position = value.toString();
                break;
            case 2:
                employee.gender = value.value<Gender>();
                break;
            default:
                return false;
            }
    
            emit dataChanged(index, index);
            return true;
        }
    
        return false;
    }
    
    Qt::ItemFlags CompanyModel::flags(const QModelIndex &index) const {
        if (!index.isValid()) {
            return Qt::NoItemFlags;
        }
        Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
        if (index.column() >= 0 && index.column() <= 3) {
            flags |= Qt::ItemIsEditable;
        }
        return flags;
    }
    
    JonBJ 1 Reply Last reply
    0
    • A arturio955

      Can somebody please help. I have a problem opening the department tab in 1st company after adding employee in department, its crashing. It gives sigsegv signal.

      CompanyModel::CompanyModel(QObject *parent)
          : QAbstractItemModel(parent){}
      
      QModelIndex CompanyModel::index(int row, int column, const QModelIndex &parent) const {
          if (!hasIndex(row, column, parent))
              return QModelIndex();
      
          if (!parent.isValid()) {
              return createIndex(row, column, quintptr(-1));
          } else if (parent.internalId() == quintptr(-1)) {
              return createIndex(row, column, parent.row());
          } else {
              return createIndex(row, column, parent.internalId() * 1000 + parent.row());
          }
      }
      
      QModelIndex CompanyModel::parent(const QModelIndex &index) const {
          if (!index.isValid())
              return QModelIndex();
      
          quintptr internalId = index.internalId();
          if (internalId == quintptr(-1))
              return QModelIndex();
      
          if (internalId < 1000) {
              return createIndex(internalId, 0, quintptr(-1));
          } else {
              int companyIndex = internalId / 1000;
              int departmentIndex = internalId % 1000;
              return createIndex(departmentIndex, 0, companyIndex);
          }
      }
      
      int CompanyModel::rowCount(const QModelIndex &parent) const {
          if (!parent.isValid())
              return companies.size();
      
          if (parent.internalId() == -1) {
              return companies.at(parent.row()).departments.size();
          } else if (parent.internalId() >= 0 && parent.internalId() < companies.size()) {
              return companies.at(parent.internalId()).departments.at(parent.row()).employees.size();
          }
      
          return 0;
      }
      int CompanyModel::columnCount(const QModelIndex &parent) const {
          Q_UNUSED(parent);
          return 4;
      }
      
      QVariant CompanyModel::data(const QModelIndex &index, int role) const {
          if (!index.isValid())
              return QVariant();
      
          if (index.internalId() == quintptr(-1)) {
              const Company &company = companies.at(index.row());
              if (role == Qt::DisplayRole && index.column() == 0)
                  return company.name;
              else if (role == Qt::DecorationRole && index.column() == 0)
                  return company.logo.isNull() ? StaticIcons::companyIcon() : company.logo;
          } else if (index.internalId() < companies.size()) {
              const Department &department = companies.at(index.internalId()).departments.at(index.row());
              if (role == Qt::DisplayRole && index.column() == 0)
                  return department.name;
              else if (role == Qt::DecorationRole && index.column() == 0)
                  return StaticIcons::departmentIcon();
          } else {
              int companyIndex = index.internalId() / 1000;
              int departmentIndex = index.internalId() % 1000;
              const Employee &employee = companies.at(companyIndex).departments.at(departmentIndex).employees.at(index.row());
              if (role == Qt::DisplayRole) {
                  switch (index.column()) {
                  case 0: return employee.name;
                  case 1: return employee.position;
                  case 2: return employee.gender == Gender::Male ? "Мужской" : "Женский";
                  }
              } else if (role == Qt::DecorationRole && index.column() == 0) {
                  return employee.photo.isNull() ? StaticIcons::employeeIcon() : employee.photo;
              }
          }
          return QVariant();
      }
      
      QVariant CompanyModel::headerData(int section, Qt::Orientation orientation, int role) const {
          if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
              switch (section) {
              case 0:
                  return QString("Name");
              case 1:
                  return QString("Position");
              case 2:
                  return QString("Gender");
              }
          }
          return QVariant();
      }
      
      bool CompanyModel::setData(const QModelIndex &index, const QVariant &value, int role) {
          if (!index.isValid() || role != Qt::EditRole) {
              return false;
          }
      
          if (index.internalId() == quintptr(-1)) {
              Company &company = companies[index.row()];
              if (index.column() == 0) {
                  company.name = value.toString();
                  emit dataChanged(index, index);
                  return true;
              }
          } else if (index.internalId() < companies.size()) {
              Company &company = companies[index.internalId()];
              Department &department = company.departments[index.row()];
              if (index.column() == 0) {
                  department.name = value.toString();
                  emit dataChanged(index, index);
                  return true;
              }
          } else {
              int companyIndex = index.internalId() / 1000;
              int departmentIndex = index.internalId() % 1000;
              Company &company = companies[companyIndex];
              Department &department = company.departments[departmentIndex];
              Employee &employee = department.employees[index.row()];
      
              switch (index.column()) {
              case 0:
                  employee.name = value.toString();
                  break;
              case 1:
                  employee.position = value.toString();
                  break;
              case 2:
                  employee.gender = value.value<Gender>();
                  break;
              default:
                  return false;
              }
      
              emit dataChanged(index, index);
              return true;
          }
      
          return false;
      }
      
      Qt::ItemFlags CompanyModel::flags(const QModelIndex &index) const {
          if (!index.isValid()) {
              return Qt::NoItemFlags;
          }
          Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
          if (index.column() >= 0 && index.column() <= 3) {
              flags |= Qt::ItemIsEditable;
          }
          return flags;
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @arturio955
      Hi. If you think about it, nobody can possibly help you with your issue from the description you give without any code, can they? [Ah, now you have added some code. I would suggest you drastically reduce this code to whatever minimal reproduces the issue, it cannot need all that code, most of it must be irrelevant.]

      If you have a SIGSEGV you must run your code from the debugger. When it receives that signal it will break at that point; look in the debugger's stack trace pane to follow it back to the line in your code which caused it and examine that.

      After that, Qt provides a QAbstractItemModelTester Class for you to use:

      The QAbstractItemModelTester class is a utility class to test item models.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        arturio955
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • A Offline
          A Offline
          arturio955
          wrote on last edited by arturio955
          #4

          What can it be if my function in infinity loop

          int CompanyModel::rowCount(const QModelIndex &parent) const {
              if (!parent.isValid()) {
                  qDebug() << "Root level: Number of companies =" << companies.size();
                  return companies.size();
              }
          
              quintptr id = parent.internalId();
          
              if (id == quintptr(-1)) {
                  if (parent.row() >= 0 && parent.row() < companies.size()) {
                      int numDepartments = companies.at(parent.row()).departments.size();
                      qDebug() << "Company level: Number of departments in company" << parent.row() << "=" << numDepartments;
                      return numDepartments;
                  } else {
                      qDebug() << "Invalid company index:" << parent.row();
                  }
              } else if (id >= 0) {
           
                  int companyIndex = id / 1000;
                  int departmentIndex = id % 1000;
          
           
                  if (companyIndex >= 0 && companyIndex < companies.size()) {
                      const Company &company = companies.at(companyIndex);
                      if (departmentIndex >= 0 && departmentIndex < company.departments.size()) {
                          int numEmployees = company.departments.at(departmentIndex).employees.size();
                          qDebug() << "Department level: Number of employees in department" << departmentIndex << "of company" << companyIndex << "=" << numEmployees;
                          return numEmployees;
                      } else {
                          qDebug() << "Invalid department index:" << departmentIndex;
                      }
                  } else {
                      qDebug() << "Invalid company index:" << companyIndex;
                  }
              }
          
              return 0;
          }
          

          output:Department level: Number of employees in department 0 of company 0 = 1
          Department level: Number of employees in department 0 of company 0 = 1
          Department level: Number of employees in department 0 of company 0 = 1
          Department level: Number of employees in department 0 of company 0 = 1
          Department level: Number of employees in department 0 of company 0 = 1
          Department level: Number of employees in department 0 of company 0 = 1
          Department level: Number of employees in department 0 of company 0 = 1
          Department level: Number of employees in department 0 of company 0 = 1
          Department level: Number of employees in department 0 of company 0 = 1
          Department level: Number of employees in department 0 of company 0 = 1
          Department level: Number of employees in department 0 of company 0 = 1
          Department level: Number of employees in department 0 of company 0 = 1
          Department level: Number of employees in department 0 of company 0 = 1
          Department level: Number of employees in department 0 of company 0 = 1
          Department level: Number of employees in department 0 of company 0 = 1
          Department level: Number of employees in department 0 of company 0 = 1
          Department level: Number of employees in department 0 of company 0 = 1
          Department level: Number of employees in department 0 of company 0 = 1
          Department level: Number of employees in department 0 of company 0 = 1 and than crash

          JonBJ 1 Reply Last reply
          0
          • A arturio955

            What can it be if my function in infinity loop

            int CompanyModel::rowCount(const QModelIndex &parent) const {
                if (!parent.isValid()) {
                    qDebug() << "Root level: Number of companies =" << companies.size();
                    return companies.size();
                }
            
                quintptr id = parent.internalId();
            
                if (id == quintptr(-1)) {
                    if (parent.row() >= 0 && parent.row() < companies.size()) {
                        int numDepartments = companies.at(parent.row()).departments.size();
                        qDebug() << "Company level: Number of departments in company" << parent.row() << "=" << numDepartments;
                        return numDepartments;
                    } else {
                        qDebug() << "Invalid company index:" << parent.row();
                    }
                } else if (id >= 0) {
             
                    int companyIndex = id / 1000;
                    int departmentIndex = id % 1000;
            
             
                    if (companyIndex >= 0 && companyIndex < companies.size()) {
                        const Company &company = companies.at(companyIndex);
                        if (departmentIndex >= 0 && departmentIndex < company.departments.size()) {
                            int numEmployees = company.departments.at(departmentIndex).employees.size();
                            qDebug() << "Department level: Number of employees in department" << departmentIndex << "of company" << companyIndex << "=" << numEmployees;
                            return numEmployees;
                        } else {
                            qDebug() << "Invalid department index:" << departmentIndex;
                        }
                    } else {
                        qDebug() << "Invalid company index:" << companyIndex;
                    }
                }
            
                return 0;
            }
            

            output:Department level: Number of employees in department 0 of company 0 = 1
            Department level: Number of employees in department 0 of company 0 = 1
            Department level: Number of employees in department 0 of company 0 = 1
            Department level: Number of employees in department 0 of company 0 = 1
            Department level: Number of employees in department 0 of company 0 = 1
            Department level: Number of employees in department 0 of company 0 = 1
            Department level: Number of employees in department 0 of company 0 = 1
            Department level: Number of employees in department 0 of company 0 = 1
            Department level: Number of employees in department 0 of company 0 = 1
            Department level: Number of employees in department 0 of company 0 = 1
            Department level: Number of employees in department 0 of company 0 = 1
            Department level: Number of employees in department 0 of company 0 = 1
            Department level: Number of employees in department 0 of company 0 = 1
            Department level: Number of employees in department 0 of company 0 = 1
            Department level: Number of employees in department 0 of company 0 = 1
            Department level: Number of employees in department 0 of company 0 = 1
            Department level: Number of employees in department 0 of company 0 = 1
            Department level: Number of employees in department 0 of company 0 = 1
            Department level: Number of employees in department 0 of company 0 = 1 and than crash

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @arturio955 What about stepping through your code in debugger to see what is happening?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              arturio955
              wrote on last edited by arturio955
              #6

              @JonB
              1 QList<Company>::data qlist.h 418 0x7ff6151c5413
              2 QList<Company>::at qlist.h 434 0x7ff6151c53df
              3 CompanyModel::rowCount CompanyModel.cpp 61 0x7ff6151b51d8
              5 CompanyModel::index CompanyModel.cpp 11 0x7ff6151b4d4a

              JonBJ 1 Reply Last reply
              0
              • A arturio955

                @JonB
                1 QList<Company>::data qlist.h 418 0x7ff6151c5413
                2 QList<Company>::at qlist.h 434 0x7ff6151c53df
                3 CompanyModel::rowCount CompanyModel.cpp 61 0x7ff6151b51d8
                5 CompanyModel::index CompanyModel.cpp 11 0x7ff6151b4d4a

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @arturio955
                That is not " stepping through your code in debugger to see what is happening". I'll leave it to someone else who wants to help you.

                A 1 Reply Last reply
                0
                • JonBJ JonB

                  @arturio955
                  That is not " stepping through your code in debugger to see what is happening". I'll leave it to someone else who wants to help you.

                  A Offline
                  A Offline
                  arturio955
                  wrote on last edited by
                  #8

                  @JonB What did you want?

                  JonBJ 1 Reply Last reply
                  0
                  • A arturio955

                    @JonB What did you want?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @arturio955
                    I am suggesting some sort of debugging effort from you. For example, I originally suggested you allow it to SEGV, then move up the stack trace to which line of code was the last to execute and examine it. You have the source code. If you make me guess

                    5 CompanyModel::index CompanyModel.cpp 11 0x7ff6151b4d4a

                    Does that perchance indicate line #11 of CompanyModel.cpp? You will have the line in front of you, so you know. Is that the 11th line of what you show, which seems to be:

                    int numDepartments = companies.at(parent.row()).departments.size();

                    ? I don't know. If it is, what is the value of companies.at(parent.row()).departments? If it is a different line, what is going on in that line which could cause a SEGV? That sort of thing.

                    I also linked to the QAbstractItemModelTester Class which might (or might not) reveal more information to you.

                    Nobody can actually reproduce your issue because they don't know what data you have.

                    1 Reply Last reply
                    1
                    • A Offline
                      A Offline
                      arturio955
                      wrote on last edited by
                      #10

                      @JonB
                      3 CompanyModel::rowCount CompanyModel.cpp 61 0x7ff6151b51d8:
                      else if (parent.internalId() >= 0 && parent.internalId() < companies.size()) {
                      return companies.at(parent.internalId()).departments.at(parent.row()).employees.size();
                      5 CompanyModel::index CompanyModel.cpp 11 0x7ff6151b4d4a:
                      if (!hasIndex(row, column, parent))
                      return QModelIndex();

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved