Search code examples
c++qtqrect

Cannot modify height value from QRect


The follow snippet results in my compilation yielding "error: passing 'const QRect' as 'this' argument of 'void QRect::setHeight(int)' discards qualifiers [-fpermissive]".

How can I fix this and also I've noticed that if I were to replace h -= 80; with h--;, the compiler does not complain.

int h = this->geometry().height();
h -= 80;
ui->datumTable->geometry().setHeight(h);

Solution

  • geometry() returns a const reference to a QRect object inside QTableWidget.

    It's meant to be a read-only getter. You should take a copy, modify it and set it back with setGeometry setter function:

    QRect rect = this->geometry();
    int h = rect.height();
    rect.setHeight(h - 80);
    ui->datumTable->setGeometry(rect);