【问题标题】:Cannot modify height value from QRect无法从 QRect 修改高度值
【发布时间】:2013-05-20 13:58:37
【问题描述】:

以下 sn-p 导致我的编译产生“错误:将 'const QRect' 作为 'void QRect::setHeight(int)' 的 'this' 参数传递会丢弃限定符 [-fpermissive]”。

我该如何解决这个问题,而且我注意到如果我要替换 h -= 80;使用 h--;,编译器不会抱怨。

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

【问题讨论】:

  • datumTable 对象或geometry 函数已被标记为const。 IE。它们不能被修改。
  • 为什么 QTableWidget 会有一个 setHeight 方法?

标签: c++ qt qrect


【解决方案1】:

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

它是只读的getter。您应该复制一份,修改它并使用setGeometry setter 函数将其重新设置:

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

【讨论】:

    【解决方案2】:
    QRect g = this->geometry().height();
    g.setHeight(g.height()-80);
    ui->datumTable->setGeometry(g);
    

    【讨论】:

      【解决方案3】:

      似乎datumTable 中的geometry() 返回一个const QRect。除非也有非常量版本,否则这不是一个简单的解决方法。

      【讨论】:

      • 很确定相同的代码已经在 Windows 上的 qt 中运行,这可能是 linux 变体问题/错误吗?这不一定是个难题
      • @johnsonwi 因为返回的值是一个常量引用,所以编译器不会让你修改它。
      猜你喜欢
      • 2011-12-05
      • 1970-01-01
      • 2017-03-13
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多