python - PyQt: How to add new tabs to QTextEdit? -


i need add new tab, having problems. want add new tabs in main window, , keep methods of class editor(). can without having create class editor() need so. sorry english.

this code:

 pyqt4 import qtgui pyqt4 import qtcore   class main(qtgui.qmainwindow):      def __init__(self):         super(main, self).__init__()          self.initui()      def initui(self):         self.setwindowtitle("editor")         self.resize(640, 480)         self.edit = editor()          newac = qtgui.qaction('new', self)         newac.setshortcut('ctrl+n')         newac.triggered.connect(self.new_)          menu = self.menubar()         filemenu = menu.addmenu('&file')         filemenu.addaction(newac)          self.tab = qtgui.qtabwidget(self)         self.setcentralwidget(self.tab)   class editor(qtgui.qtextedit):      def __init__(self, parent=none):         super(editor, self).__init__(parent)       def new_(self):         tab = qtgui.qtextedit(self.tab)         self.tab.addtab(tab, 'untitled')  def main():     import sys     app = qtgui.qapplication(sys.argv)     w = main()     w.show()     sys.exit(app.exec_())  if __name__ == "__main__":     main()  

if want have same text on both (or more) tabs can use same editor class, if not, need instantiate editor object each tab.

also code have problems:

1- handling tabs inside editor objects. instead, must handle tabs @ main level.

2- "default" tab you're adding when create main object not have related qtextedit change:

self.tab = qtgui.qtabwidget(self) self.setcentralwidget(self.tab)     # <---- tab without qtextedit 

add this:

self.tab = qtgui.qtabwidget(self) self.editor = editor(self.tab)      # editor receives self.tab widget parent. self.setcentralwidget(self.tab)  

also need define editor class before main.

3- main object don't have method called new_, editor does. line:

newac.triggered.connect(self.new_) 

it's wrong.

so code might like:

from pyqt4 import qtgui pyqt4 import qtcore   class editor(qtgui.qtextedit):      def __init__(self, parent=none):         super(editor, self).__init__(parent)   class main(qtgui.qmainwindow):      def __init__(self, parent=none):         super(main, self).__init__(parent)          self.initui()      def initui(self):         self.setwindowtitle("editor")         self.resize(640, 480)          newac = qtgui.qaction('new', self)         newac.setshortcut('ctrl+n')         newac.triggered.connect(self.new_)          menu = self.menubar()         filemenu = menu.addmenu('&file')         filemenu.addaction(newac)          self.tab = qtgui.qtabwidget(self)         self.setcentralwidget(self.tab)         self.tab.addtab(editor(), "new text")      def new_(self):         self.tab.addtab(editor(), "new text")   def main():     import sys     app = qtgui.qapplication(sys.argv)     w = main()     w.show()     sys.exit(app.exec_())  if __name__ == "__main__":     main() 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -