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

QList with Struct issue in QT6

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.1k 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 13 Nov 2022, 12:30 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
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 13 Nov 2022, 12:38 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 13 Nov 2022, 17:24
      2
      • C Christian Ehrlicher
        13 Nov 2022, 12:38

        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 13 Nov 2022, 17:24 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?

        J C 2 Replies Last reply 13 Nov 2022, 17:42
        0
        • C Ceraolo_gmail
          13 Nov 2022, 17:24

          @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?

          J Offline
          J Offline
          JonB
          wrote on 13 Nov 2022, 17:42 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 13 Nov 2022, 18:24
          1
          • C Ceraolo_gmail
            13 Nov 2022, 17:24

            @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?

            C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 13 Nov 2022, 18:09 last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • J JonB
              13 Nov 2022, 17:42

              @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 13 Nov 2022, 18:24 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
              • C Online
                C Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 13 Nov 2022, 18:52 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

                3/7

                13 Nov 2022, 17:24

                • Login

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