简体   繁体   中英

pyqt5 custom dialog input popup within main window

I need to generate a custom popup input window triggered by clicking a QPushButton in my app (via clicked ). It needs to get several inputs from the user of different types and then return them to the calling function inside the main window app. I have found built in functions such as QInputDialog that can do this for single specific inputs, but I can't figure out how to do this in the case of a popup that asks for several inputs of different types at once (preferably in a window designed in Qt Designer). Does anyone know how to do this?

import sys
import os
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import uic

path = os.path.dirname(__file__) #uic paths from itself, not the active dir, so path needed
qtCreatorFile = "NAME.ui" #Ui file name, from QtDesigner

Ui_MainWindow, QtBaseClass = uic.loadUiType(path + qtCreatorFile) #process through pyuic

class MyApp(QMainWindow, Ui_MainWindow): #gui class
    def __init__(self):
        #Set up the gui via Qt
        super(MyApp, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.add_button.clicked.connect(self.add_row) #add_button is QPushButton

    def add_row(self):
        data1, data2, data3 = #popup form to get data (types are not the same)
        #do stuff with data
        pass

#start app 
if __name__ == "__main__":
    app = QApplication(sys.argv) #instantiate a QtGui (holder for the app)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

There is no single solution but I will give you a guide to do what you want.

If you want to get a widget with the behavior of QInputDialog you must first choose the right template, in this case a good option is Dialog with Buttons Bottom or Dialog with Buttons Right, add the components you want, position it, etc.

在此输入图像描述

Then as you show your code you create a class that inherits from QDialog and then create a method where you get the results but to do so do not use show() but exec_()

path = os.path.dirname(__file__)
qtCreatorFile = "some_dialog.ui"

Ui_Dialog, _ = uic.loadUiType(os.path.join(path,qtCreatorFile))

class CustomDialog(QDialog, Ui_Dialog):
    def __init__(self):
        super(CustomDialog, self).__init__()
        self.setupUi(self)
        # set initials values to widgets

    def getResults(self):
        if self.exec_() == QDialog.Accepted:
            # get all values
            val = self.some_widget.some_function()
            val2 = self.some_widget2.some_another_function()
            return val1, val2, ...
        else:
            return None

And then use it in your function:

class MyApp(QMainWindow, Ui_MainWindow): #gui class
    def __init__(self):
        #Set up the gui via Qt
        super(MyApp, self).__init__()
        self.setupUi(self)

        self.add_button.clicked.connect(self.add_row) #add_button is QPushButton

    def add_row(self):
        w = CustomDialog()
        values = w.getResults()
        if values:
            data1, data2, data3 = values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM