Congratulations to our 2022 Qt Champions!

[SOLVED] Error with setRect in QRect

  • Hello all!

    I have a QRect and when I try to setRect it return this error

    /QT/Projetos/VisaoFinanceira/fluxocaixawidget.cpp:174: error: member function 'setRect' not viable: 'this' argument has type 'const QRect', but function is not marked const,7,7,7);


    Here is the code:
    QList<QRect> fluxoRect;


  • Hey,

    look at the definition of the at method:
    const T & at(int i) const

    QList returns a const reference to your QRect, hence you're not allowed to alter it.

    You have to do something like this:
    @ QList<QRect> fluxoRect;

    QRect r = fluxoRect.takeAt(i);
    r.setRect(7, 7, 7, 7);
    fluxoRect.replace(i, r);@


  • Lifetime Qt Champion


    at is a constant function so you can't modify it's returned value like that.

    Either use the value method or the [] operator.

    You can also take the rect modify it and replace the original value with the new value.

    Hope it helps

  • Thank you for the replies Saugglocke and SGaist

    It worked.


Log in to reply