Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Pretty C++ general question (using Qt)



  • 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.



  • @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);
    


  • @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);
    


  • 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..


  • Moderators

    @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



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

    the * has generally speaking 2 roles:

    So not multiplication then? ;-)



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


  • Moderators

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

    @Pl45m4
    does this compile for you? What compiler?


Log in to reply