Search code examples
pythonpyside2

Apply function to QtextEdit or QPlainTextEdit python pyside2


I would like to ask You how can I print my results in QTextEdit or QPlainTextEdit, I've tried a few combination from here and some others site and nothing works, I will glad if someone will help me to fix it

That is my code:

from PySide2 import QtCore, QtWidgets
from PySide2.QtWidgets import QMainWindow, QWidget, QLabel, QLineEdit,QPlainTextEdit, QTextEdit, QMessageBox, QApplication
from PySide2.QtWidgets import QPushButton
from PySide2.QtCore import QSize
#from PySide2.QtGui import *
import sys

class Cam_Ext(QMainWindow):


    def __init__(self, Custom):
        QMainWindow.__init__(self, Custom)

        self.setMinimumSize(QSize(700, 900))
        self.setWindowTitle("Print groupes seletionner")

        ###btn1
        self.btn = QtWidgets.QPushButton('Print groupes' , self)
        self.btn.move(180, 100)
        self.btn.resize(350, 40)
        self.btn.setStyleSheet("background-color: rgb(255, 255, 255); font-family: arial; font-size: 17px; font-weight: bold;")
        self.btn.clicked.connect(self.Renommer)

        self.line = QPlainTextEdit(self)
        self.line.setStyleSheet("font-size: 12px; font-weight: bold; ")
        self.line.move(100, 170)
        self.line.resize(500, 400)
        self.line.setText(self.Renommer)
        #self.line.setPlaceholderText(self.Renommer)


        self.show()
    def Renommer(self):
        # -*- coding: utf-8 -*-
        import PhotoScan
        import os
        doc = PhotoScan.app.document
        pr_name = doc.path
        project_name = os.path.split(pr_name)[-1]
        print(project_name)

        groups = doc.chunk.camera_groups
        # print(groups)
        #x = 0
        seg = "SEG01"
        for group in groups:
            # print(group)
            if group.selected:
                print(project_name, "-",group, "-", seg, ";")
                #x += 1
def main():

    global doc
    doc = PhotoScan.app.document

    global app
    app = QtWidgets.QApplication.instance()
    Custom = app.activeWindow()

    dlg = Cam_Ext(Custom)

PhotoScan.app.addMenuItem("Pp/Print groupes seletionner", main)

I have to use lambda ? I don't know how to print my results from my function inside my text window, in mode append , I want keep my text inside and add new below each time when I click on my QPushButton, to this window, please help me, what I need to change ??

That's my print if that can help :

    2018-08-09 14:29:54 Error: 'PySide2.QtWidgets.QTextEdit.insertPlainText' called with wrong argument types:
2018-08-09 14:29:54   PySide2.QtWidgets.QTextEdit.insertPlainText(PySide2.QtWidgets.QHBoxLayout)
2018-08-09 14:29:54 Supported signatures:
2018-08-09 14:29:54   PySide2.QtWidgets.QTextEdit.insertPlainText(unicode)

Solution

  • You must add the text in the loop using appendPlainText().

    class Cam_Ext(QMainWindow):
        def __init__(self, Custom):
            QMainWindow.__init__(self, Custom)
            ...
            self.btn.clicked.connect(self.Renommer)
    
            self.line = QPlainTextEdit(self)
            self.line.setStyleSheet("font-size: 12px; font-weight: bold; ")
            self.line.move(100, 170)
            self.line.resize(500, 400)
            self.show()
    
        def Renommer(self):
            ...
            # uncomment if you want to clean the previous text
            # self.line.clear()
            for group in groups:
                # print(group)
                if group.selected:
                    self.line.appendPlainText("{}-{}-{};".format(project_name, group, seg))