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. QList with Struct issue in QT6
Forum Updated to NodeBB v4.3 + New Features

QList with Struct issue in QT6

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.3k 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.
  • C Offline
    C Offline
    Ceraolo_gmail
    wrote on last edited by Ceraolo_gmail
    #1

    I would like to use a QList of a struct, and in particular to use the "contains" function.
    I have a nasty issue, that can be replicated using the following code:

    struct STest{
        int i1,i2;
        bool operator== (const STest & x);
    };
       QList <STest> myList;
        STest test1={1,2}, test2={11,12};
    
        myList.append(test1);
        myList.append(test2);
    
        bool contains;
        contains=myList.contains(test1);
        qDebug()<<contains;
    

    It compiles without warnings and runs smoothly in Qt 5.15, but cannot compile in Qt6.4. I get the error message shown at the end of this post.
    Despite of the many attempts I made I couls not sort this out.
    Any suggestions? Thank you in advance.

    D:\OneDrive - University of Pisa\Documenti\C++\Qt\untitled\mainwindow.cpp:28: error: passing 'const STest' as 'this' argument discards qualifiers [-fpermissive]
    In file included from C:/Qt/6.4.0/mingw_64/include/QtCore/qobject.h:13,
                     from C:/Qt/6.4.0/mingw_64/include/QtWidgets/qwidget.h:9,
                     from C:/Qt/6.4.0/mingw_64/include/QtWidgets/qmainwindow.h:8,
                     from C:/Qt/6.4.0/mingw_64/include/QtWidgets/QMainWindow:1,
                     from ..\untitled\mainwindow.h:4,
                     from ..\untitled\mainwindow.cpp:1:
    C:/Qt/6.4.0/mingw_64/include/QtCore/qlist.h: In instantiation of 'qsizetype QtPrivate::indexOf(const QList<T>&, const U&, qsizetype) [with V = STest; U = STest; qsizetype = long long int]':
    C:/Qt/6.4.0/mingw_64/include/QtCore/qlist.h:932:30:   required from 'qsizetype QListSpecialMethodsBase<T>::indexOf(const AT&, qsizetype) const [with AT = STest; T = STest; qsizetype = long long int]'
    C:/Qt/6.4.0/mingw_64/include/QtCore/qlist.h:44:31:   required from 'bool QListSpecialMethodsBase<T>::contains(const AT&) const [with AT = STest; T = STest]'
    ..\untitled\mainwindow.cpp:28:29:   required from here
    C:/Qt/6.4.0/mingw_64/include/QtCore/qlist.h:903:20: error: passing 'const STest' as 'this' argument discards qualifiers [-fpermissive]
      903 |             if (*n == u)
          |                 ~~~^~~~
    ..\untitled\mainwindow.cpp:10:6: note:   in call to 'bool STest::operator==(const STest&)'
       10 | bool STest::operator== (const STest &x){
          |      ^~~~~
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      The operator== must be const.

      The compiler also tells you that you want to use a non-const function but expects a const one: passing 'const STest' as 'this' argument discards qualifiers [-fpermissive]

      C 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        The operator== must be const.

        The compiler also tells you that you want to use a non-const function but expects a const one: passing 'const STest' as 'this' argument discards qualifiers [-fpermissive]

        C Offline
        C Offline
        Ceraolo_gmail
        wrote on last edited by Ceraolo_gmail
        #3

        @Christian-Ehrlicher Yes, the issue is somehow reated to the "constantness" of something.
        But I don't understand how to solve this. To be acceptable by 5.15 the code requires 'const' in the parenthesis of the operator ==.

        I even suspect that my example is correct since Qt 5.15 does not give any warnings or errors. In that case I would consider accceptable to bypass the Qt6 error message, but I don't know how to do this.
        Maybe setting "-fpermissive"? But how to do this?

        JonBJ Christian EhrlicherC 2 Replies Last reply
        0
        • C Ceraolo_gmail

          @Christian-Ehrlicher Yes, the issue is somehow reated to the "constantness" of something.
          But I don't understand how to solve this. To be acceptable by 5.15 the code requires 'const' in the parenthesis of the operator ==.

          I even suspect that my example is correct since Qt 5.15 does not give any warnings or errors. In that case I would consider accceptable to bypass the Qt6 error message, but I don't know how to do this.
          Maybe setting "-fpermissive"? But how to do this?

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

          @Ceraolo_gmail
          You are supposed to change your == operator declaration to match exactly the one in the docs, so:

          bool operator== (const STest & x) const;
          

          It is the const at the end that you are missing.

          C 1 Reply Last reply
          1
          • C Ceraolo_gmail

            @Christian-Ehrlicher Yes, the issue is somehow reated to the "constantness" of something.
            But I don't understand how to solve this. To be acceptable by 5.15 the code requires 'const' in the parenthesis of the operator ==.

            I even suspect that my example is correct since Qt 5.15 does not give any warnings or errors. In that case I would consider accceptable to bypass the Qt6 error message, but I don't know how to do this.
            Maybe setting "-fpermissive"? But how to do this?

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • JonBJ JonB

              @Ceraolo_gmail
              You are supposed to change your == operator declaration to match exactly the one in the docs, so:

              bool operator== (const STest & x) const;
              

              It is the const at the end that you are missing.

              C Offline
              C Offline
              Ceraolo_gmail
              wrote on last edited by
              #6

              @JonB
              Great! I could not find the piece of docs you linked.
              This solves my issue.
              Thanks a lot!

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

                You're right - the Qt documentation is not that clear about the constness but c++ is: https://en.cppreference.com/w/cpp/language/operator_comparison

                1 Reply Last reply
                1

                • Login

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