Python Forum
[PyQt] Why lineEdit is showing text like this ??
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Why lineEdit is showing text like this ??
#1
The below code is showing lineEdit such that when a text is written, it showed the text not throughout of the width of the lineEdit. Kind of seemed like it is divided by half.

Based on my try and errors, the cause is
self.lineEdit=lineEdit(ui.lineEdit)
to initialize the mainwindow.
Any Help would be great
Thnks :)



from PyQt5 import QtCore, QtGui, QtWidgets, uic
import sys,re
import pandas as pd
from glob import glob
import os



class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        ui=uic.loadUi('simple.ui',self)        

        # initializes the user interface

        self.lineEdit=lineEdit(ui.lineEdit)
        
        
               
        

        
class lineEdit(QtWidgets.QLineEdit):
     def __init__(self, parent):
        super().__init__(parent)   
        self.parent=parent
        self.setAcceptDrops(True)
        self.setDragEnabled(True)
        
        
        
        
        
     def dragEnterEvent(self, event):
      
        if event.mimeData().hasUrls:
            
            event.acceptProposedAction()
        else:
            event.ignore() 

#    def mimeTypes(self):
#        mimeTypes=super().mimeTypes()
#        mimeTypes.append("text/plain")
#        return mimeTypes
     def dragMoveEvent(self, event):
         if event.mimeData().hasUrls:
            event.setDropAction(QtCore.Qt.CopyAction)
            event.acceptProposedAction()
         else:
            event.ignore()
     def dropEvent(self, event):
         
         
         mymodel=QtGui.QStandardItemModel()

         if event.mimeData().hasUrls:
            event.setDropAction(QtCore.Qt.CopyAction)
            
            for url in event.mimeData().urls():
                links=url.toLocalFile()
               
            self.setText(links)
            return links
            
        
           
        
if __name__ == "__main__":
    

    if not QtWidgets.QApplication.instance():
        app = QtWidgets.QApplication(sys.argv)
    else:
        app = QtWidgets.QApplication.instance() 

    
    MainWindow=MainWindow()

    MainWindow.show()
    app.exec_() 
Reply
#2
why an ui file?

from PyQt5 import QtCore, QtGui, QtWidgets, uic
import sys,re
import pandas as pd
from glob import glob
import os
 
class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent) 
        self.lineEdit=lineEdit(self)
        self.setCentralWidget(self.lineEdit)
         
class lineEdit(QtWidgets.QLineEdit):
     def __init__(self, parent):
        super().__init__(parent)   
        self.parent=parent
        self.setAcceptDrops(True)
        self.setDragEnabled(True)       
         
     def dragEnterEvent(self, event):
        if event.mimeData().hasUrls:
            event.acceptProposedAction()
        else:
            event.ignore() 

     def dragMoveEvent(self, event):
         if event.mimeData().hasUrls:
            event.setDropAction(QtCore.Qt.CopyAction)
            event.acceptProposedAction()
         else:
            event.ignore()
     def dropEvent(self, event):
          
          
         mymodel=QtGui.QStandardItemModel()
 
         if event.mimeData().hasUrls:
            event.setDropAction(QtCore.Qt.CopyAction)
             
            for url in event.mimeData().urls():
                links=url.toLocalFile()
                
            self.setText(links)
            return links

if __name__ == "__main__": 
    app = QtWidgets.QApplication(sys.argv)
    MainWindow=MainWindow()
    MainWindow.show()
    app.exec_()
Reply
#3
Okay let us assume that your UI file (which you ought not be using for this very reason) has a QLineEdit object called MyLineEdit (which it would never have since that is not how it goes about referencing variables). Then you should simply be able to do the following (I have not tested this because I never use UI files as they are a waste of time and effort):

ui.MyLineEdit = ClassLineEdit()

You are thus replacing whatever object you created in the UI file with your own Class Object.

Also when using super( ) you need to do the following:

class lineEdit(QtWidgets.QLineEdit):
     def __init__(self, parent):
        super(lineEdit, self).__init__()   
Note you most likely have no reason to pass a handle to your Parent to the base QLineEdit so do not do so.

Next had you made your import statement more direct and to the point as such:

from PyQt5.QtWidgets import QLineEdit
you could have done the following:

class NewLineEdit(QLineEdit):
     def __init__(self, parent):
        super(lineEdit, self).__init__()
-- or not even use super and do this --
class NewLineEdit(QLineEdit):
     def __init__(self, parent):
        QLineEdit.__init__(self)
And lastly are you even using the handle to the parent within your Line Edit objects or are you just passing it in unnecessarily -- if not needed we can simplify it as follows:
class NewLineEdit(QLineEdit):
     def __init__(self):
        QLineEdit.__init__(self)
Reply
#4
(Sep-06-2019, 05:20 PM)Axel_Erfurt Wrote: Also when using super( ) you need to do the following:

class lineEdit(QtWidgets.QLineEdit):
def __init__(self, parent):
super(lineEdit, self).__init__()

I was of the impression that this should not be necessary in Python 3. Can you explain why it should be done?
Reply
#5
The reason I am using .ui file because editing it in designer is easy and after saving, it is not required to convert to code, just save in designer and loadUi in python. Seems easy. Furthermore, If converted to code, it requires editing sometimes.
thnks @Denni but did not solved. Still continues...
Reply
#6
(Sep-06-2019, 09:07 PM)alandmoore Wrote:
(Sep-06-2019, 05:20 PM)Axel_Erfurt Wrote: Also when using super( ) you need to do the following:

class lineEdit(QtWidgets.QLineEdit):
def __init__(self, parent):
super(lineEdit, self).__init__()

I was of the impression that this should not be necessary in Python 3. Can you explain why it should be done?

I did not wrote it. In my code I wrote

class lineEdit(QtWidgets.QLineEdit):
     def __init__(self, parent):
        super().__init__(parent)  
Reply
#7
So any solution to edit a class in .ui file ??
Reply
#8
your ui file has a class?
Reply
#9
@Axel , the problem is lineEdit is generating twice as coincident each. I do not know how can I solve ? I also tried with loadUiType but same is happening
Reply
#10
show your ui file, I do not know what's in it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGUI] Showing text in QTableView sequence 0 3,026 Jan-20-2019, 05:00 PM
Last Post: sequence
  [PyQt] Need Help about comboBox and lineEdit DeanONeil 1 2,663 Oct-13-2017, 05:42 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020