Created
June 3, 2019 23:42
-
-
Save Atsushi4/fd0b42aa8f104afa1ca78306ad4f0c6e to your computer and use it in GitHub Desktop.
Qt-users:770 Sample code for tiling images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
QT += widgets | |
SOURCES += \ | |
image_tile.cpp | |
images.files += image/* | |
images.path = $$shadowed(image) | |
INSTALLS += images |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tested on Qt5.12 and Windows 10.