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. Tabs inconsistent width in class inheriting from QPlainTextEdit

Tabs inconsistent width in class inheriting from QPlainTextEdit

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.9k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    Moschops
    wrote on last edited by
    #1

    I'm working with a class that inherits from QPlainTextEdit.

    Text is loaded into this object with the setPlainText function.

    Some of the lines of text contain tab characters. When I highlight the tabs, on some lines they are barely a few pixels wide, but on the very next line they can be as wide as two characters.

    I'd really like them to be a uniform width (ideally a multiple of the character width, as I've set a monospace font). I think the tabStopWidth isn't what I actually want; I don't want to control where the tab will jump to in even divisions across the page - I want a tab character to take up exactly <this many> characters, every time, no matter where it is.

    I tried to set them (in the constructor of the QWidget inheriting class) to a fixed width, like this:

    @ ui->setupUi(this);
    // editor is the class inheriting from QPlainTextEdit
    QFont font;
    font.setFamily("Courier");
    font.setStyleHint(QFont::Monospace);
    font.setFixedPitch(true);
    font.setPointSize(10);
    ui->editor->setFont(font);

    int tabStop = 4;  // 4 characters
    QFontMetrics metrics(ui->editor->font());
    ui->editor->setTabStopWidth(tabStop * metrics.width(' '));@
    

    but that didn't seem to do anything. None of the tabs look like they're four characters wide.

    Edit: And it also isn't monospacing :( I'm sure I must be missing something obvious; setting a monospace font and a constant tab width must be a pretty common demand.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Moschops
      wrote on last edited by
      #2

      I hate to BUMP, but this is causing me no end of trouble, sadly :(

      I need to place the cursor at a specified location, and the tabs don't even seem to count as a consistent number of characters wide, so when I see a tab in the text, it's impossible to know how many characters that counts as for using the QTextCursor::setPosition function.

      It seems ridiculous that I can't set the number of characters a tab takes up. Is that really the case?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        AFAIK, QPlainTextEdit does not implement what you need, it's optimized for plain text. You should rather be using QTextEdit

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Moschops
          wrote on last edited by
          #4

          Okey dokey. I have had a quick look at QTextEdit and to be honest, I don't see a function there that looks like it might do what I want, but I'd need to get into it a bit to be sure. I see setting tab stop width, but that's not the same thing as setting the width of a tab-character.

          1 Reply Last reply
          0

          • Login

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