4

我需要让小部件相互通信。

我的中心 Widget 有 2 个小部件实例:widget A(instance of A()), widget B(instance of B()) 当 awidget A受事件影响时(例如:)mouseRelease,我需要widget A调用一个函数并进行一些计算、处理数据。然后我需要将此处理后的数据传递给widget B而后者在从接收此数据widget A,将使用此数据调用一个函数,对其进行处理,并对其进行更多操作。

这两个小部件都是从QWidget. 我几乎可以肯定我需要使用信号和插槽,但我不明白如何实现函数调用部分。

我在这里一遍又一遍地阅读了 Qt 的解释:http: //qt-project.org/wiki/Signals_and_Slots_in_PySide但是我无法弄清楚如何连接两个处理数据发出和发送的小部件。

我真的很感激这里的一些帮助。如果你不能用 Python 回答但又能胜任 C++,请尽管使用它来解释,而不是完全避免。

4

4 回答 4

12

这是一个示例,演示 PyQt4 中两个小部件之间的信号/插槽连接:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4 import QtCore, QtGui

class widgetB(QtGui.QWidget):
    procDone = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super(widgetB, self).__init__(parent)

        self.lineEdit = QtGui.QLineEdit(self)
        self.button = QtGui.QPushButton("Send Message to A", self)
        self.layout = QtGui.QHBoxLayout(self)
        self.layout.addWidget(self.lineEdit)
        self.layout.addWidget(self.button)

        self.button.clicked.connect(self.on_button_clicked)

    @QtCore.pyqtSlot()
    def on_button_clicked(self):
        self.procDone.emit(self.lineEdit.text())

    @QtCore.pyqtSlot(str)
    def on_procStart(self, message):
        self.lineEdit.setText("From A: " + message)

        self.raise_()

class widgetA(QtGui.QWidget):
    procStart = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super(widgetA, self).__init__(parent)

        self.lineEdit = QtGui.QLineEdit(self)
        self.lineEdit.setText("Hello!")

        self.button = QtGui.QPushButton("Send Message to B", self)
        self.button.clicked.connect(self.on_button_clicked)

        self.layout = QtGui.QHBoxLayout(self)
        self.layout.addWidget(self.lineEdit)
        self.layout.addWidget(self.button)

    @QtCore.pyqtSlot()
    def on_button_clicked(self):
        self.procStart.emit(self.lineEdit.text())

    @QtCore.pyqtSlot(str)
    def on_widgetB_procDone(self, message):
        self.lineEdit.setText("From B: " + message)

        self.raise_()


class mainwindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(mainwindow, self).__init__(parent)

        self.button = QtGui.QPushButton("Click Me", self)
        self.button.clicked.connect(self.on_button_clicked)

        self.setCentralWidget(self.button)

        self.widgetA = widgetA()
        self.widgetB = widgetB()

        self.widgetA.procStart.connect(self.widgetB.on_procStart)
        self.widgetB.procDone.connect(self.widgetA.on_widgetB_procDone)

    @QtCore.pyqtSlot()
    def on_button_clicked(self):
        self.widgetA.show()
        self.widgetB.show()

        self.widgetA.raise_()


if __name__ == "__main__":
    import sys

    app  = QtGui.QApplication(sys.argv)
    main = mainwindow()
    main.show()
    sys.exit(app.exec_())
于 2012-12-30T15:07:39.167 回答
9

为 PyQt5 更新@user1006989 的答案:

from PyQt5 import QtCore, QtGui, QtWidgets


class widgetB(QtWidgets.QWidget):

    procDone = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super(widgetB, self).__init__(parent)

        self.lineEdit = QtWidgets.QLineEdit(self)
        self.button = QtWidgets.QPushButton("Send Message to A", self)
        self.layout = QtWidgets.QHBoxLayout(self)
        self.layout.addWidget(self.lineEdit)
        self.layout.addWidget(self.button)

        self.button.clicked.connect(self.on_button_clicked)

    @QtCore.pyqtSlot()
    def on_button_clicked(self):
        self.procDone.emit(self.lineEdit.text())

    @QtCore.pyqtSlot(str)
    def on_procStart(self, message):
        self.lineEdit.setText("From A: " + message)

        self.raise_()


