Exactly matching 1 tab to K spaces in QPlainTextEdit
-
#include <QPlainTextEdit> #include <QVBoxLayout> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QVBoxLayout* layout = new QVBoxLayout{}; QPlainTextEdit* tedit = new QPlainTextEdit{this}; layout->addWidget(tedit); ui->centralwidget->setLayout(layout); QTextOption opt; opt.setWrapMode(QTextOption::NoWrap); opt.setFlags(QTextOption::ShowTabsAndSpaces); tedit->document()->setDefaultTextOption(opt); int N = 100; int pointsize = 18; int spaces_per_tab = 2; QTextCursor c = tedit->textCursor(); c.insertText(QString("\t").repeated(N) + "\n"); c.insertText(QString(" ").repeated(N*spaces_per_tab)); QFont f = QFont{}; f.setPointSize(pointsize); f.setWordSpacing(pointsize * 0.75); tedit->setFont(f); QFontMetrics fmt{f}; int adv = fmt.horizontalAdvance(" ")*spaces_per_tab; tedit->setTabStopDistance(adv); }
I am trying to make a tab equal to
spaces_per_tab
spaces in q QPlainTextEdit. However, if you run this code you will see that the estimated horizontalAdvance is slightly off such that 2 spaces is not at all equal to 1 tab.How do I get the actual width of a space?
-
#include <QPlainTextEdit> #include <QVBoxLayout> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QVBoxLayout* layout = new QVBoxLayout{}; QPlainTextEdit* tedit = new QPlainTextEdit{this}; layout->addWidget(tedit); ui->centralwidget->setLayout(layout); QTextOption opt; opt.setWrapMode(QTextOption::NoWrap); opt.setFlags(QTextOption::ShowTabsAndSpaces); tedit->document()->setDefaultTextOption(opt); int N = 100; int pointsize = 18; int spaces_per_tab = 2; QTextCursor c = tedit->textCursor(); c.insertText(QString("\t").repeated(N) + "\n"); c.insertText(QString(" ").repeated(N*spaces_per_tab)); QFont f = QFont{}; f.setPointSize(pointsize); f.setWordSpacing(pointsize * 0.75); tedit->setFont(f); QFontMetrics fmt{f}; int adv = fmt.horizontalAdvance(" ")*spaces_per_tab; tedit->setTabStopDistance(adv); }
I am trying to make a tab equal to
spaces_per_tab
spaces in q QPlainTextEdit. However, if you run this code you will see that the estimated horizontalAdvance is slightly off such that 2 spaces is not at all equal to 1 tab.How do I get the actual width of a space?
-
@jsulm
? The OP is trying to use that,tedit->setTabStopDistance(adv)
. Issue is (apparently) that his calculation ofadv
is not right, either in general or at least for 2 spaces, and he wants to know why.@cadol001
Does your code do same as claimed solution at https://stackoverflow.com/a/50192868/489865 ? It does seems to be. I assume thesetWordSpacing()
is irrelevant and can be removed? Does yours work for otherspaces_per_tab
values, e.g. 4 or 8, or is it always wrong? What about of you pick a differentpointsize
?QFontMetrics fmt{f};
Ah ha! You use QFontMetrics. Example uses QFontMetricsF. That should be why your figures are wrong? The former returns stuff (including
horizontalAdvance()
) asint
s, the latter asqreal
s (cf.QRect
vsQRectF
). Presumably if you change over toQFontMetricsF
you will be good?