使用QSignalMapper映射多信号到同一个槽

关键词:python qt4 信号和槽 SignalMapper

还是举例说明比较好写:

1)C++版本:

 QSignalMapper *signalMapper = new QSignalMapper(this);
    for (i = 0; i < 10; i++) {
        button = new QPushButton(QObject::tr(buttonName));
        signalMapper->setMapping(button[i], i);
        connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
    }
    connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(handle(int)));
 2)pyqt4版本:

 

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Window(QWidget):

    def __init__(self, parent = None):
    
        QWidget.__init__(self, parent)
        
        layout = QVBoxLayout(self)
        mapper = QSignalMapper(self)
        
        for i in range(5):
        
            button = QPushButton()
            button.setText("Button " + str(i))
            self.connect(button, SIGNAL("clicked()"), mapper, SLOT("map()"))
            if i % 2 == 0:
                mapper.setMapping(button, str(i))
            else:
                mapper.setMapping(button, i)
            layout.addWidget(button)
        
        self.connect(mapper, SIGNAL("mapped(const QString &)"), self.stringMapped)
        self.connect(mapper, SIGNAL("mapped(int)"), self.intMapped)
    
    def stringMapped(self, value):
    
        print "stringMapped", value
    
    def intMapped(self, value):
    
        print "intMapped", value
    

if __name__ == "__main__":

    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的QSignalMapper使用案例: ```cpp #include <QtWidgets> class MyWidget : public QWidget { Q_OBJECT public: explicit MyWidget(QWidget *parent = nullptr) : QWidget(parent) { QVBoxLayout *layout = new QVBoxLayout(this); QPushButton *button1 = new QPushButton("Button 1", this); QPushButton *button2 = new QPushButton("Button 2", this); QPushButton *button3 = new QPushButton("Button 3", this); layout->addWidget(button1); layout->addWidget(button2); layout->addWidget(button3); QSignalMapper *mapper = new QSignalMapper(this); connect(button1, SIGNAL(clicked()), mapper, SLOT(map())); connect(button2, SIGNAL(clicked()), mapper, SLOT(map())); connect(button3, SIGNAL(clicked()), mapper, SLOT(map())); mapper->setMapping(button1, "Button 1 clicked"); mapper->setMapping(button2, "Button 2 clicked"); mapper->setMapping(button3, "Button 3 clicked"); connect(mapper, SIGNAL(mapped(QString)), this, SLOT(handleButtonClicked(QString))); } private slots: void handleButtonClicked(QString text) { QMessageBox::information(this, "Button clicked", text); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWidget widget; widget.show(); return app.exec(); } ``` 在上面的代码中,我们创建了三个QPushButton,并将它们添加到一个垂直布局中。然后,我们创建了一个QSignalMapper,并将每个QPushButton的clicked()信号连接到QSignalMapper的map()函数上。接着,我们使用QSignalMapper的setMapping()函数将每个QPushButton和一个字符串映射在一起,这个字符串是在按钮被点击时将会传递给函数的参数。最后,我们将QSignalMapper的mapped()信号连接到MyWidget的handleButtonClicked()函数上,这个函数会弹出一个消息框,显示按钮被点击的文本。这样,当任意一个QPushButton被点击时,handleButtonClicked()函数就会被调用,并且会显示对应的文本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值