[Solved] moc, setProperty



  • Hi there,

    i have a short question concerning the setProperty member of an widget.
    In a slot i receive changes in a combobox which has the dynamic property "relatedProperty" set to a property of another widget like "orientation" for example.
    The slot procedure extracts the dynamic property. This is used for setting a widgets property depending on the "relatedProperty" value of the combobox and the value of the combobox itself. To achive this behaviour i tried the following c++ code.

    @
    char property = sender()->property("relatedProperty").toChar().toAscii();
    qDebug() << tempModule.widget->setProperty(&property, Value);
    @

    But i only get "false" as output for the last qDebug() line.
    I spent a lot of time solving this but i can't find a solution.
    I hope someone has good ideas to fixthis problem.

    cheers paul



  • I think I understand what you want and I try to make a solution.

    You can try this code:

    @QString relatedProperty(sender()->relatedProperty->property("relatedProperty").toString());
    char *property = new char[relatedProperty.toStdString().size() + 1];
    strcpy(property, relatedProperty.toStdString().c_str());
    ui->plainTextEdit->setProperty(property, Value);
    delete [] property;@

    Simpler version:

    @ui->plainTextEdit->setProperty(sender()->relatedProperty->property("relatedProperty").toString().toStdString().c_str(), Value);@

    Tell me if it worked, or if it is not what you need, explain better and we will make it work.



  • toChar() returns the QVariant as QChar, a single character. This is most probably not what you've meant, because "orientation" usually does not fit into a single character. Use toString() instead.

    @
    QObject objectA;
    objectA.setProperty("relatedProperty", QString("orientation"));

    ...

    QByteArray relatedProperty = objectA.property("relatedProperty").toString().toLatin1();

    QObject objectB;
    objectB.setProperty(relatedProperty.constData(), ...);
    @
    On a sidenote: do not use toAscii(); it is deprecated and will be no longer available in Qt5.



  • thank you very mouch for your help
    @
    ui->plainTextEdit->setProperty(sender()->property("relatedProperty").toString().toStdString().c_str(), Value);
    @
    works like a charm!



  • You are welcome. Feel free to edit the thread title to '[Solved] ...' to indicate that there is a solution inside.

    And bear in mind that there is a significat performance penalty in using <code>.toStdString().c_str()</code> over <code>.toLatin1().constData()</code>.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.