简体   繁体   English

哪些Qt窗口小部件可用来绘制类似“生活游戏”的应用程序?

[英]What Qt widgets to use to draw a “game-of-life”-like application?

For an experiment, I'd like to create a simple graphical application. 为了进行实验,我想创建一个简单的图形应用程序。

My goal isn't complex: I just need to draw single pixels or lines of different colors, and refresh the view regularly. 我的目标并不复杂:我只需要绘制单个像素或不同颜色的线,并定期刷新视图即可。 Something like Conway's Game of Life . 像是Conway的《人生游戏》

I'm used to work with Qt but never for this kind of task. 我曾经和Qt一起工作过,但是从来没有做过这样的事情。

What widgets/objects should I use to get started ? 我应该使用哪些小部件/对象开始使用? Is there anything special I should know/do ? 我有什么特别的事情要知道吗?

Thank you. 谢谢。

I'd suggest the "graphics view" framework http://doc.trolltech.com/4.6/graphicsview.html 我建议使用“图形视图”框架http://doc.trolltech.com/4.6/graphicsview.html

It is extremely powerful, much more than you need it to be. 它非常强大,远远超出了您的需要。

Simply, for the creatures in the game of life, create graphics items and set the coordinates for them. 简单来说,对于生命游戏中的生物,创建图形项并为其设置坐标。 Nothing more. 而已。

Use a QTableView where you implement your own subclass of QAbstractItemDelegate to draw the cells. 使用QTableView在其中实现自己的QAbstractItemDelegate子类以绘制单元格。 Take a look at the Pixelator example . 看一下Pixelator示例

For simple pixel and line drawing, you may want to implement a basic QWidget subclass and implement the paintEvent(). 对于简单的像素和线条画,您可能需要实现基本的QWidget子类并实现paintEvent()。 In there you would do your drawing 在那儿你会画画

MyWidget.h: MyWidget.h:

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>

class MyWidget : public QWidget
{
    Q_OBJECT

    public:
        MyWidget(QWidget *parent = 0);
    protected:
        void paintEvent(QPaintEvent *event);
};

#endif

MyWidget.cpp: MyWidget.cpp:

#include <QtGui>

#include "MyWidget.h"

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
{
}

void MyWidget::MyWidget(QPaintEvent * /* event */)
{
    QPainter painter(this);

    // Then do things like..
    painter.drawLine(...
    painter.drawRect(...
}

You can find a more complete example here: http://doc.qt.nokia.com/4.6/painting-basicdrawing.html 您可以在此处找到更完整的示例: http : //doc.qt.nokia.com/4.6/painting-basicdrawing.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM