python - Creating a 'Page View' style layout for a rich text editor with the scroll-bar aligned to the right edge -
i'm trying create rich-text editor layout similar microsoft word's 'page view' or 'print layout.' i'd have qtextedit horizontally centered in main window, scroll-bar aligned against far right border of main window.
i couldn't find way move qtextedit's default scroll-bar independent of qtextedit itself. instead, tried creating separate scroll-bar, , making qtextedit grow vertically using solution found here: a qwidget qtextedit wraps height automatically contents?
here attempt:
import sys pyside import qtgui, qtcore class mainwindow(qtgui.qmainwindow): def __init__(self): super(mainwindow, self).__init__() self.initui() def initui(self): cw = centralwidget() self.setcentralwidget(cw) self.setgeometry(200, 200, 1000, 600) self.show() def resizeevent(self, event): self.centralwidget().setfixedheight(event.size().height()) class centralwidget(qtgui.qwidget): def __init__(self): super(centralwidget, self).__init__() self.initui() def initui(self): text = maintextedit() text.setminimumwidth(850) text.setstylesheet('border: 0;') pagewidget = qtgui.qwidget() scroll = qtgui.qscrollarea() scroll.setverticalscrollbarpolicy(qtcore.qt.scrollbaralwayson) scroll.sethorizontalscrollbarpolicy(qtcore.qt.scrollbaralwaysoff) scroll.setmaximumwidth(18) # if alter setwidgetresizeable false, # textedit center, scrolling not work. scroll.setwidgetresizable(true) scroll.setwidget(pagewidget) hbox = qtgui.qhboxlayout() hbox.setcontentsmargins(0,0,0,0) hbox.addstretch(0.5) hbox.addwidget(text) hbox.addstretch(0.5) pagewidget.setlayout(hbox) hbox2 = qtgui.qhboxlayout() hbox2.setcontentsmargins(0,0,0,0) hbox2.addwidget(pagewidget) hbox2.addwidget(scroll) self.setlayout(hbox2) class maintextedit(qtgui.qtextedit): def __init__(self, *args, **kwargs): super(maintextedit, self).__init__(*args, **kwargs) self.document().contentschanged.connect(self.sizechange) self.setfontpointsize(80) self.setverticalscrollbarpolicy(qtcore.qt.scrollbaralwaysoff) def sizechange(self): docheight = self.document().size().height() self.setminimumheight(docheight) def main(): app = qtgui.qapplication(sys.argv) mw = mainwindow() sys.exit(app.exec_()) if __name__ == '__main__': main() there @ to the lowest degree 2 problems this:
problem #1
as is, code above not horizontally center qtextedit in main window, scroll bar @ far-right work. if alter scroll.setwidgetresizable(true) scroll.setwidgetresizable(false) on line 41, qtextedit center horizontally, scroll-bar not work. seems can 1 feature or other, not both.
problem #2
in order maintain mainwindow auto-expanding when qtextedit grows, mainwindow assigns fixed height centralwidget whenever mainwindow resized (see line 19 of code above). works until user tries vertically shrink main window. window can vertically expanded clicking , dragging bottom border, can't vertically shrunk.
conclusion
maybe wrong approach all-together. suggestions?
set symmetrical margin via setviewportmargins on qtextedit inherits qabstractscrollarea.
example:
from pyside import qtgui, qtcore app = qtgui.qapplication([]) window = qtgui.qwidget() layout = qtgui.qvboxlayout(window) edit = qtgui.qtextedit('jfdh afdhgfkjg fdnvfh vklkfjvkflj lddkl ljklfjkl jvkldjfkvljfgvjldf ll dl dljvklj ljljlbl llkb jbgl') edit.setverticalscrollbarpolicy(qtcore.qt.scrollbaralwayson) edit.setviewportmargins(30, 0, 30, 30) layout.addwidget(edit) window.show() app.exec_() gives:
python qt4 pyside qtextedit qscrollarea
No comments:
Post a Comment