简体   繁体   English

PyQt4信号和插槽-QToolButton

[英]PyQt4 signals and slots - QToolButton

In PyQt4 I have a main window which when the settings button is clicked opens the settings dialog 在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_()

The above works and my SettingsDialog is displayed but I cant get the setPageIndex to work 上面的作品和我的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)

The bookSettingsBtn is a QToolButton bookSettingsBtn是一个QToolButton

self.bookSettingsBtn = QtGui.QToolButton(self.navigationFrame)

And the settingsStackedWidget is a QStackedWidget 而settingsStackedWidget是一个QStackedWidget

self.settingsStackedWidget = QtGui.QStackedWidget(SettingsDialog)

At this point I am pretty confused on signals and slots and nothing I have read clears this up - if anyone can point out what I am doing wrong above and also potentially direct me to a good (beginners) guide / tutorial on signals and slots it would be greatly appreciated 在这一点上,我对信号和插槽非常困惑,而且我所读的内容也无法解决所有问题-如果有人可以指出我在上面做错的事情,还可能将我引向有关信号和插槽的良好(初学者)指南/教程将不胜感激

I would also like to know how to make setPageIndex work as follows: 我也想知道如何使setPageIndex如下工作:

def setPageIndex(self, selection):
     self.settingsStackedWidget.setCurrentIndex(selection)

I'm not sure why you're doing the following, but that's the issue: 我不确定为什么要执行以下操作,但这就是问题所在:

def showSettings(self):
    dialog = QtGui.QDialog()
    dialog.ui = SettingsDialog()
    dialog.ui.setupUi(dialog)
    dialog.exec_()

SettingsDialog itself is a proper QDialog . SettingsDialog本身是一个适当的QDialog You don't need to instantiate another QDialog . 您无需实例化另一个QDialog

Right now, you're creating an empty QDialog and then populate it with the same ui as SettingsDialog (ie setupUi(dialog) ), then you show this dialog. 现在,您正在创建一个空的QDialog ,然后使用与SettingsDialog相同的ui来填充它(即setupUi(dialog) ),然后显示此对话框。 But... The signal connection is for SettingsDialog , and the dialog you're showing doesn't have that. 但是...信号连接用于SettingsDialog ,而您所显示的对话框没有该连接。

Basically, you don't need that extra QDialog at all. 基本上,您根本不需要额外的QDialog The following should be enough: 以下内容就足够了:

def showSettings(self):
    dialog = SettingsDialog()
    dialog.exec_()

Ok. 好。 So here is an example how you pass an argument to a slot 所以这是一个示例,您如何将参数传递给插槽

from functools import partial 从functools导入部分

# 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

In your case the argument would be an int. 在您的情况下,参数将为int。 Of course you have to get the value from somewhere and then put it in the partial part as the argument (here I just used the text of the button), but you can use int, bool etc. Just watch the slot signature. 当然,您必须从某处获取值,然后将其作为参数放入局部部分(这里我只是使用按钮的文本),但是您可以使用int,bool等。只需注意插槽的签名即可。

Here is a tutorial that helped me: http://zetcode.com/gui/pyqt4/ 这是对我有帮助的教程:http: //zetcode.com/gui/pyqt4/

I hope this helps. 我希望这有帮助。

Hey here I have a fully running example (just copy paste it in a python file and run it): Maybe this helps you. 嘿,这里有一个完整运行的示例(只需将其复制粘贴到python文件中并运行):也许这对您有所帮助。 It's a small example but here you see how it works. 这是一个小例子,但是在这里您可以看到它是如何工作的。

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_()

So I dont really understand why but changing the way the settingsDialog is called from the MainWindow has fixed my problem. 所以我真的不明白为什么,但是改变从MainWindow调用settingsDialog的方式已经解决了我的问题。 I guess the parent window needed to be specified??: 我猜需要指定父窗口??:

class MainDialog(QtGui.QMainWindow, ui_Design.Ui_arbCrunchUI):
....
    def showSettings(self):
        self.settingsDialog = QtGui.QDialog(self)
        self.settingsDialog.ui = SettingsDialog(self)
        self.settingsDialog.ui.show()

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(partial(self.setPageIndex, 1))

    @QtCore.pyqtSlot(int)
    def setPageIndex(self, selection):
        self.settingsStackedWidget.setCurrentIndex(selection)

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

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