Reputation: 183
I created a custom dialog that contains a QLineEdit. When certain criteria matches on the main screen, the dialog opens up and allows the user to enter input. Upon returning the the main screen, that input needs to be grabbed and used.
In QtDesigner, I created I dialog (with the Ok and Cancel buttons already on it) and added a QLineEdit with an object name of lineEdit.
also I'm using three .py file one is main_app which is importing other two files mainwindow file and dialog file (converted from .ui file) following is the method I'm using to open dialog in my main_app file.
def open_dialog(self):
Dialog = QtWidgets.QDialog()
ui = sub_search.Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
resp = Dialog.exec_()
if resp == QtWidgets.QDialog.Accepted:
print("Accepted!") #this place will be used to do something more once I have
#userinupt from dialog box
till this end code is working fine I'm not sure how to return and use user input following is the code I'm trying to return input of lineEdit from dialog box
def get_text_from(self):
if self.lineEdit.text() != "" and self.buttonBox.accepted():
swc = self.lineEdit.text()
return swc
elif self.lineEdit.text() == "" and self.buttonBox.accepted():
warning_message(title="Warning!", message="please provide swc clli,thanks!")
else:
pass
following are the complete code where I want to put more filter based on user input main_app.py
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5.QtWidgets import QMessageBox
import pandas as pd
from pyqt_master_data import search, sub_search
pd.set_option('display.max_rows', None, 'display.max_columns', None)
master_data = pd.read_excel('Master_data.xlsx', 'Shares')
class SearchTool(search.Ui_main, QtWidgets.QMainWindow):
def __init__(self):
super(SearchTool, self).__init__()
self.setupUi(self)
self.push()
def master_data_search(self):
if self.lineEdit.text() != "":
day = self.lineEdit.text().strip()
if day.upper() in "TUESDAY":
self.open_dialog()
else:
day_df = master_data[(master_data.Day.str.contains(day,
case=False, na=False))]
model = PandasModel(day_df)
self.tableView.setModel(model)
def push(self):
self.pushButton.clicked.connect(self.master_data_search)
def open_dialog(self):
Dialog = QtWidgets.QDialog()
ui = sub_search.Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
resp = Dialog.exec_()
if resp == QtWidgets.QDialog.Accepted:
print("Accepted!")
sub_search.Ui_Dialog.get_text_from() #do something here
else:
print('rejected')
def warning_message(title="Warning!", message="Please provide input,
thanks!"):
QMessageBox.information(None, title, message)
class PandasModel(QtCore.QAbstractTableModel):
"""
Class to populate a table view with a pandas dataframe
"""
def __init__(self, data, parent=None):
QtCore.QAbstractTableModel.__init__(self, parent)
self._data = data
def rowCount(self, parent=None):
return len(self._data.values)
def columnCount(self, parent=None):
return self._data.columns.size
def data(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
return str(self._data.values[index.row()][index.column()])
return None
def headerData(self, col, orientation, role):
if orientation == QtCore.Qt.Horizontal and role ==
QtCore.Qt.DisplayRole:
return self._data.columns[col]
return None
if __name__ == "__main__":
app = QtWidgets.QApplication([])
qt_app = SearchTool()
qt_app.show()
app.exec_()
search.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_main(object):
def setupUi(self, main):
main.setObjectName("main")
main.resize(855, 886)
main.setMouseTracking(True)
main.setTabletTracking(True)
self.centralwidget = QtWidgets.QWidget(main)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setFrameShape(QtWidgets.QFrame.Box)
self.frame.setFrameShadow(QtWidgets.QFrame.Plain)
self.frame.setObjectName("frame")
self.verticalLayout = QtWidgets.QVBoxLayout(self.frame)
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(self.frame)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.frame)
self.lineEdit.setClearButtonEnabled(True)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.pushButton = QtWidgets.QPushButton(self.frame)
font = QtGui.QFont()
font.setBold(True)
font.setItalic(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.verticalLayout_2.addWidget(self.frame)
self.frame_2 = QtWidgets.QFrame(self.centralwidget)
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.frame_2)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.frame_3 = QtWidgets.QFrame(self.frame_2)
self.frame_3.setMouseTracking(True)
self.frame_3.setTabletTracking(True)
self.frame_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_3.setObjectName("frame_3")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.frame_3)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.tableView = QtWidgets.QTableView(self.frame_3)
self.tableView.setMouseTracking(True)
self.tableView.setTabletTracking(True)
self.tableView.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.tableView.setFrameShadow(QtWidgets.QFrame.Plain)
self.tableView.setDragEnabled(True)
self.tableView.setDefaultDropAction(QtCore.Qt.CopyAction)
self.tableView.setSortingEnabled(True)
self.tableView.setObjectName("tableView")
self.verticalLayout_4.addWidget(self.tableView)
self.verticalLayout_3.addWidget(self.frame_3)
self.verticalLayout_2.addWidget(self.frame_2)
main.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(main)
self.menubar.setGeometry(QtCore.QRect(0, 0, 855, 26))
self.menubar.setObjectName("menubar")
main.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(main)
self.statusbar.setObjectName("statusbar")
main.setStatusBar(self.statusbar)
self.retranslateUi(main)
QtCore.QMetaObject.connectSlotsByName(main)
def retranslateUi(self, main):
_translate = QtCore.QCoreApplication.translate
main.setWindowTitle(_translate("main", "MainWindow"))
self.label.setText(_translate("main", "DATA"))
self.lineEdit.setPlaceholderText(_translate("main", "day"))
self.pushButton.setText(_translate("main", "SEARCH"))
sub_search.py
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMessageBox
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(478, 170)
Dialog.setMaximumSize(QtCore.QSize(560, 170))
self.verticalLayout_2 = QtWidgets.QVBoxLayout(Dialog)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.frame = QtWidgets.QFrame(Dialog)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.verticalLayout = QtWidgets.QVBoxLayout(self.frame)
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(self.frame)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.frame)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.verticalLayout_2.addWidget(self.frame)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setCenterButtons(True)
self.buttonBox.setObjectName("buttonBox")
self.verticalLayout_2.addWidget(self.buttonBox)
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def get_text_from(self):
if self.lineEdit.text() != "" and self.buttonBox.accepted():
swc = self.lineEdit.text()
return swc
elif self.lineEdit.text() == "" and self.buttonBox.accepted():
warning_message(title="Warning!", message="please provide swc clli,thanks!")
else:
pass
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "TextLabel"))
self.buttonBox.accepted.connect(self.get_text_from)
def warning_message(title="Warning!", message="Please provide input,
thanks!"):
QMessageBox.information(None, title, message)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
my Questions are how do capture lineEdit value and use in my main_app file. I realize I've still got lots to learn so if someone could point in the right direction that would be awesome. I got decent start but I'm not sure how to finish this..
thanks in advance!
Upvotes: 0
Views: 2232
Reputation: 24420
You could try something like this:
class SearchTool(search.Ui_main, QtWidgets.QMainWindow):
def __init__(self):
super(SearchTool, self).__init__()
self.setupUi(self)
self.push()
def master_data_search(self):
if self.lineEdit.text() != "":
day = self.lineEdit.text().strip()
if day.upper() in "TUESDAY":
result = self.open_dialog()
if result is not None:
print(f'the search result was {result}')
else:
print('the search was canceled')
else:
print('do something else')
def push(self):
self.pushButton.clicked.connect(self.master_data_search)
def open_dialog(self):
print('open dialog')
Dialog = QtWidgets.QDialog(self)
ui = sub_search.Ui_Dialog()
ui.setupUi(Dialog)
#Dialog.show()
resp = Dialog.exec_()
if resp == QtWidgets.QDialog.Accepted:
print("Accepted!")
# retrieve text from line edit in dialog and return it
return ui.lineEdit.text()
else:
print('rejected')
return None
Upvotes: 1