• 4578阅读
  • 2回复

《c++ gui qt 编程》问题求助? [复制链接]

上一主题 下一主题
离线zhongfugutao
 

只看楼主 倒序阅读 楼主  发表于: 2012-11-01
刚学习QT一个多月的菜鸟,现在正在啃《C++ gui qt》这本书,在看书的第五章 “创建自定义窗口部件”的icon editor 有点疑问:
例子中有一个鼠标点击或者拖动对像素进行设置或者清空的函数setImagePixel,最后调用了update(pixelRect(i, j))函数,书上说对需要重新绘制的区域调用该函数。
我有点不明白,调用update应该会触发paintevent事件吧,这个事件在例子中重写了,对整个图片都进行了绘制,这样的话调用带参数的update和只调用update()有什么区别呢?也没法重绘部分区域啊?

各位老大给点提示呗。书上为什么这么写?好处是神马?
离线zhongfugutao

只看该作者 1楼 发表于: 2012-11-01
不好意思,自己发现原因了,重写的paintEvent没有仔细看,绘图的时候,有个判断:</br>
if (!event->region().intersect(rect).isEmpty()) {</br>
这里判断了要绘制的矩形和event事件需要绘制的矩形有没有交汇的地方,有的话才绘制,没有就不绘制图形。

  1. void IconEditor::paintEvent(QPaintEvent *event)
  2. {
  3.     QPainter painter(this);
  4.     if (zoom >= 3) {
  5.         painter.setPen(palette().foreground().color());
  6.         for (int i = 0; i <= image.width(); ++i)
  7.             painter.drawLine(zoom * i, 0,
  8.                              zoom * i, zoom * image.height());
  9.         for (int j = 0; j <= image.height(); ++j)
  10.             painter.drawLine(0, zoom * j,
  11.                              zoom * image.width(), zoom * j);
  12.     }
  13.     for (int i = 0; i < image.width(); ++i) {
  14.         for (int j = 0; j < image.height(); ++j) {
  15.             QRect rect = pixelRect(i, j);
  16.        if (!event->region().intersect(rect).isEmpty()) {
  17.                 QColor color = QColor::fromRgba(image.pixel(i, j));
  18.                 if (color.alpha() < 255)
  19.                     painter.fillRect(rect, Qt::white);
  20.                 painter.fillRect(rect, color);
  21.             }
  22.         }
  23.     }
  24. }


离线liyoujun

只看该作者 2楼 发表于: 2012-11-02
顶一个。
快速回复
限100 字节
 
上一个 下一个