Using the layout is ok but it's not needed in your case as you won't be doing any resizing or refitting the scene.
Here is something I crafted quickly:
#include <QtGui>
int main(int argc, char **argv){
view.setScene(&scene);
font.setFamily("monospace");
font.setPointSize(24);
int x = 0;
for(int i=0;i<text.size();i++){
item->setFont(font);
scene.addItem(item);
item->setPos(x, 0);
x += fm.width(item->text())+fm.rightBearing(ch)+10; // +10 for additional spacing
}
view.show();
return app.exec();
}
#include <QtGui>
int main(int argc, char **argv){
QApplication app(argc, argv);
QGraphicsView view;
QGraphicsScene scene;
view.setScene(&scene);
QString text = "Example text";
QFont font;
font.setFamily("monospace");
font.setPointSize(24);
QFontMetrics fm(font);
int x = 0;
for(int i=0;i<text.size();i++){
QChar ch = text.at(i);
QGraphicsSimpleTextItem *item = new QGraphicsSimpleTextItem;
item->setText(QString(ch));
item->setFont(font);
scene.addItem(item);
item->setPos(x, 0);
item->setFlag(QGraphicsItem::ItemIsSelectable);
x += fm.width(item->text())+fm.rightBearing(ch)+10; // +10 for additional spacing
}
view.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
You can control the distances between characters by adjusting x (and "y" if you want multiple rows).
Bookmarks