开发者

What Qt widgets to use to draw a "game-of-life"-like application?

开发者 https://www.devze.com 2023-01-15 07:16 出处:网络
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. Somethin

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.

I'm used to work with Qt but never for this kind of开发者_如何学运维 task.

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

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. Take a look at the Pixelator example.


For simple pixel and line drawing, you may want to implement a basic QWidget subclass and implement the paintEvent(). In there you would do your drawing

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:

#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

0

精彩评论

暂无评论...
验证码 换一张
取 消