Skip to content

Instantly share code, notes, and snippets.

@pauljurczak
Last active September 10, 2015 08:48
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 pauljurczak/2b6ba7987e80987591e5 to your computer and use it in GitHub Desktop.
Save pauljurczak/2b6ba7987e80987591e5 to your computer and use it in GitHub Desktop.
Fractional pixel size with Qt
#include <QApplication>
#include <QGraphicsScene>
#include <QLabel>
#include <QBrush>
#include <QGraphicsSimpleTextItem>
#include <QColor>
#include <QGraphicsView>
using namespace std;
int main(int argc, char *argv[])
{
const int scale = 44;
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
QImage image("test.bmp");
scene.addPixmap(QPixmap::fromImage(image.scaled(image.width(), image.height())));
for (int x = 0; x < image.width(); ++x)
for (int y = 0; y < image.height(); ++y) {
auto text = new QGraphicsSimpleTextItem();
text->setText(QString::number(qGray(image.pixel(x, y))));
text->setBrush(QBrush(Qt::red));
text->setFlag(QGraphicsItem::ItemIgnoresTransformations,true);
text->setPos(view.mapFromScene(x+0.2, y+0.2));
scene.addItem(text);
}
view.scale(scale, scale);
view.show();
return a.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment