2

I'm trying to write a map editor in Qt, using QGraphicsView and QGraphicsScene for both the map and tile sheets.

The problem I'm having right now is with making a good widget for importing tiles. For this, I'm using a QTabWidget (for different tile sheets), and TileWidget as the widget for each tab, which contains the QGraphicsScene and QGraphicsView.

It's working to a rough degree, but not all the tiles (or TileObjects, which are implementations of QGraphicsItem) are visible. I'm even calling view->ensureVisible(scene->sceneRect()), but still not all of the QGraphicsScene is not visible, even with scroll bars.

I understand this is due to limiting the maximum size of my QTabWidget, but that is necessary.

This happens mainly when I import a larger tile sheet.

I have a TileWidget as the QWidget for the QTabWidget, which has both the QGraphicsScene and the QGraphicsView.

TileWidget::TileWidget(QWidget *parent)
 : QWidget(parent)
{
    scene = new QGraphicsScene;
    view = new TileView(scene, this);

    connect(view, SIGNAL(newBrushSelected(TileObject *b)), this, SLOT(selectNewBrush(TileObject *b)));
}

TileView is simply a QGraphicsView re-implemented to handle mouse release events.

To add tiles, I simply call scene->addItem().

I have no other code for TileView. When I use

void TileWidget::showEvent(QShowEvent *event)
{
    view->fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio);
}

I get something like this.

enter image description here

It's okay for smaller tile sheets, but not for larger ones. What should I add to keep the size of the tiles normal, and navigate TileView using scroll bars?

Nevermind, figured it out. Just me being stupid.

Rikonator
  • 1,830
  • 16
  • 27

1 Answers1

3

You need something like:

p_myGraphicsView->fitInView(
    myGraphicsView->scene()->itemsBoundingRect(),
    Qt::KeepAspectRatio);
Samuel Harmer
  • 4,264
  • 5
  • 33
  • 67
  • That is the documented way of doing what you want. Of course, that only works if the implementation of `QGraphicsItem::boundingRect()` is correct. I'm afraid the site hosting your image is blocked for me at work so I will take a look later if no one else answers in the mean time. – Samuel Harmer Apr 02 '12 at 12:51
  • I realized I was implementing your code incorrectly. I was calling it in the constructor, when no items had been added. But, calling it after adding all the items does not help either. It scales down all the tiles into a tiny little corner. I tried to use `setSceneRect()` after adding all the items. With it, all tiles horizontally are visible but I still can't scroll all the way down. Do you need me to post some code? Maybe I'm implementing it completely wrong. – Rikonator Apr 02 '12 at 13:22
  • @Rikonator, the code shouldn't go in the constructor. One of the nuances of QGraphicsView is that you can't do a whole lot with it until _after_ it's shown (it hasn't been shown yet by the end of the constructor). Try reimplementing QWidget::showEvent(), then putting Styne666's code in there. – Anthony Apr 02 '12 at 17:08
  • To clarify, I don't mean to reimplement QGraphicsView::showEvent() (although that might work, I don't know...), I mean to reimplement QDialog::showEvent(), or whatever your window is. – Anthony Apr 02 '12 at 17:09
  • t works for smaller tile sheets. But, for larger ones it tries to fit the scene in the widget, so the tile sheet is scaled down. I tried to set the maximum size of the `viewport()` for `QGraphicsView`, but I am still unable to scroll through the entire scene for larger tile sheets. Does this have something more to do with `QAbstractScrollArea` of `QGraphicsView`? – Rikonator Apr 03 '12 at 12:47