[PyQt] Why lineEdit is showing text like this ?? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [PyQt] Why lineEdit is showing text like this ?? (/thread-20920.html) Pages:
1
2
|
Why lineEdit is showing text like this ?? - scorp08 - Sep-06-2019 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_() RE: Why lineEdit is showing text like this ?? - Axel_Erfurt - Sep-06-2019 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_() RE: Why lineEdit is showing text like this ?? - Denni - Sep-06-2019 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 QLineEdityou 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) RE: Why lineEdit is showing text like this ?? - alandmoore - Sep-06-2019 (Sep-06-2019, 05:20 PM)Axel_Erfurt Wrote: Also when using super( ) you need to do the following: I was of the impression that this should not be necessary in Python 3. Can you explain why it should be done? RE: Why lineEdit is showing text like this ?? - scorp08 - Sep-06-2019 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 @ RE: Why lineEdit is showing text like this ?? - Axel_Erfurt - Sep-06-2019 (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: I did not wrote it. In my code I wrote class lineEdit(QtWidgets.QLineEdit): def __init__(self, parent): super().__init__(parent) RE: Why lineEdit is showing text like this ?? - scorp08 - Sep-07-2019 So any solution to edit a class in .ui file ?? RE: Why lineEdit is showing text like this ?? - Axel_Erfurt - Sep-07-2019 your ui file has a class? RE: Why lineEdit is showing text like this ?? - scorp08 - Sep-08-2019 @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 RE: Why lineEdit is showing text like this ?? - Axel_Erfurt - Sep-08-2019 show your ui file, I do not know what's in it. |