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. Boolean values and Qt roles in models

Boolean values and Qt roles in models

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 1.0k 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
    andi456
    wrote on last edited by
    #1

    Hi,

    as far as i understand it, if the role Qt::CheckStateRole is not used in the data method of a QAbstractTableModel a boolean value would be displayed as string (true/false). With the additional Qt::CheckStateRole i managed to get a default CheckBox alongside with string representation of the boolean value. The corresponding column is set to be editable and checkable. So now i can alter the string representation but the CheckBox does not respond to user input, although the Qt::ItemIsUserCheckable is set in the flags method too. Are those flags mutually exclusive in that way?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Do you properly handle Qt::CheckStateRole in your setData() function?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      A 1 Reply Last reply
      3
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        Also, Qt::CheckStateRole does not expect a boolean but a Qt::CheckState

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2
        • Christian EhrlicherC Christian Ehrlicher

          Do you properly handle Qt::CheckStateRole in your setData() function?

          A Offline
          A Offline
          andi456
          wrote on last edited by VRonin
          #4

          @Christian-Ehrlicher

          } else if (role==Qt::CheckStateRole) {
          
                    int zeile,
                            spalte;
                    zeile = index.row();
                    spalte = index.column();
                   nlohmann::json row_obj = ZTableModel::get_row_obj(zeile);
                   QList<QString> headerliste = ZTableModel::get_header(j_zl);
                   for (int i=0; i<headerliste.size(); ++i) {
                      if (i == spalte) {
                          QString qs = headerliste.at(i);
                          std::string spaltenname = qs.toStdString();
                          QVariant qvtest = value;
                          if ((value.canConvert<bool>()) && (qvtest.convert(QVariant::Bool))) {
                              bool neuerboolwert = value.toBool();
                              row_obj[spaltenname] = neuerboolwert;
                          }
                      }
          
                  }
             emit dataChanged(index, index);
             return true;
          

          This is the part of the setData() function, that should handle Qt::CheckStateRole. If it is inappropriate for boolean values, i wonder which role should be used instead.

          VRoninV 1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @andi456 said in Boolean values and Qt roles in models:

            i wonder which role should be used instead.

            Read the post from @VRonin or the documentation: "This role is used to obtain the checked state of an item. (Qt::CheckState)"

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1
            • A andi456

              @Christian-Ehrlicher

              } else if (role==Qt::CheckStateRole) {
              
                        int zeile,
                                spalte;
                        zeile = index.row();
                        spalte = index.column();
                       nlohmann::json row_obj = ZTableModel::get_row_obj(zeile);
                       QList<QString> headerliste = ZTableModel::get_header(j_zl);
                       for (int i=0; i<headerliste.size(); ++i) {
                          if (i == spalte) {
                              QString qs = headerliste.at(i);
                              std::string spaltenname = qs.toStdString();
                              QVariant qvtest = value;
                              if ((value.canConvert<bool>()) && (qvtest.convert(QVariant::Bool))) {
                                  bool neuerboolwert = value.toBool();
                                  row_obj[spaltenname] = neuerboolwert;
                              }
                          }
              
                      }
                 emit dataChanged(index, index);
                 return true;
              

              This is the part of the setData() function, that should handle Qt::CheckStateRole. If it is inappropriate for boolean values, i wonder which role should be used instead.

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @andi456 said in Boolean values and Qt roles in models:

              for (int i=0; i<headerliste.size(); ++i) {
              if (i == spalte)

              Why are you iterating?

              if ((value.canConvert<bool>()) && (qvtest.convert(QVariant::Bool)))

              As mentioned Qt::CheckStateRole does not take a bool. When you click on the checkbox value will contain Qt::Checked that equals 2.

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              A 1 Reply Last reply
              1
              • VRoninV VRonin

                @andi456 said in Boolean values and Qt roles in models:

                for (int i=0; i<headerliste.size(); ++i) {
                if (i == spalte)

                Why are you iterating?

                if ((value.canConvert<bool>()) && (qvtest.convert(QVariant::Bool)))

                As mentioned Qt::CheckStateRole does not take a bool. When you click on the checkbox value will contain Qt::Checked that equals 2.

                A Offline
                A Offline
                andi456
                wrote on last edited by
                #7

                @VRonin You're right, the iteration is unnecessary.. I will correct that and see, if it changes something and write the code to convert the Qt::Checked value to boolean and for the data() function too.

                1 Reply Last reply
                1
                • A Offline
                  A Offline
                  andi456
                  wrote on last edited by andi456
                  #8
                              int zeile,
                                    spalte;
                              zeile = index.row();
                              spalte = index.column();
                              nlohmann::json row_obj = ZTableModel::get_row_obj(zeile);
                              QList<QString> headerliste = ZTableModel::get_header(j_zl);
                              QString qs = headerliste.at(spalte);
                              std::string spaltenname = qs.toStdString();
                              QVariant qvtest = value;
                              bool neuerboolwert;
                              if ((value.canConvert<int>()) && (qvtest.convert(QVariant::Int))) {
                                  int checker = value.toInt();
                                  if (checker==Qt::Checked) {
                                      neuerboolwert = true;
                                      std::cout << "Checked" << std::endl;
                                      row_obj[spaltenname] = neuerboolwert;
                                      //code to update the data structure
                                  } else {
                                      neuerboolwert = false;
                                      std::cout << "UnChecked" << std::endl;
                                      row_obj[spaltenname] = neuerboolwert;
                                     //code to update the data structure
                                      }
                                  }
                  
                     emit dataChanged(index, index);
                     return true;
                  

                  Seems to work with the corresponding code to set Qt::Checked/Qt::Unchecked values on the data() function. Anyway, I'll disable the editable flag for boolean values, because i can still enter text to the right of checkbox, which overrides it (the checkbox).

                  Thanks for your support.

                  Kind regards,

                  Andreas

                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9

                    @andi456 said in Boolean values and Qt roles in models:

                    i can still enter text to the right of checkbox, which overrides it (the checkbox).

                    If this happens it's a bug in your setData/data implementation

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @VRonin said in Boolean values and Qt roles in models:

                      If this happens it's a bug in your setData/data implementation

                      And flags() - it returns Qt::ItemIsEditable

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      3

                      • Login

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