PDA

View Full Version : Seeking suggestions for a grid-based ASCII character layout



Quasimojo
1st April 2012, 19:12
I am trying to lay out a Qt form as the UI for an ASCII character-based RPG. The UI elements will be arranged around a display region on the form. I would like a grid (perhaps 64x64) where each grid cell could display either an ASCII character or a sprite of some sort. I need the contents of each cell to fill the entire cell (no margins or padding). I would like the grid layout and its contents to expand/shrink as the form is re-sized.

My first thought was to use a grid layout widget and populate it with a 64x64 grid of label controls. However, I can't seem to get simple text characters to scale properly - expanding to the edges of the cell, while not causing the size of the grid layout to change.

I would appreciate suggestions on the best approach to accomplish this. Perhaps a grid layout widget isn't the best way to go?

Thanks.

wysota
1st April 2012, 22:26
Use QGraphicsView or QtQuick.

Quasimojo
2nd April 2012, 00:14
Use QGraphicsView or QtQuick.

Maybe the method of using the QGraphicsView to accomplish this just isn't apparent to me, but I don't see how that would produce the desired result. What I basically want is a grid of labels, the contents of which would each expand and shrink as the form is resized, maintaining the aspect ratio of the label contents. I think I've decided to go with a pixmap approach, rather than ASCII characters, but I still need to determine how to a) make the pics automatically resize to fit within the grid cells, and b) make them automatically resize to maintain their aspect ratio as the form is resized.

Thanks.

wysota
2nd April 2012, 09:32
Maybe the method of using the QGraphicsView to accomplish this just isn't apparent to me, but I don't see how that would produce the desired result.


#include <QtGui>

class View : public QGraphicsView {
public:
View(QWidget *parent = 0) : QGraphicsView(parent) {}
protected:
void resizeEvent(QResizeEvent *e) {
QGraphicsView::resizeEvent(e);
fitInView(sceneRect(), Qt::KeepAspectRatio);
}
};

int main(int argc, char**argv) {
QApplication app(argc, argv);
View view;
QGraphicsScene scene;
scene.setSceneRect(0,0,100,100);
QFont f = view.font();
f.setPointSize(3);
for(int r = 0; r < 10; ++r) {
for(int c = 0; c < 10; ++c) {
QGraphicsSimpleTextItem *item = scene.addSimpleText(QString("%1-%2").arg(r).arg(c), f);
item->setPos(c*10,r*10);
}
}
view.setScene(&scene);
view.resize(600,400);
view.setRenderHints(QPainter::Antialiasing|QPainte r::TextAntialiasing);
view.show();
return app.exec();
}