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. Implementation of QVector

Implementation of QVector

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 936 Views 1 Watching
  • 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.
  • ChristianHeuselC Offline
    ChristianHeuselC Offline
    ChristianHeusel
    wrote on last edited by ChristianHeusel
    #1

    According to the documentation over at https://doc.qt.io/qt-5/qvector.html#at there is only one implementation for QVector::at(), of which applies constness.
    I just assumed from my STL expierience that there would be one which is non-const.
    version of the function what would make sense in my opinion.
    I got the error with the following codesnippet, please explain if i get something entirely wrong :)

    void MyClass::on_MyComboBox_currentTextChanged(const QString& newtext) {
       auto index = _ui->MyComboBox->currentIndex();
       _myQVector.at(index) = arg1;
       for(const auto& element : _myQVector) qInfo() << element;
    }
    
    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      You can't assign like this. at returns a value and not variable

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      ChristianHeuselC 1 Reply Last reply
      3
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        Oddly enough, there is no non const version of at()
        However, the operator [] has one
        https://doc.qt.io/qt-5/qvector.html#operator-5b-5d

        1 Reply Last reply
        2
        • dheerendraD dheerendra

          You can't assign like this. at returns a value and not variable

          ChristianHeuselC Offline
          ChristianHeuselC Offline
          ChristianHeusel
          wrote on last edited by ChristianHeusel
          #4

          @dheerendra

          @dheerendra said in Implementation of QVector:

          [..] at returns a value and not variable

          Yeah but why is that the case? As far as i know there could be a non-const version like this:

          T& QVector::at()
          

          For the operator[] there is a non-const and a const one, why is this not the case with at()?!

          aha_1980A 1 Reply Last reply
          0
          • ChristianHeuselC ChristianHeusel

            @dheerendra

            @dheerendra said in Implementation of QVector:

            [..] at returns a value and not variable

            Yeah but why is that the case? As far as i know there could be a non-const version like this:

            T& QVector::at()
            

            For the operator[] there is a non-const and a const one, why is this not the case with at()?!

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @ChristianHeusel the soley reason at() exists is to provide a read-only access to make sure the implicitely shared container does not detach.

            For write access, use []

            regards

            Qt has to stay free or it will die.

            ChristianHeuselC 1 Reply Last reply
            2
            • aha_1980A aha_1980

              @ChristianHeusel the soley reason at() exists is to provide a read-only access to make sure the implicitely shared container does not detach.

              For write access, use []

              regards

              ChristianHeuselC Offline
              ChristianHeuselC Offline
              ChristianHeusel
              wrote on last edited by
              #6

              @aha_1980 Supposing that your answer is correct my question is now invalid since the purpose of std::vector::at() is then a diffrent one than it is in QT.
              Question will be marked as resolved :)

              aha_1980A 1 Reply Last reply
              1
              • ChristianHeuselC ChristianHeusel

                @aha_1980 Supposing that your answer is correct my question is now invalid since the purpose of std::vector::at() is then a diffrent one than it is in QT.
                Question will be marked as resolved :)

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @ChristianHeusel it's indirectly stated here: https://doc.qt.io/qt-5/qvector.html#operator-5b-5d (a bit hidden)

                "non-const operators can cause a deep copy".

                Qt has to stay free or it will die.

                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