PDA

View Full Version : Help message box



ChrisOfBristol
1st April 2015, 11:20
I want to load and display a text file when help is clicked. It needs to be a fair-sized panel with a vertical scrollbar, the text should preferably but not necessarily be HTML and loaded from a disk file. The best I can do so far is this:

with open(filename, 'r') as infile:
result = infile.read()
self.helptext = QtGui.QTextEdit()
self.helptext.setHtml(result)
self.helptext.setReadOnly(-1)
self.helptext.resize(800, 500)
self.helptext.show()
However, it floats on the main window and does not close if I close the main window. (Not modal?) I can make it attach to the main window and close when the main window is closed (modal?) by using QtGui.QTextEdit(self). This is not satisfactory though, as there is no 'X' on the window title bar (window decoration?) so no way of closing it. I have looked here http://pyqt.sourceforge.net/Docs/PyQt4/qtextedit.htmls for a way of adding it without success. Is there a way of doing so, or should I be using a different sort of widget? I have looked at all the widgets I can find but can't find what I want, a messagebox is not suitable for several reasons.

anda_skoa
1st April 2015, 14:56
A modal dialog is a dialog that prevents user interaction with the dialog's parent window.
E.g. an error message box that needs to be acknowledged with OK.

If you want that simply create QDialog subclass and put your content widget in that.

Cheers,
_

ChrisOfBristol
2nd April 2015, 11:50
That made sense to me and was a good pointer in the right direction, I've come up with this now:

def actionHelpTriggered(self):
helpPanel = QtGui.QDialog()
helpPanel.setModal(True)
###helpPanel.setSizeGripEnabled(False) #### doesn't work, it doesn't stop dialog panel being stretched bigger.
helpPanel.setWindowTitle('Help')
self.helptext = QtGui.QTextEdit(helpPanel)
with open('help.html', 'r') as infile:
result = infile.read()
self.helptext.setHtml(result)
self.helptext.setReadOnly(-1)
self.helptext.resize(800, 500)
helpPanel.show()
helpPanel.helptext.show()

It does what I want, in that the dialog is modal with a title bar with an 'X' and the textEdit on top of it and a vertical slider. The dialog starts at the same size as the textEdit panel.

...in actionHelpTriggered
helpPanel.helptext.show()
AttributeError: 'QDialog' object has no attribute 'helptext'

I don't know why I get this as the last two lines seem to work, and without them nothing shows.

anda_skoa
2nd April 2015, 12:22
The error is from line 13 in your snippet. QDialog does not have a member named "helptext".

What you want instead of creating a dialog and then creating a text edit is to create a new class derived from QDialog that creates the text edit in its constructor.
I.e. when using this dialog you only have to deal with one object.

You might also want to call helpPanel.exec() instead of setting window modality and calling show, but you'll have to check if this is still the behavior you want.

Cheers,
_

ChrisOfBristol
2nd April 2015, 15:30
helptext.show()has the same effect - it causes a warning, so it's odd that it works.

...QDialog that creates the text edit in its constructor...is completely beyond my level of understanding. I'm going to stick with what I've got because at least it works.