在PyQt4中,我有一个主窗口,当单击“设置”按钮时,将打开“设置”对话框。
from PyQt4 import QtCore, QtGui
import ui_Design, ui_Settings_Design
class MainDialog(QtGui.QMainWindow, ui_Design.Ui_arbCrunchUI):
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
self.setupUi(self)
self.settingsBtn.clicked.connect(lambda: self.showSettings())
def showSettings(self):
dialog = QtGui.QDialog()
dialog.ui = SettingsDialog()
dialog.ui.setupUi(dialog)
dialog.exec_()上面的工作和我的SettingsDialog显示,但我无法使setPageIndex工作
class SettingsDialog(QtGui.QDialog, ui_Settings_Design.Ui_SettingsDialog):
def __init__(self, parent=None):
super(SettingsDialog, self).__init__(parent)
self.setupUi(self)
self.bookSettingsBtn.clicked.connect(self.setPageIndex)
@QtCore.pyqtSlot()
def setPageIndex(self):
print 'selected'
self.settingsStackedWidget.setCurrentIndex(0)bookSettingsBtn是一个QToolButton
self.bookSettingsBtn = QtGui.QToolButton(self.navigationFrame)settingsStackedWidget是QStackedWidget
self.settingsStackedWidget = QtGui.QStackedWidget(SettingsDialog)在这一点上,我对信号和插槽感到非常困惑,而我所读到的任何东西都不能澄清这一点--如果有人能指出我在上面做错了什么,并且有可能引导我阅读一本关于信号和插槽的好(初学者)指南/教程,我们将不胜感激。
我还想知道如何使setPageIndex工作如下:
def setPageIndex(self, selection):
self.settingsStackedWidget.setCurrentIndex(selection)发布于 2015-01-06 19:01:50
我不知道你为什么要做以下事情,但这就是问题所在:
def showSettings(self):
dialog = QtGui.QDialog()
dialog.ui = SettingsDialog()
dialog.ui.setupUi(dialog)
dialog.exec_()SettingsDialog本身就是一个适当的QDialog。您不需要实例化另一个QDialog。
现在,您正在创建一个空的QDialog,然后使用与SettingsDialog (即setupUi(dialog))相同的ui填充它,然后显示这个对话框。但是..。信号连接是用于SettingsDialog的,而您正在显示的对话框没有。
基本上,您根本不需要额外的QDialog。以下几点应该足够:
def showSettings(self):
dialog = SettingsDialog()
dialog.exec_()发布于 2015-01-06 12:40:06
好的。下面是一个例子,您如何将参数传递给一个槽
从函式工具进口部分
# here you have a button bookSettingsBtn:
self.bookSettingsBtn = QtGui.QPushButton("settings")
self.bookSettingsBtn.clicked.connect(partial(self.setPageIndex, self.bookSettingsBtn.text()))
@pyqtSlot(str) # this means the function expects 1 string parameter (str, str) 2 string parameters etc.
def setPageIndex(self, selection):
print "you pressed button " + selection在您的例子中,这个论点将是一个int。当然,您必须从某个地方获取值,然后将其作为参数放在部分部分(这里我刚刚使用了按钮的文本),但是您可以使用int、bool等。只需查看时隙签名。
这里有一个帮助我的教程:http://zetcode.com/gui/pyqt4/
我希望这能帮到你。
发布于 2015-01-06 13:52:42
嘿,这里有一个完全运行的示例(只需将其复制到python文件中并运行它):也许这对您有帮助。这是一个很小的例子,但是在这里你可以看到它是如何工作的。
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from functools import partial
class MyForm(QMainWindow):
def __init__(self, parent=None):
super(MyForm, self).__init__(parent)
button1 = QPushButton('Button 1')
button2 = QPushButton('Button 2')
button1.clicked.connect(partial(self.on_button, button1.text()))
button2.clicked.connect(partial(self.on_button, button1.text()))
layout = QHBoxLayout()
layout.addWidget(button1)
layout.addWidget(button2)
main_frame = QWidget()
main_frame.setLayout(layout)
self.setCentralWidget(main_frame)
@pyqtSlot(str)
def on_button(self, n):
print "Text of button is: " + str(n)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
form = MyForm()
form.show()
app.exec_()https://stackoverflow.com/questions/27798292
复制相似问题