简体   繁体   English

主窗口中的pyqt5自定义对话框输入弹出窗口

[英]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 ). 我需要生成一个自定义弹出输入窗口,通过单击我的应用程序中的QPushButton (通过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). 我发现内置函数,如QInputDialog ,可以为单个特定输入执行此操作,但我无法弄清楚如何在弹出窗口的情况下同时请求多个不同类型的输入(最好是在窗口中)在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. 如果你想获得一个具有QInputDialog行为的小部件,你必须首先选择正确的模板,在这种情况下,一个好的选择是带按钮底部的Dialog或带按钮的对话框,添加你想要的组件,定位它等。

在此输入图像描述

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_() 然后在显示代码时创建一个继承自QDialog的类,然后创建一个获得结果的方法,但这样做不要使用show()而是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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