Beginner: Formatting text output (PyQt4)

G

Glen

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.setFontFixedPitch(True)
ui.textEdit.setCurrentCharFormat(qTxtFormat)

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

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

Any suggestions?

Thanks,

Glen
 
G

Glen

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.setFontFixedPitch(True)
ui.textEdit.setCurrentCharFormat(qTxtFormat)

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

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

Any suggestions?

Thanks,

Glen
 
T

Tina I

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.setFontFixedPitch(True)
ui.textEdit.setCurrentCharFormat(qTxtFormat)

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

qStr = QtCore.QString( QtCore.QString( str(tL2[0])
).rightJustified(2) + ':' + QtCore.QString( str(tL2[1])
).rightJustified(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
 
J

Jeremy Sanders

Glen said:
What seems to be happening is that the font that pyqt is using is not
fixed width, so I did this:
qTxtFormat = QTextCharFormat()
qTxtFormat.setFontFixedPitch(True)
ui.textEdit.setCurrentCharFormat(qTxtFormat)

Does something like ui.textEdit.setCurrentFont(QFont('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
 
D

David Boddie

What seems to be happening is that the font that pyqt is using is not
fixed width, so I did this:
qTxtFormat = QTextCharFormat()
qTxtFormat.setFontFixedPitch(True)
ui.textEdit.setCurrentCharFormat(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
 
G

Glen

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
 
S

Steve Holden

Glen said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top