QGraphicsScene 中不显示任何内容

发布于 2024-11-15 05:30:25 字数 3004 浏览 2 评论 0原文

我正在尝试使用 qgraphicsview qgraphicsitem 创建一个像国际象棋一样的场景。

我正在按照官方示例尝试创建它,但没有任何显示。代码非常相似。首先,我想知道我的细胞课程。所以我只是尝试画一个矩形。但没有任何显示。下面是我的代码,有人可以帮助我吗?我在 Windows 7 上使用 Qt 4.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();

I am trying to use the qgraphicsview qgraphicsitem to create a scene like a chess.

I am following the Official example trying to create it, but there is nothing displaying. The code is pretty the same. At the first, I am wondering is my Cell class. So I just try to draw a rect. But there is nothing displaying. Below is a my code, could someone help me out. I am using the Qt 4.7 on 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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏佲洛 2024-11-22 05:30:25

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

只需替换

graphicsView = new QGraphicsView;

为 :

graphicsView = new QGraphicsView(this);

并且不要忘记调整窗口大小(或在代码中设置最小大小),否则,您可能会认为它不起作用 =)

Your QFrame does not "own" your QGraphicsView. So there is no reason for it to display the view inside it.

Just replace

graphicsView = new QGraphicsView;

with :

graphicsView = new QGraphicsView(this);

And don't forget to resize the window, (or set in the code a minimum size) otherwise, you may think that it didn't work =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文