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. [solved] Centering a QTextFrame
Forum Update on Monday, May 27th 2025

[solved] Centering a QTextFrame

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 822 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.
  • P Offline
    P Offline
    Pt_develop
    wrote on last edited by
    #1

    I'm trying to accomplish something with QTextEdit that may not be possible but perhaps someone has an idea that doesn't involve creating my own layout manager.

    My goal is to find a way of horizontally centering a fixed width QTextFrame within it's parent QTextFrame. In the simplest form the parent frame would be the the associated QTextDocument's rootFrame. Please note that I am not trying to center the text in the fixed width QTextFrame (that's easy). I'm trying to center the frame itself. This appears to be equivalent to having dynamic left and right margins that keep the frame centered with respect to its parent.

    I've tried several approaches but none of them have worked. I'm wondering if adding a dummy empty left and right floating frames (with frame format positions set to QTextFrameFormat::FloatLeft and QTextFrameFormat::FloatRight) bracketing the actual frame of interest would work. If someone has tried this approach I'd appreciate hearing from you.

    Or perhaps there is a simple way to accomplish my goal that I've overlooked. Anyone have any ideas?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Pt_develop
      wrote on last edited by
      #2

      Well, I think I've solved the problem; at least all my tests seem to work now. The idea is to not use a QTextFrame directly but instead use a 1 by 3 table as a surrogate for formatting purposes. The table's column 0 and 2 cells remain empty and variable while the middle column 1 cell becomes the surrogate QTextFrame. Setting the tables QTextTableFormat alignment to Qt:AlignHCenter then produces the desired result as it appears that the middle table cell will act just like a QTextFrame for most purposes.

      Actually this solution appears more general than my original problem since it centers a QTextFrame or block independent of whether or not it is of fixed width.

      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