Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

Compare with string and print the value assigned to the letter



  •             struct Property
                {
                    QString data3[1];
                    QString properties[1];
                };
                QVector<Property> property;
                property =
                {
                    {{ "М2"}, {"Single thread modification."}},
                    {{ "М"  }, {"Single thread modification."}},
                    {{ " "     }, {"Single thread modification."}},
                    {{ "D"   }, {"Modification for two threads" }},
                };
                for (int i = 0; i < property.size(); i++)
                {
                    if (data3.at(0)==data_[3] || data3.at(1)==data_[3])
                       return description = property.properties(0);
                }
    

    Confused.
    I compare with the string: if there is one of the values, the message Single thread modification or Modification for two threads is displayed.
    Now nothing is displayed



  • @fender
    Before beginning to decipher your code/address your question: in your struct Property you define the data3 & properties members as arrays of QStrings, each holding just 1 element.

    Is that really what you intend, or should a single Property instance hold just one (of each) data3 & properties QString?



  • @JonB
    That's right, you understand, an array with one element

    Each letter / number from the line has its own decryption
    For example: SAB-A-F-30-1-M2
    Decryption
    A - text
    F - text
    30 - text
    1 - text
    M2 - Single thread modification


  • Lifetime Qt Champion

    @fender What is data3 and data_?



  • @fender

    I admit I am not the world's expert C++-er, but:

    • Where are the declarations/values of all the local variables or member variables you do not show (data3, data_, description)?

    • Following on from that: your data3.at() do not refer to the data3 in your Property/property. Is that really right?

    • What is property.properties(0) supposed to refer to?

    • Are you indeed intending an assignment rather than a comparison in the return result?

    Also, I'm surprised statements like property.properties(0) even compile....


  • Moderators

    hi @fender

    this

    return description = property.properties(0);

    is not a comparison, but an assignment.

    I'm not sure what's supposed to be return bool? QString ? I'm also not sure about the order of execution here, description might be return befor it gets it's new value.



  • @J.Hilk
    In case you're asking: the code as written will return the value assigned to description, which I presume to be QString. And as I said probably not what the OP intended?

    For my edification, could you explain to me how the user;s code compiles:

    QVector<Property> property;
    ...
    return description = property.properties(0);
    

    Even if we ignore the description, what is property.properties(0) at compilation-time??


  • Moderators

    @JonB
    I'm a bit lost about that myself,
    maybe the op has omitted a function declaration in the struct, because neither arrays nor QString support direct access via ().

    If It's a typo and it's meant to be [] than it would be a QChar or QCharRef



  • @J.Hilk

    maybe the op has omitted a function declaration in the struct

    My point is that QVector<Property> property makes property a QVector<Property> avector of Propertys, it's not a struct Property. So the property.properties the OP uses has nothing to do with the properties member in struct Property. properties would need to be a member of the QVector, not the struct....



  • QVector<Property> property;
                property =
                {
                    {{ "М2"}, {"Single thread modification."}},
                    {{ "М"  }, {"Single thread modification."}},
                    {{ " "     }, {"Single thread modification."}},
                    {{ "D"   }, {"Modification for two threads" }},
                };
    

    You try to populate an array of Property struct, but that doesn't work for a container.

    Populate an array of struct:

    struct Property
        {
            QString data3;
            QString properties;
        };
    Property properties[] =
        {
            { "М2", "Single thread modification."},
            { "М",  "Single thread modification."},
            { " ",  "Single thread modification."},
            { "D",  "Modification for two threads" },
        };
    

    Populate a vector:

    QVector<Property> _properties;
        _properties<< Property {"М2", "Single thread modification."}
        << Property { "М",  "Single thread modification."}
        << Property { " ",  "Single thread modification."}
        << Property { "D",  "Modification for two threads" };
    

    Access to values:

    // array
    for (int i = 0; i < sizeof(properties)/sizeof(Property); i++)
        {
        qDebug()<<properties[i].data3;
        }
    
    // vector
    for (Property p : _properties)
        {
        qDebug()<<p.data3;
        }
    


  • @JonB
    I have a string

    QString MainWindow::dataString() const
    {
        QString format = "%0%1%2%3%4%5%6%7%8%9%10%11";
        format
                .arg(data_[0])     
                .arg(data_[1])     
                .arg(data_[2])  
                .arg(data_[3])             
                .arg(data_[4])            
                .arg(data_[5])             
                .arg(data_[6])             
                .arg(data_[7])             
                .arg(data_[8])             
                .arg(data_[9])             
                .arg(data_[10])            
                .arg(data_[11]);          
        return format;
    }
    

    The idea was as follows: if the date [3] contains "M2", then the following tab displays the decryption "M2 - Single thread modification."
    So I tried to compare the value from the array with the string and if it matches the output text



  • @fender

    1. Your original code is so all-over-the-place/uncompilable/unintelligible that it's hard to help.

    2. Your new code seems to have nothing to do with it.

    3. Your new code will not work as you might think it does because when you read http://doc.qt.io/qt-5/qstring.html#arg (you did read that, didn't you?) you see that it does not alter your format variable --- which specifies a format --- but rather returns a new QString which is the result of applying the formatting.

    I don't wish to sound harsh, but you really need to gather your ideas together a bit more carefully, and perhaps look at some other peoples' examples of C++ coding in general if you hope to get anywhere useful.


Log in to reply