将参数传递给QT设计器构建的slot函数
Pass arguments to slot function built by QT Designer
我是GUI设计的新手,开始使用QT Designer 4.8.6。我正在使用信号插槽机制连接按钮以发挥作用。
以下是pyuic4util为创建GUI脚本生成的一些连接:
QtCore.QObject.connect(self.btn_add_codes, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.sel_codes_file)
QtCore.QObject.connect(self.btn_add_xls, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.sel_excel_file)
我的主python文件中的相关函数(path_codes
和path_excel
是QLineEdit小部件):
class MainDialog(QtGui.QMainWindow, gui_v1.Ui_MainWindow):
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
self.setupUi(self)
def sel_codes_file(self):
self.path_codes.setText(QtGui.QFileDialog.getOpenFileName())
def sel_excel_file(self):
self.path_excel.setText(QtGui.QFileDialog.getOpenFileName())
我想为所有按钮使用一个通用函数,这些按钮的操作是搜索文件并在LineEdit小部件中显示路径。我在MainDialog类中添加了这个函数:
def select_file(self, textbox):
self.textbox.setText(QtGui.QFileDialog.getOpenFileName())
我将连接修改为:
QtCore.QObject.connect(self.btn_add_codes, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.select_file(textbox=self.path_codes)
它不起作用。主窗口没有显示此代码,我收到此错误:AttributeError: 'MainDialog' object has no attribute 'textbox'
是否可以将参数传递给插槽连接函数?如果是,我做错了什么?谢谢
这个lambda工作吗?至少在使用C++时,我是这样使用Qt的。
self.btn_add_codes.clicked.connect(lambda codes=self.path_codes: MainWindow.select_file(codes))
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- 无法在 CLion 中构建 C++ 项目
- 函数向量_指针有不同的原型,我可以构建一个吗
- 如何使用ndk-build.cmd构建Android.so文件
- libssh 的函数在构建 libssh 时无法在 Qt 和 cmake 错误中找到
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 构建可组合有向图(扫描仪生成器的汤普森构造算法)
- 无法使用Qt Creator在Windows中构建yaml-cpp
- 构建一个由C和C++文件组成的库
- llvm构建器向基本块添加终止符
- FLTK 2.0构建和演示,适用于VS2019的2011年左右的代码库
- 如何跨平台将二进制资源构建到程序中?
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- 如何解决 Ninja c++ 构建和执行问题
- 使用 cmake 的 LLVM 构建在 tsan_libdispatch_mac.cc 期间失败; "Error: conflicting types for ..."
- CMake WxWidgets项目成功地在Linux上构建,但没有在Windows上构建
- 更改命令行 qt5 源代码构建配置的正确/快速方法
- 用CMake构建C++协议
- 将参数传递给QT设计器构建的slot函数