Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Problem with getting text from PlainTextEdit
Forum Updated to NodeBB v4.3 + New Features

Problem with getting text from PlainTextEdit

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 513 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zetafunction
    wrote on 12 Oct 2019, 16:18 last edited by Zetafunction 10 Dec 2019, 17:20
    #1

    Via TabWidget as central widget my little programm displays a QPainTextEdit. What i want:

    • TabWidget which displays a custom PlainTextEdit (works)

    • a save function which saves the written text e.g. as *.txt file
      This save-function should only save the text file which is 'active' in the TabWidget if multible tabs are opened.

    The problem with my code is, that for some reason the toPlainText() function does not get the written text and prints 'None' if i want to print the written text.

    from PyQt5.QtWidgets import QApplication, QMainWindow, QMenuBar, QWidget, QMenu, QMessageBox, qApp, QTextEdit, QPlainTextEdit, QTabWidget,QVBoxLayout, QInputDialog, QFileDialog
    from PyQt5.QtGui import QIcon, QImage, QColor, QPainter, QTextFormat, QFontMetricsF
    from PyQt5.QtCore import Qt, pyqtSlot, QRect, QSize
    import sys
    from PyQt5.Qt import QAction
    from idlelib import statusbar
    import numpy as np
    
    class QCodeEditor(QPlainTextEdit):
       #some unnecessary code for this problem to customize the QPlainTextEdit
    
        def getText(self):
            print(str(self.toPlainText()))
    
    class Window(QMainWindow):
        def __init__(self):
            super().__init__()
            
            top = 50
            left = 50
            width = 1800
            heigt = 900
            
            icon = "src/icons/icon.png"
            
            self.setWindowTitle("Title")
            self.setGeometry(top, left, width, heigt)
            self.setWindowIcon(QIcon(icon))
            
            self.layout = QVBoxLayout(self)
            
            # Initialize tab screen
            tabs = QTabWidget()
            tab1 = QWidget()
            #self.tabs.setStyleSheet("background-color: rgb(36, 36, 36);")
            # Add tabs
            tabs.addTab(tab1,"Untitled")
            
            tab1.layout = QVBoxLayout(self)
            self.texteditor = QCodeEditor()
            self.texteditor.setStyleSheet("background-color: rgb(36, 36, 36); color: rgb(255, 255, 255);font-size: 15px;font-family: Bookman;")
            tab1.layout.addWidget(self.texteditor)
            tab1.setLayout(tab1.layout)
            
            self.layout.addWidget(tabs)
            self.setLayout(self.layout)
            self.setCentralWidget(tabs)
    
            
    ###Functions###
         
            def newfileEvent(self, name="Untitled"):
                widget = QWidget()
                tabs.addTab(widget, name)
                widget.layout = QVBoxLayout()
                textedit = QCodeEditor()
                textedit.setStyleSheet("background-color: rgb(36, 36, 36); color: rgb(255, 255, 255);font-size: 15px;font-family: Bookman;")
                widget.layout.addWidget(textedit)
                widget.setLayout(widget.layout)
                
            def file_save(self):
                # S_File will get the directory path and extension.
                S__File = QFileDialog.getSaveFileName(None,'SaveTextFile','/', "All Files(*.*);; Text Files (*.txt)")
            
                text = QCodeEditor().getText()
                print(text)
                
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 12 Oct 2019, 18:28 last edited by
      #2

      Hi,

      You call print in your getText method and return nothing from it hence the result you get as returning nothing from a python method corresponds to "return None".

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      1/2

      12 Oct 2019, 16:18

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved