Reputation: 25
I want to edit lineEdit
from an .ui
file which is imported. I would not like to convert .ui
to .py
code because .ui
will be changed frequently and converting to .py and import from the .py seems a longer proccess for me.
When run the code, It seems that instead of one, two seperate lineEdit
is generated in mainwindow. Probably parent-subclass relationships due to formclass ui
but I could not understand why?
from PyQt5 import QtCore, QtGui, QtWidgets, uic
import sys,re
import pandas as pd
from glob import glob
import os
ui,base=uic.loadUiType("test.ui")
class MainWindow(ui,base):
def __init__(self, parent=None):
base.__init__(self,parent)
# initializes the user interface
self.setupUi(self)
self.lineEdit=lineEdit(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__":
if not QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
else:
app = QtWidgets.QApplication.instance()
MainWindow=MainWindow()
MainWindow.show()
app.exec_()
Changed self.lineEdit=lineEdit(self)
resulted two seperate lineEdit
while self.lineEdit=lineEdit(self.lineEdit)
cause two coincident lineEdit
widget at the same coordinates of mainwindow.
Any help would be nice...
Edit:test.ui is here https://drive.google.com/open?id=1oe6z2BaiLYm0mo-nadmDvzsoLHXnwkfm
Upvotes: 1
Views: 72
Reputation: 244003
Explanation:
You are assuming that with line self.lineEdit=lineEdit(self.lineEdit)
it replaces QLineEdit, nope. To replace a variable with another variable does not imply the elimination of the previous variable since the ownership of the previous QLineEdit has it Qt, in your case you are indicating that you are creating another QLineEdit that will be child of the first since you pass the parent as the first parameter so it will be located regarding the position of the QLineEdit.
Solution:
If you want to use a custom widget in the .ui then you must promote the widget, for this you can follow the following posts:
Considering the above, the solution is:
├── lineedit.py
├── main.py
└── test.ui
main.py
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic
current_dir = os.path.dirname(os.path.realpath(__file__))
Ui, Base = uic.loadUiType(os.path.join(current_dir, "test.ui"))
class MainWindow(Base, Ui):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
if __name__ == "__main__":
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
lineedit.py
from PyQt5 import QtCore, QtWidgets
class LineEdit(QtWidgets.QLineEdit):
def __init__(self, parent=None):
super().__init__(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.acceptProposedAction()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls():
for url in event.mimeData().urls():
links = url.toLocalFile()
self.setText(links)
test.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>646</height>
</rect>
</property>
<property name="windowTitle">
<string>A.G_bulucu</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="LineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>80</x>
<y>30</y>
<width>311</width>
<height>20</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>400</width>
<height>16777215</height>
</size>
</property>
<property name="inputMethodHints">
<set>Qt::ImhNone</set>
</property>
<property name="text">
<string/>
</property>
<property name="frame">
<bool>true</bool>
</property>
<property name="echoMode">
<enum>QLineEdit::Normal</enum>
</property>
<property name="alignment">
<set>Qt::AlignJustify|Qt::AlignVCenter</set>
</property>
<property name="dragEnabled">
<bool>false</bool>
</property>
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>10</x>
<y>30</y>
<width>61</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>File</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>400</x>
<y>30</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>open</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>30</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<customwidgets>
<customwidget>
<class>LineEdit</class>
<extends>QLineEdit</extends>
<header>lineedit</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
Upvotes: 1
Reputation: 1804
You generate next QLineEdit in
self.lineEdit=lineEdit(self.lineEdit)
You can manipulate with ui from code (like put placeholder QWidget in place of LineEdit and in constructor put custom line edit in this widget or you may register custom widget in QtDesigner. Read more here: https://doc.qt.io/qt-5/designer-creating-custom-widgets.html
Upvotes: 0