Skip to content

Instantly share code, notes, and snippets.

@Atsushi4
Created June 3, 2019 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Atsushi4/fd0b42aa8f104afa1ca78306ad4f0c6e to your computer and use it in GitHub Desktop.
Save Atsushi4/fd0b42aa8f104afa1ca78306ad4f0c6e to your computer and use it in GitHub Desktop.
Qt-users:770 Sample code for tiling images
#include <QtWidgets/QApplication>
#include <QtWidgets/QGraphicsScene>
#include <QtWidgets/QGraphicsView>
#include <QtGui/QPixmap>
#include <QtWidgets/QGraphicsPixmapItem>
#include <QtCore/QDebug>
namespace {
static constexpr int FilePathKey = 1;
void func1();
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
func1();
return app.exec();
}
namespace {
void func1() {
auto view = new QGraphicsView{};
auto scene = new QGraphicsScene{qApp};
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
auto pixMap = QPixmap(64, 64);
auto filePath = QString("image/%1_%2.png").arg(i).arg(j);
pixMap.load(filePath);
auto item = scene->addPixmap(pixMap);
item->setFlag(QGraphicsItem::ItemIsFocusable);
item->setOffset(70 * i, 70 * j);
item->setData(FilePathKey, filePath);
}
}
view->setScene(scene);
QObject::connect(scene, &QGraphicsScene::focusItemChanged,
qApp, [](QGraphicsItem *newItem, QGraphicsItem *oldItem, Qt::FocusReason reason){
qDebug() << (newItem ? newItem->data(FilePathKey).toString() : QString{})
<< (oldItem ? oldItem->data(FilePathKey).toString() : QString{})
<< reason;
});
view->show();
}
}
QT += widgets
SOURCES += \
image_tile.cpp
images.files += image/*
images.path = $$shadowed(image)
INSTALLS += images
@Atsushi4
Copy link
Author

Atsushi4 commented Jun 4, 2019

I tested on Qt5.12 and Windows 10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment