Beginner: Formatting text output (PyQt4)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Glen

    #1

    Beginner: Formatting text output (PyQt4)

    Hello again, I don't blame anyone for not answering my last post, since I
    obviously hadn't spent much time researching, but I've come a little ways
    and have another question.

    How can I better format text output to a QTextEdit object? I'm inserting
    5 columns into each row. When I write the info to a file, it looks like
    the following:

    42: 115 26: 114 35: 112 19: 108 16: 107
    45: 107 40: 106 5: 105 41: 104 2: 103
    9: 102 48: 102 15: 101 22: 101 27: 101
    39: 101 43: 101 10: 100 6: 99 34: 99
    32: 98 49: 98 20: 97 30: 97 8: 96
    17: 96 38: 96 12: 95 14: 95 37: 95
    4: 94 13: 94 44: 94 36: 93 3: 92
    24: 92 28: 92 31: 91 29: 89 7: 88
    1: 87 18: 85 46: 85 33: 84 11: 83
    23: 83 47: 82 25: 80 21: 79 50: 56
    52: 39 51: 38 53: 36 54: 25 55: 18

    When I write the contents of the file to my TextEdit object it comes out
    uneven, something like this:
    42: 115 26: 114 35: 112 19: 108 16: 107
    45: 107 40: 106 5: 105 41: 104 2: 103
    9: 102 48: 102 15: 101 22: 101 27: 101
    39: 101 43: 101 10: 100 6: 99 34: 99
    32: 98 49: 98 20: 97 30: 97 8: 96
    17: 96 38: 96 12: 95 14: 95 37: 95
    4: 94 13: 94 44: 94 36: 93 3: 92
    24: 92 28: 92 31: 91 29: 89 7: 88
    1: 87 18: 85 46: 85 33: 84 11: 83
    23: 83 47: 82 25: 80 21: 79 50: 56
    52: 39 51: 38 53: 36 54: 25 55: 18

    What seems to be happening is that the font that pyqt is using is not
    fixed width, so I did this:
    qTxtFormat = QTextCharFormat ()
    qTxtFormat.setF ontFixedPitch(T rue)
    ui.textEdit.set CurrentCharForm at(qTxtFormat)

    Also, I tried using the pyqt formatting such as the following:

    qStr = QtCore.QString( QtCore.QString( str(tL2[i][0]) ).rightJustifie d(2)
    + ':' + QtCore.QString( str(tL2[i][1]) ).rightJustifie d(4) )
    This still gives me uneven columns.

    Any suggestions?

    Thanks,

    Glen

  • Glen

    #2
    Re: Beginner: Formatting text output (PyQt4)

    On Wed, 18 Apr 2007 22:50:14 +0000, Glen wrote:

    Ok, obviously, my post didn't come out the way I wanted it to. In the
    first example from my text file below, the dictionary pairs, constructed
    from sorted tuples were in straight columns. When I write them to my
    QTextEdit, however, the columns are no longer straight. Some of the
    padding inserted by my call to rightJustified( ) are not equal to the
    actual size of the text, so some rows are longer than others in the text
    browser. I know I've encountered this before, perhaps with Java, but I
    can't remember the solution. How do I get the formatting of my output to
    the gui to reflect the output I would get to a file using the builtin
    write() function? Any help is appreciated. Sorry about the confusion.

    Thanks,
    G
    Hello again, I don't blame anyone for not answering my last post,
    since
    I obviously hadn't spent much time researching, but I've come a little
    ways and have another question.
    >
    How can I better format text output to a QTextEdit object? I'm
    inserting 5 columns into each row. When I write the info to a file, it
    looks like the following:
    >
    42: 115 26: 114 35: 112 19: 108 16: 107 45: 107 40:
    106 5: 105 41: 104 2: 103
    9: 102 48: 102 15: 101 22: 101 27: 101
    39: 101 43: 101 10: 100 6: 99 34: 99 32: 98 49:
    98 20: 97 30: 97 8: 96 17: 96 38: 96 12: 95
    14: 95 37: 95
    4: 94 13: 94 44: 94 36: 93 3: 92
    24: 92 28: 92 31: 91 29: 89 7: 88
    1: 87 18: 85 46: 85 33: 84 11: 83
    23: 83 47: 82 25: 80 21: 79 50: 56 52: 39 51:
    38 53: 36 54: 25 55: 18
    >
    When I write the contents of the file to my TextEdit object it comes out
    uneven, something like this:
    42: 115 26: 114 35: 112 19: 108 16: 107 45: 107 40: 106
    5: 105 41: 104 2: 103 9: 102 48: 102 15: 101 22: 101 27:
    101 39: 101 43: 101 10: 100 6: 99 34: 99 32: 98 49: 98
    20: 97 30: 97 8: 96 17: 96 38: 96 12: 95 14: 95
    37: 95 4: 94 13: 94 44: 94 36: 93 3: 92 24: 92 28:
    92 31: 91 29: 89 7: 88
    1: 87 18: 85 46: 85 33: 84 11: 83
    23: 83 47: 82 25: 80 21: 79 50: 56 52: 39 51: 38
    53: 36 54: 25 55: 18
    >
    What seems to be happening is that the font that pyqt is using is not
    fixed width, so I did this:
    qTxtFormat = QTextCharFormat ()
    qTxtFormat.setF ontFixedPitch(T rue)
    ui.textEdit.set CurrentCharForm at(qTxtFormat)
    >
    Also, I tried using the pyqt formatting such as the following:
    >
    qStr = QtCore.QString( QtCore.QString( str(tL2[i][0])
    ).rightJustifie d(2) + ':' + QtCore.QString( str(tL2[i][1])
    ).rightJustifie d(4) )
    This still gives me uneven columns.
    >
    Any suggestions?
    >
    Thanks,
    >
    Glen

    Comment

    • Tina I

      #3
      Re: Beginner: Formatting text output (PyQt4)

      Glen wrote:

      >Hello again, I don't blame anyone for not answering my last post,
      since
      >I obviously hadn't spent much time researching, but I've come a little
      >ways and have another question.
      >>
      >How can I better format text output to a QTextEdit object? I'm
      >inserting 5 columns into each row. When I write the info to a file, it
      >looks like the following:
      >>
      >42: 115 26: 114 35: 112 19: 108 16: 107 45: 107 40:
      >106 5: 105 41: 104 2: 103
      > 9: 102 48: 102 15: 101 22: 101 27: 101
      >39: 101 43: 101 10: 100 6: 99 34: 99 32: 98 49:
      >98 20: 97 30: 97 8: 96 17: 96 38: 96 12: 95
      >14: 95 37: 95
      > 4: 94 13: 94 44: 94 36: 93 3: 92
      >24: 92 28: 92 31: 91 29: 89 7: 88
      > 1: 87 18: 85 46: 85 33: 84 11: 83
      >23: 83 47: 82 25: 80 21: 79 50: 56 52: 39 51:
      >38 53: 36 54: 25 55: 18
      >>
      >When I write the contents of the file to my TextEdit object it comes out
      >uneven, something like this:
      >42: 115 26: 114 35: 112 19: 108 16: 107 45: 107 40: 106
      >5: 105 41: 104 2: 103 9: 102 48: 102 15: 101 22: 101 27:
      >101 39: 101 43: 101 10: 100 6: 99 34: 99 32: 98 49: 98
      > 20: 97 30: 97 8: 96 17: 96 38: 96 12: 95 14: 95
      > 37: 95 4: 94 13: 94 44: 94 36: 93 3: 92 24: 92 28:
      >92 31: 91 29: 89 7: 88
      > 1: 87 18: 85 46: 85 33: 84 11: 83
      >23: 83 47: 82 25: 80 21: 79 50: 56 52: 39 51: 38
      >53: 36 54: 25 55: 18
      >>
      >What seems to be happening is that the font that pyqt is using is not
      >fixed width, so I did this:
      > qTxtFormat = QTextCharFormat ()
      > qTxtFormat.setF ontFixedPitch(T rue)
      > ui.textEdit.set CurrentCharForm at(qTxtFormat)
      >>
      >Also, I tried using the pyqt formatting such as the following:
      >>
      > qStr = QtCore.QString( QtCore.QString( str(tL2[i][0])
      > ).rightJustifie d(2) + ':' + QtCore.QString( str(tL2[i][1])
      > ).rightJustifie d(4) )
      >This still gives me uneven columns.
      >>
      >Any suggestions?
      >>
      >Thanks,
      >>
      >Glen
      >
      Do you need to use QTextEdit for the output? Sounds like maybe you
      should look at for example the QTableWidget or maybe the QTreeWidget?

      Tina

      Comment

      • Jeremy Sanders

        #4
        Re: Beginner: Formatting text output (PyQt4)

        Glen wrote:
        What seems to be happening is that the font that pyqt is using is not
        fixed width, so I did this:
        qTxtFormat = QTextCharFormat ()
        qTxtFormat.setF ontFixedPitch(T rue)
        ui.textEdit.set CurrentCharForm at(qTxtFormat)
        Does something like ui.textEdit.set CurrentFont(QFo nt('fixed')) work? It
        seems to work for me if you use plain text.

        Tabs or html/rich text formatting should be a better way to get the layout
        (or just use a table).

        Jeremy

        --
        Jeremy Sanders

        Comment

        • David Boddie

          #5
          Re: Beginner: Formatting text output (PyQt4)

          On Thursday 19 April 2007 00:50, Glen wrote:
          What seems to be happening is that the font that pyqt is using is not
          fixed width, so I did this:
          qTxtFormat = QTextCharFormat ()
          qTxtFormat.setF ontFixedPitch(T rue)
          ui.textEdit.set CurrentCharForm at(qTxtFormat)
          It may be that the font you're using isn't actually a monospaced font.
          The result of this is that the font used to display the text won't be
          a fixed pitch font.

          To test this, try specifying a known monospaced font and see if you get
          the output you expect.

          David

          Comment

          • Glen

            #6
            Re: Beginner: Formatting text output (PyQt4) Solved

            Sorry I've been away a while, figuring stuff out. It absolutely wasn't a
            monospaced font that I was using. I'm still getting used to Qt Designer
            and didn't see the expandable item under 'font' where I could easily
            select another font. I felt pretty stupid when I saw it. My program
            output the contents of my data file perfectly once I selected a fixed
            pitch font.

            Thanks a lot for the replies. I didn't know how long to wait, since I'm
            new around here, and I kind of gave up. I got some good info from you
            guys anyway. Thanks. You'll probably be hearing from me pretty
            regularly, since I'm in the process of learning Python and PyQt
            simultaneously.

            Glen

            Comment

            • Steve Holden

              #7
              Re: Beginner: Formatting text output (PyQt4) Solved

              Glen wrote:
              Sorry I've been away a while, figuring stuff out. It absolutely wasn't a
              monospaced font that I was using. I'm still getting used to Qt Designer
              and didn't see the expandable item under 'font' where I could easily
              select another font. I felt pretty stupid when I saw it. My program
              output the contents of my data file perfectly once I selected a fixed
              pitch font.
              >
              Thanks a lot for the replies. I didn't know how long to wait, since I'm
              new around here, and I kind of gave up. I got some good info from you
              guys anyway. Thanks. You'll probably be hearing from me pretty
              regularly, since I'm in the process of learning Python and PyQt
              simultaneously.
              >
              Welcome to c.l.py!

              regards
              Steve
              --
              Steve Holden +1 571 484 6266 +1 800 494 3119
              Holden Web LLC/Ltd http://www.holdenweb.com
              Skype: holdenweb http://del.icio.us/steve.holden
              Recent Ramblings http://holdenweb.blogspot.com

              Comment

              Working...