C++ 无法从QRect修改高度值
以下代码段导致我的编译产生“错误:将'const QRect'作为'void QRect::setHeight(int)'的'this'参数传递”。丢弃限定符[-fppermissive]” 我如何解决这个问题,我也注意到,如果我要替换h-=80;用h--;,编译器没有抱怨C++ 无法从QRect修改高度值,c++,qt,qrect,C++,Qt,Qrect,以下代码段导致我的编译产生“错误:将'const QRect'作为'void QRect::setHeight(int)'的'this'参数传递”。丢弃限定符[-fppermissive]” 我如何解决这个问题,我也注意到,如果我要替换h-=80;用h--;,编译器没有抱怨 int h = this->geometry().height(); h -= 80; ui->datumTable->geometry().setHeight(h); 似乎datumTable中的geo
int h = this->geometry().height();
h -= 80;
ui->datumTable->geometry().setHeight(h);
似乎
datumTable
中的geometry()。除非有一个非常量版本,否则这不是一个简单的解决方案 几何体()
它是一个只读的getter。您应该复制、修改并使用setGeometry
setter函数将其设置回:
QRect rect = this->geometry();
int h = rect.height();
rect.setHeight(h - 80);
ui->datumTable->setGeometry(rect);
datumTable
对象或geometry
函数已标记为const
。也就是说,它们不能被修改。为什么QTableWidget会有一个setHeight方法呢?很确定同样的代码在windows上的qt中工作过,这可能是linux变体的问题/错误吗?这不一定是个困难的问题problem@johnsonwi因为返回的值是常量引用,所以编译器不允许您修改它。
QRect g = this->geometry().height();
g.setHeight(g.height()-80);
ui->datumTable->setGeometry(g);