簡體   English   中英

PyQt4信號和插槽-QToolButton

[英]PyQt4 signals and slots - QToolButton

在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)

我不確定為什么要執行以下操作,但這就是問題所在:

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

SettingsDialog本身是一個適當的QDialog 您無需實例化另一個QDialog

現在,您正在創建一個空的QDialog ,然后使用與SettingsDialog相同的ui來填充它(即setupUi(dialog) ),然后顯示此對話框。 但是...信號連接用於SettingsDialog ,而您所顯示的對話框沒有該連接。

基本上,您根本不需要額外的QDialog 以下內容就足夠了:

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

好。 所以這是一個示例,您如何將參數傳遞給插槽

從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

在您的情況下,參數將為int。 當然,您必須從某處獲取值,然后將其作為參數放入局部部分(這里我只是使用按鈕的文本),但是您可以使用int,bool等。只需注意插槽的簽名即可。

這是對我有幫助的教程:http: //zetcode.com/gui/pyqt4/

我希望這有幫助。

嘿,這里有一個完整運行的示例(只需將其復制粘貼到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_()

所以我真的不明白為什么,但是改變從MainWindow調用settingsDialog的方式已經解決了我的問題。 我猜需要指定父窗口??:

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