Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Pretty C++ general question (using Qt)

    General and Desktop
    4
    7
    193
    Loading More Posts
    • 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.
    • U
      U7Development last edited by U7Development

      Hi mates.. first of all, my apologies if this does not belong to Qt Development itself, i guess is somehow related...

      I have many QLabels on my form, i decided to use a standard vector to contain them, so i can iterate the vector to configure my QLabels shortly. For ranged detects the QLabel function members, but standard iterator does not, why?

      for (std::vector<QLabel*>::iterator ite = vQLabels.begin(); ite != vQLabels.end(); ++ite) { 
              *ite = new QLabel;       // <--- this is OK
              *ite->setStyleSheet(C_STYLESHEET_FIELD);     // <---- Error no member named setStyleSheet in __gnu_cxx
      }
      

      it is supposed that ite is the iterator pointing to QLabel object isn't ?

      But this works....

      for (auto& v : vQLabels){
            v->setStyleSheet(C_STYLESHEET_FIELD);     // <----- correct
      }
      

      Any ideas?.
      Thanks so much.

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @U7Development last edited by JonB

        @U7Development said in Pretty C++ general question (using Qt):

        *it->setStyleSheet(C_STYLESHEET_FIELD); // <---- Error no member named setStyleSheet in __gnu_cxx

        Guess (I like making these ;) ): what's the precedence of that leading *, compared to ->? Isn't it supposed to be:

        (*it)->setStyleSheet(C_STYLESHEET_FIELD);
        
        1 Reply Last reply Reply Quote 5
        • JonB
          JonB @U7Development last edited by JonB

          @U7Development said in Pretty C++ general question (using Qt):

          *it->setStyleSheet(C_STYLESHEET_FIELD); // <---- Error no member named setStyleSheet in __gnu_cxx

          Guess (I like making these ;) ): what's the precedence of that leading *, compared to ->? Isn't it supposed to be:

          (*it)->setStyleSheet(C_STYLESHEET_FIELD);
          
          1 Reply Last reply Reply Quote 5
          • U
            U7Development last edited by U7Development

            ohh you are right that works.. thanks.

            what i did not get is ite is supposed to be a pointer poiting to the QLabel pointer itself .. so theoretically if I use -> I could access to member functions too... seems weird isn't ?.

            for (std::vector<QLabel*>::iterator ite = vQLabels.begin(); ite != vQLabels.end(); ++ite) { 
                    *ite = new QLabel;     
                    QLabel*& lab = *ite;
                    lab->setStyleSheet(C_STYLESHEET_FIELD);     // <--- this is OK
            }
            

            Is the (*) a conventional prefix to use while containing pointers only ?
            Thanks again..

            J.Hilk 1 Reply Last reply Reply Quote 0
            • J.Hilk
              J.Hilk Moderators @U7Development last edited by J.Hilk

              @U7Development said in Pretty C++ general question (using Qt):

              Is the (*) a conventional prefix to use while containing pointers only ?
              Thanks again..

              the * has generally speaking 2 roles:

              • When used in a declaration (e.g. QLabel *label), it "creates" a pointer variable
              • When not used in a declaration, it is a dereference operator

              You can read it as "the value pointed to by"
              see here for more information


              edit:

              just as an FYI you could also write
              (*(*it)).setStyleSheet(C_STYLESHEET_FIELD);
              See here for more information

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

              Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              JonB 1 Reply Last reply Reply Quote 4
              • JonB
                JonB @J.Hilk last edited by

                @J-Hilk said in Pretty C++ general question (using Qt):

                the * has generally speaking 2 roles:

                So not multiplication then? ;-)

                Pl45m4 J.Hilk 2 Replies Last reply Reply Quote 2
                • Pl45m4
                  Pl45m4 @JonB last edited by Pl45m4

                  @JonB

                  Multiplication?!
                  I thought asterisk is a wildcard character in C++? :o

                  QString a = "DoNotBelieve";
                  QString b = "WhatImWritingHere";
                  
                  QString aa = "Seriously.Just.Dont";
                  QString bb = "Seriously.Just.Dont";
                  
                  if ( *a == *b)
                  {
                        qDebug() << "true, because aa = bb";
                  }
                  
                  

                  EDIT:

                  It hurts even MY eyes :D


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  1 Reply Last reply Reply Quote 0
                  • J.Hilk
                    J.Hilk Moderators @JonB last edited by

                    @JonB what is multiplication, but a chain of addition? 😎

                    @Pl45m4
                    does this compile for you? What compiler?

                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

                    Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post