Java实现Qt的SIGNAL-SLOT机制(保存到Map中,从而将它们关联起来,收到信号进行解析,最后反射调用)
SIGNAL-SLOT是Qt的一大特色,使用起来十分方便。在传统的AWT和Swing编程中,我们都是为要在
- public static void connect(QObject sender, String signal, Object receiver, String slot) {
- if (sender.signalSlotMap == null)
- sender.signalSlotMap = new HashMap<String, List<ReceiverSlot>>();
- List<ReceiverSlot> slotList = sender.signalSlotMap.get(signal);
- if (slotList == null) {
- slotList = new LinkedList<ReceiverSlot>();
- sender.signalSlotMap.put(signal, slotList);
- }
- slotList.add(createReceiverSlot(receiver, slot));
- }
- static class ReceiverSlot {
- Object receiver;
- Method slot;
- Object[] args;
- }
- private static ReceiverSlot createReceiverSlot(Object receiver, String slot) {
- ReceiverSlot receiverSlot = new ReceiverSlot();
- receiverSlot.receiver = receiver;
- Pattern pattern = Pattern.compile("(\\w+)\\(([\\w+,]*)\\)");
- Matcher matcher = pattern.matcher(slot);
- if (matcher.matches() && matcher.groupCount() == 2) {
- // 1.Connect SIGNAL to SLOT
- try {
- String methodName = matcher.group(1);
- String argStr = matcher.group(2);
- ArrayList<String> argList = new ArrayList<String>();
- pattern = Pattern.compile("\\w+");
- matcher = pattern.matcher(argStr);
- while (matcher.find())
- argList.add(matcher.group());
- String[] arguments = argList.toArray(new String[0]);
- receiverSlot.slot = findMethod(receiver, methodName, arguments);
- receiverSlot.args = new Object[0];
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- else {
- // 2.Connect SIGNAL to SIGNAL
- if (receiver instanceof QObject) {
- receiverSlot.slot = emitMethod;
- receiverSlot.args = new Object[] { slot };
- }
- }
- return receiverSlot;
- }
- private static Method emitMethod;
- protected Map<String, List<ReceiverSlot>> signalSlotMap;
- static {
- try {
- emitMethod = QObject.class.getDeclaredMethod("emit", String.class, Object[].class);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private static Method findMethod(Object receiver, String methodName, String[] arguments)
- throws NoSuchMethodException {
- Method slotMethod = null;
- if (arguments.length == 0)
- slotMethod = receiver.getClass().getMethod(methodName, new Class[0]);
- else {
- for (Method method : receiver.getClass().getMethods()) {
- // 1.Check method name
- if (!method.getName().equals(methodName))
- continue;
- // 2.Check parameter number
- Class<?>[] paramTypes = method.getParameterTypes();
- if (paramTypes.length != arguments.length)
- continue;
- // 3.Check parameter type
- boolean isMatch = true;
- for (int i = 0; i < paramTypes.length; i++) {
- if (!paramTypes[i].getSimpleName().equals(arguments[i])) {
- isMatch = false;
- break;
- }
- }
- if (isMatch) {
- slotMethod = method;
- break;
- }
- }
- if (slotMethod == null)
- throw new NoSuchMethodException("Cannot find method[" + methodName +
- "] with parameters: " + Arrays.toString(arguments));
- }
- return slotMethod;
- }
- protected void emit(String signal, Object... args) {
- System.out.println(getClass().getSimpleName() + " emit signal " + signal);
- if (signalSlotMap == null)
- return;
- List<ReceiverSlot> slotList = signalSlotMap.get(signal);
- if (slotList == null || slotList.isEmpty())
- return;
- for (ReceiverSlot objSlot : slotList) {
- try {
- if (objSlot.slot == emitMethod)
- objSlot.slot.invoke(objSlot.receiver, objSlot.args[0], args);
- else
- objSlot.slot.invoke(objSlot.receiver, args);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public class QWidget<T extends JComponent> extends QObject implements QSwing<T> {
- protected T widget;
- public QWidget(Class<T> clazz) {
- try {
- widget = clazz.newInstance();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Override
- public T getSwingWidget() {
- return this.widget;
- }
- }
- public class QPushButton extends QWidget<JButton> {
- public static final String CLICKED = "clicked";
- public QPushButton(String text) {
- super(JButton.class);
- widget.setText(text);
- widget.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- emit(CLICKED);
- }
- });
- }
- }
- public class QLineEdit extends QWidget<JTextField> {
- public static final String RETURN_PRESSED = "returnPressed";
- public QLineEdit() {
- super(JTextField.class);
- widget.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- emit(RETURN_PRESSED);
- }
- });
- }
- }
- public class AddressBar extends QWidget<JPanel> {
- /**
- * SIGNAL
- */
- public static final String NEW_BUTTON_CLICKED = "newButtonClicked";
- public static final String GO_TO_ADDRESS = "goToAddress(String,String)";
- /**
- * SLOT
- */
- public static final String HANDLE_GO_TO_ADDRESS = "handleGoToAddress()";
- private QPushButton newButton;
- private QLineEdit addressEdit;
- private QPushButton goButton;
- public AddressBar() {
- super(JPanel.class);
- // 1.Create widget
- newButton = new QPushButton("New");
- addressEdit = new QLineEdit();
- goButton = new QPushButton("Go");
- // 2.Set property
- addressEdit.getSwingWidget().setColumns(10);
- // 3.Connect signal-slot
- connect(newButton, QPushButton.CLICKED, this, NEW_BUTTON_CLICKED);
- connect(addressEdit, QLineEdit.RETURN_PRESSED, this, HANDLE_GO_TO_ADDRESS);
- connect(goButton, QPushButton.CLICKED, this, HANDLE_GO_TO_ADDRESS);
- // 4.Add to layout
- getSwingWidget().add(newButton.getSwingWidget());
- getSwingWidget().add(addressEdit.getSwingWidget());
- getSwingWidget().add(goButton.getSwingWidget());
- }
- public void handleGoToAddress() {
- emit(GO_TO_ADDRESS, addressEdit.getSwingWidget().getText(), "test string");
- }
- }
- public class TabBar extends JTabbedPane {
- /**
- * SLOT
- */
- public static final String HANDLE_NEW_TAB = "handleNewTab()";
- public static final String HANDLE_GO_TO_SITE = "goToSite(String,String)";
- public TabBar() {
- handleNewTab();
- }
- public void handleNewTab() {
- WebView tab = new WebView();
- add("blank", tab);
- }
- public void goToSite(String url, String testStr) {
- System.out.println("Receive url: " + url + ", " + testStr);
- WebView tab = (WebView) getSelectedComponent();
- tab.load(url);
- }
- }
- public class MainWindow extends JFrame {
- public static void main(String[] args) {
- JFrame window = new MainWindow();
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- window.setSize(320, 340);
- window.setVisible(true);
- }
- public MainWindow() {
- // 1.Create widget
- AddressBar addressBar = new AddressBar();
- TabBar tabBar = new TabBar();
- // 2.Set property
- // 3.Connect signal-slot
- QObject.connect(addressBar, AddressBar.NEW_BUTTON_CLICKED, tabBar, TabBar.HANDLE_NEW_TAB);
- QObject.connect(addressBar, AddressBar.GO_TO_ADDRESS, tabBar, TabBar.HANDLE_GO_TO_SITE);
- // 4.Add to layout
- GridBagLayout layout = new GridBagLayout();
- setLayout(layout);
- GridBagConstraints grid = new GridBagConstraints();
- grid.fill = GridBagConstraints.BOTH;
- grid.gridx = grid.gridy = 0;
- grid.weightx = 1.0;
- grid.weighty = 0.1;
- add(addressBar.getSwingWidget(), grid);
- grid.fill = GridBagConstraints.BOTH;
- grid.gridx = 0;
- grid.gridy = 1;
- grid.weightx = 1.0;
- grid.weighty = 0.9;
- add(tabBar, grid);
- }
- }
- @SuppressWarnings("serial")
- class WebView extends JEditorPane {
- public WebView() {
- setEditable(false);
- }
- public void load(final String url) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- try {
- WebView.this.setPage(url);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
- }
- }

http://blog.csdn.net/dc_726/article/details/7632430
Java实现Qt的SIGNAL-SLOT机制(保存到Map中,从而将它们关联起来,收到信号进行解析,最后反射调用)的更多相关文章
- kafka flumn sparkstreaming java实现监听文件夹内容保存到Phoenix中
ps:具体Kafka Flumn SparkStreaming的使用 参考前几篇博客 2.4.6.4.1 配置启动Kafka (1) 在slave机器上配置broker 1) 点击CDH上的kafk ...
- 【redis,1】java操作redis: 将string、list、map、自己定义的对象保存到redis中
一.操作string .list .map 对象 1.引入jar: jedis-2.1.0.jar 2.代码 /** * @param args */ public s ...
- Redis使用场景一,查询出的数据保存到Redis中,下次查询的时候直接从Redis中拿到数据。不用和数据库进行交互。
maven使用: <!--redis jar包--> <dependency> <groupId>redis.clients</groupId> < ...
- ffmpeg从AVFrame取出yuv数据到保存到char*中
ffmpeg从AVFrame取出yuv数据到保存到char*中 很多人一直不知道怎么利用ffmpeg从AVFrame取出yuv数据到保存到char*中,下面代码将yuv420p和yuv422p的数 ...
- 将数字n转换为字符串并保存到s中
将数字n转换为字符串并保存到s中 参考 C程序设计语言 #include <stdio.h> #include <string.h> //reverse函数: 倒置字符串s中各 ...
- Android把图片保存到SQLite中
1.bitmap保存到SQLite 中 数据格式:Blob db.execSQL("Create table " + TABLE_NAME + "( _id INTEGE ...
- c# 抓取和解析网页,并将table数据保存到datatable中(其他格式也可以,自己去修改)
使用HtmlAgilityPack 基础请参考这篇博客:https://www.cnblogs.com/fishyues/p/10232822.html 下面是根据抓取的页面string 来解析并保存 ...
- Flask实战第43天:把图片验证码和短信验证码保存到memcached中
前面我们已经获取到图片验证码和短信验证码,但是我们还没有把它们保存起来.同样的,我们和之前的邮箱验证码一样,保存到memcached中 编辑commom.vews.py .. from utils i ...
- 1.scrapy爬取的数据保存到es中
先建立es的mapping,也就是建立在es中建立一个空的Index,代码如下:执行后就会在es建lagou 这个index. from datetime import datetime fr ...
随机推荐
- 在CentOS系统上将deb包转换为rpm包
转载自 http://www.heminjie.com/system/linux/1487.html deb文件格式本是ubuntu/debian系统下的安装文件,那么我想要在redhat/cento ...
- Dropout 理论基础与实战细节
Dropout: A Simple Way to Prevent Neural Networks from Overfitting 对于 dropout 层,在训练时节点保留率(keep probab ...
- apply plugin: 'idea' --- gradle idea
如果你的项目使用了Gradle作为构建工具,那么你一定要使用Gradle来自动生成IDE的项目文件,无需再手动的将源代码导入到你的IDE中去了. 如果你使用的是eclipse,可以在build.gra ...
- [Linux] ssh秘钥对免密码登陆
准备两台linux服务器 a和b , 在a上使用ssh命令登陆b服务器 , 并且不用 输入密码 1.在a服务器上,比如是root用户 ,进去/root/.ssh目录 ,没有就创建, 就是进入家目录的. ...
- End-to end provisioning of storage clouds
Embodiments discussed in this disclosure provide an integrated provisioning framework that automates ...
- 半监督学习(semi-supervised learning)
P(x)P(x,y)}⇒P(y|x) 自 P(x) 生成的无标签样本: 自 P(x,y) 生成的标记样本:
- NYOJ 24 素数的距离问题
素数的距离问题 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描写叙述 如今给出你一些数.要求你写出一个程序,输出这些整数相邻近期的素数,并输出其相距长度.假设左右有等距离长 ...
- 保存画面为图片 当前MFC保存该程序为图片 c++ vc
将屏幕保存为图片.使用vs2008编译通过. [cpp] view plaincopy #include "stdafx.h" #include <windows.h> ...
- [Songqw.Net 基础]WPF插件化中同步Style
原文:[Songqw.Net 基础]WPF插件化中同步Style 版权声明:本文为博主原创文章,未经博主允许可以随意转载 https://blog.csdn.net/songqingwei1988/a ...
- HTML5逐步实现
渐变 Context对象能够通过createLinearGradient()和createRadialGradient()两个方法创建渐变对象.这两个方法的原型例如以下: Object createL ...