• 7657阅读
  • 3回复

继承QGraphicsView后无法显示item???【结贴】 [复制链接]

上一主题 下一主题
离线rockyluo
 

只看楼主 倒序阅读 楼主  发表于: 2010-05-06
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
写了个GraphicsView类继承自QGraphicsView
.h文件:
  1. class GraphicsView : public QGraphicsView
  2. {
  3.     Q_OBJECT
  4. public:
  5.     GraphicsView ( GraphicsScene * scene, QWidget * parent = 0 );
  6. protected:
  7.     virtual void paintEvent( QPaintEvent * event );
  8. };


.cpp文件:
  1. GraphicsView::GraphicsView(GraphicsScene * scene, QWidget * parent)
  2.     : QGraphicsView(scene,parent)
  3. {
  4.     this->setScene(scene);
  5. }
  6. void GraphicsView::paintEvent(QPaintEvent *event)
  7. {
  8.     qDebug()<<"View paintEvent...";
  9. }


后在程序中使用GraphicsView却始终无法将item显示出来
  1. GraphicsScene *scene = new GraphicsScene();
  2.     scene->setSceneRect(0, 0, 600, 400);
  3.     GraphicsView *view = new GraphicsView(scene, this);
  4.     view->setRenderHint(QPainter::Antialiasing);
  5.     view->resize(600,400);
  6.     view->move(50,50);
  7.     GraphicsPixmapItem *item1 = new GraphicsPixmapItem(QPixmap(":/girl.bmp"));  
  8.     item1->setPos(100,0);
  9.     scene->addItem(item1);
  10.     view->show();


     // GraphicsScene类继承自QGraphicsScene
     // 用GraphicsScene无法显示item1,而直接用QGraphicsScene可以显示item1
何原因???
[ 此帖被rockyluo在2010-05-06 17:49重新编辑 ]
                                                                                                              我们要团结一心,秉承Linux的开源精神,共享Qt问题的解决方法!
离线beajisong

只看该作者 1楼 发表于: 2010-05-06
自己没画自己
离线rockyluo

只看该作者 2楼 发表于: 2010-05-06
Re:继承QGraphicsView后无法显示item???【结贴】
引用第1楼beajisong于2010-05-06 17:02发表的  :
自己没画自己


啊 谢谢提醒, 极其的感谢!!!
改了后出来了
  1. void GraphicsView::paintEvent(QPaintEvent *event)
  2. {
  3.     qDebug()<<"View paintEvent...";
  4.     QGraphicsView::paintEvent(event);
  5. }


再次谢谢beajisong!
                                                                                                              我们要团结一心,秉承Linux的开源精神,共享Qt问题的解决方法!
离线rockyluo

只看该作者 3楼 发表于: 2010-05-06
另外,多句嘴....
本来在paintEvent里头用QPainter画东西的,也是半天没搞出来,
  1. void GraphicsView::paintEvent(QPaintEvent *event)
  2. {
  3.     qDebug()<<"View paintEvent...";
  4.     QGraphicsView::paintEvent(event);
  5.     QPainter painter(this);
  6.     painter.setRenderHint(QPainter::Antialiasing);
  7.     painter.setPen(QPen(Qt::red,3,Qt::DotLine));
  8.     painter.drawLine(QPoint(0,0),QPoint(0,100));
  9. }


运行出错信息如下:
QPainter::begin: Paint device returned engine == 0, type: 1
此问题乃是当前的this为一个非激活状态的绘图设备,导致无法为QPainter提供绘图引擎

解决方法如下:
  1.     ...
  2.     QPainter painter(this->viewport());
  3.     ...

将当前可见视口作为绘图引擎
这样就能正确画线了
                                                                                                              我们要团结一心,秉承Linux的开源精神,共享Qt问题的解决方法!
快速回复
限100 字节
 
上一个 下一个