2011-06-14 117 views
1

我想使用qgraphicsview qgraphicsitem来创建一个象棋的场景。什么都不显示在QGraphicsScene

我正在试图创建它的官方示例,但没有任何显示。代码非常相似。首先,我想知道是我的Cell类。所以我只是试图画一个矩形。但没有任何显示。下面是我的代码,有人可以帮助我。我使用Qt 4.7在Windows 7

Cell.h

class Cell : public QGraphicsItem 
{ 
    //Q_OBJECT; 

public: 
    Cell(const QColor &color,int x, int y); 
    QRectF boundingRect() const; 
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); 
private: 
    int x,y; 
public: 
    QColor color; 
protected: 
    void mousePressEvent(QGraphicsSceneMouseEvent *event); 
}; 

Cell.cpp

Cell::Cell(const QColor &color, int x, int y) 
{ 
    this->x=x; 
    this->y=y; 
    this->color=color; 
    setAcceptedMouseButtons(Qt::LeftButton); 

} 

QRectF Cell::boundingRect() const 
{ 
    return QRectF(0,0,30,15); 
} 

void Cell::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 
{ 


    QBrush b=painter->brush(); 
    painter->setBrush(QColor::fromRgb(0,0,255)); 
    painter->drawRect(0,0,30,15); 
    painter->fillRect(this->boundingRect(),QColor::fromRgb(0,0,255)); 
    painter->setBrush(b); 
    return; 
} 

void Cell::mousePressEvent(QGraphicsSceneMouseEvent *event) 
{ 
    QGraphicsItem::mouseMoveEvent(event); 
    this->color=QColor::fromRgb(0,0,0); 
    update(); 

} 

view.h

class View : public QFrame 
{ 
    Q_OBJECT 

public: 
    QGraphicsView *getview() const; 

public: 
    View(QWidget *parent); 
private: 
    QGraphicsView *graphicsView; 
}; 

view.cpp

View::View(QWidget *parent) 
    :QFrame(parent) 
{ 
    graphicsView = new QGraphicsView; 
    graphicsView->setRenderHint(QPainter::Antialiasing, false); 
    graphicsView->setOptimizationFlags(QGraphicsView::DontSavePainterState); 
    graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate); 
} 

QGraphicsView *View::getview() const 
{ 
    return graphicsView; 
} 

mainwindow.h

class MainWindow : public QWidget 
{ 
    Q_OBJECT 

public: 
    explicit MainWindow(QWidget *parent = 0); 
    ~MainWindow(); 

private: 
    void populateScene(); 

    QGraphicsScene *scene; 
}; 

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) : 
    QWidget(parent) 
{ 
    populateScene(); 
    View *v=new View(0); 
    v->getview()->setScene(scene); 

    QHBoxLayout *layout = new QHBoxLayout; 
    layout->addWidget(v); 
    setLayout(layout); 
} 

MainWindow::~MainWindow() 
{ 
} 

void MainWindow::populateScene() 
{ 
    scene=new QGraphicsScene(); 
    for(int x=0;x<30;x++) 
    { 
     for(int y=0;y<20;y++) 
     { 
      QGraphicsItem *item=new Cell(QColor::fromRgb(0,255,255),30,15); 
      item->setPos(QPointF(30,15)); 
      scene->addItem(item); 
     } 
    } 
} 

的main.cpp

QApplication a(argc, argv); 
    MainWindow w; 
    w.show(); 

    return a.exec(); 

回答

2

你QFrame不 “拥有” 你的QGraphicsView。所以没有理由在其中显示视图。

只需更换

graphicsView = new QGraphicsView; 

有:

graphicsView = new QGraphicsView(this); 

而且不要忘了调整窗口的大小(或在代码中设置的最小尺寸),否则,你可能会认为它没't work =)

+0

非常感谢,它现在有效。 – 2011-06-14 09:22:29