Qt4:编写一个创建对话框并返回用户选择的函数
我不太确定这个问题有没有简单的解决办法,但我想写一个函数,这个函数可以显示一个对话框(这个对话框是在一个继承了 QDialog
的类中定义的),并在用户与对话框交互完成后返回用户输入的内容。换句话说,我想要的功能类似于 QFileDialog::getOpenFileName
这个静态方法,只需要一行代码就能打开对话框并返回用户的输入,而不是使用比较繁琐的信号/槽机制。
使用场景:
/* Shows the dialog, waits until user presses OK or Cancel,
then returns the user's choice.
*/
result = createDialogAndReturnUserChoice()
我现在在用 PyQt,但对于传统的 Qt4 C++ 框架的答案也没问题。
编辑//
下面是实现的方法:
dialog = CustomDialog() # creates the custom dialog we have defined in a class inheriting QDialog
if dialog.exec_(): # on exec_(), the whole program freezes until the user is done with the dialog; it returns the response of the user
# success
else:
# failure
1 个回答
1
听起来你已经准备好了所有需要的东西。你可以在你自己创建的 QDialog
类中写一个静态函数,来实现你想要的功能。你可以创建一个结构体或者类,把用户生成的数据封装起来,然后从你的静态函数中返回这些数据。Qt 提供了所有的源代码,所以你可以查看 QFileDialog::getOpenFileName()
在 qfiledialog.cpp
中是怎么实现的。
补充:抱歉,我没注意到你是在用 Python。我不太清楚这个语言在扩展 C++ 类和静态方法方面有什么功能。