class widgetA(QtWidgets.QWidget):
    procStart = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super(widgetA, self).__init__(parent)

        self.lineEdit = QtWidgets.QLineEdit(self)
        self.lineEdit.setText("Hello!")

        self.button = QtWidgets.QPushButton("Send Message to B", self)
        self.button.clicked.connect(self.on_button_clicked)

        self.layout = QtWidgets.QHBoxLayout(self)
        self.layout.addWidget(self.lineEdit)
        self.layout.addWidget(self.button)

    @QtCore.pyqtSlot()
    def on_button_clicked(self):
        self.procStart.emit(self.lineEdit.text())

    @QtCore.pyqtSlot(str)
    def on_widgetB_procDone(self, message):
        self.lineEdit.setText("From B: " + message)

        self.raise_()


class mainwindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(mainwindow, self).__init__(parent)

        self.button = QtWidgets.QPushButton("Click Me", self)
        self.button.clicked.connect(self.on_button_clicked)

        self.setCentralWidget(self.button)

        self.widgetA = widgetA()
        self.widgetB = widgetB()

        self.widgetA.procStart.connect(self.widgetB.on_procStart)
        self.widgetB.procDone.connect(self.widgetA.on_widgetB_procDone)

    @QtCore.pyqtSlot()
    def on_button_clicked(self):
        self.widgetA.show()
        self.widgetB.show()

        self.widgetA.raise_()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    main = mainwindow()
    main.show()
    sys.exit(app.exec_())
于 2017-01-15T14:41:01.210 回答
3

我认为您可以在这里使用两种不同的方法:使AandB类紧密耦合,或者使用主窗口作为中介。

在第一种情况下,您将不得不“让A知道B”,这意味着A引用B. 在这种情况下,您甚至根本不必执行信号和插槽,但您可以直接调用B's 方法。

另一种方法是A发出一个带有参数的信号,该参数包含应该由 接收的数据,B主窗口捕获该信号并B使用数据调用。或者,您可以将数据放在一个A的属性中,让主窗口直接从这里获取它,而不将其作为信号的参数传递。

后一种方法可以让你写得更自由一点AB因为A不必知道如何调用B.

在代码中解释它应该是这样的:

class A(QWidget):
    the_signal = Signal(...)
    def mouseReleaseEvent(self, event):
        self.a_function()
        self.the_data = self.produce_data()
        self.the_signal.emit()

class B(QWidget):
    def process_data(self, data):
        pass

class MainWindow(QMainWindow):
    def __init__(self):
        self.a_widget = A()
        self.b_widget = B()
        self.a_widget.the_signal.connect(self.do_process)
        # if the signal has the data as argument:
        # self.a_widget.the_signal.connect(self.b_widget.process_data)
    def do_process(self):
        self.b_widget.process_data(self.a_widget.the_data)
于 2012-12-30T15:05:20.037 回答
1

可以使用几种方法来完成,最简单的一种(不是最聪明的)是建立中间连接,从对象 A 到主窗口,从主窗口到 B

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "classa.h"
#include "classb.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    classA *a;
    classB *b;

signals:
    void TosignalA();
    void TosignalB();

public slots:
    void emitB();

private slots:
    void on_pushButton_clicked();
};

#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    a = new classA(this);
    b=new classB(this);
    connect(this,SIGNAL(TosignalA()),this->a,SLOT(emitsig()));
    connect(this->a,SIGNAL(signal()),this,SLOT(emitB()));
    connect(this,SIGNAL(TosignalB()),this->b,SLOT(on_signal()));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::emitB()
{
    emit TosignalB();
}

void MainWindow::on_pushButton_clicked()
{
    emit TosignalA();
}

类.h

#ifndef CLASSA_H
#define CLASSA_H

#include <QObject>

class classA : public QObject
{
    Q_OBJECT
public:
    explicit classA(QObject *parent = 0);

signals:
    void signal();

public slots:
    void emitsig();

};

#endif // CLASSA_H

类.cpp

#include "classa.h"

classA::classA(QObject *parent) :
    QObject(parent)
{

}

void classA::emitsig()
{
    emit signal();
}

类b.h

#ifndef CLASSB_H
#define CLASSB_H

#include <QObject>
#include <QtGui>

class classB : public QObject
{
    Q_OBJECT
public:
    explicit classB(QObject *parent = 0);

signals:

public slots:
    void on_signal();

};

#endif // CLASSB_H

类b.cpp

#include "classb.h"

classB::classB(QObject *parent) :
    QObject(parent)
{
}
void classB::on_signal()
{
    QMessageBox *msgBox = new QMessageBox();
     msgBox->setText("signal emitted!");
     msgBox->exec();
}
于 2012-12-30T15:09:43.143 回答