Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Exactly matching 1 tab to K spaces in QPlainTextEdit
Forum Updated to NodeBB v4.3 + New Features

Exactly matching 1 tab to K spaces in QPlainTextEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 179 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cadol001
    wrote on last edited by
    #1
    #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?

    jsulmJ 1 Reply Last reply
    0
    • C cadol001
      #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?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @cadol001 See https://doc.qt.io/qt-6/qplaintextedit.html#tabStopDistance-prop

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      JonBJ 1 Reply Last reply
      0
      • jsulmJ jsulm

        @cadol001 See https://doc.qt.io/qt-6/qplaintextedit.html#tabStopDistance-prop

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @jsulm
        ? The OP is trying to use that, tedit->setTabStopDistance(adv). Issue is (apparently) that his calculation of adv 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 the setWordSpacing() is irrelevant and can be removed? Does yours work for other spaces_per_tab values, e.g. 4 or 8, or is it always wrong? What about of you pick a different pointsize?

        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()) as ints, the latter as qreals (cf. QRect vs QRectF). Presumably if you change over to QFontMetricsF you will be good?

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved