在Windows下,Flex可以通过ActiveX方式通过ExterneralInterface接口与Qt通信,但是在Linux下,ExternalInterface方法就无效了,只能通过Socket方式与Qt通信。进行通信的两端Flex与Qt,需要绑定指定的端口,监听端口发送的数据(字符串),双方做出相应的响应。下面展示一个Qt与Flex进行文字交互的实例,如下图所示。
在QT端,首先在主窗口中调用其成员函数initSocket()初始化QTcpSocket连接,调用openSwf()函数初始化窗口部件QWebView,并将Flash嵌入到webView中。
- void Widget::initSocket()
-
- {
-
- process= new QProcess(this);
-
- m_server = new QTcpServer(this);
-
- if(true == (m_server->listen(QHostAddress::LocalHost,5150)))
-
- {
-
- qDebug() << "bandsuccess!";
-
- }
-
- connect(m_server, SIGNAL(newConnection()),this, SLOT(FlexConnected()));
-
- }
在initSocket()函数中,调用QTcpServer::listen()方法绑定IP和端口地址,并且将newConnection()信号与flash连接完毕FlexConnected()槽相关联。在FlexConnected()槽中,将readyRead()信号与处理TCP数据流接收和处理readPendingDatagrams()槽相关联。在Flex端,首先构造一个QTSocket对象,然后在其构造函数中初始化IP地址、端口和Flex提供的Socket对象,最后调用InitConnect()函数初始化连接。
- public function InitConnect():void
-
- {
- socket.connect(server,port);
-
-
-
- socket.addEventListener(Event.CLOSE,closeHandler);
-
-
-
- socket.addEventListener(Event.CONNECT,connectHandler);
-
- socket.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
-
- socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
- }
下面代码是整个工程的代码,包含QT部分和Flex部分。
QT部分代码:
-
-
- class Widget : public QWidget
-
- {
-
- Q_OBJECT
-
- public:
-
- explicit Widget(QWidget *parent = 0);
-
- ~Widget();
-
- void openSwf();
-
- void processTheDatagram(QByteArray array);
-
- private slots:
-
- void on_pushButton_clicked();
-
- void readPendingDatagrams();
-
- void FlexConnected();
-
- private:
-
- void initSocket();
-
- private:
-
- Ui::Widget *ui;
-
- QTcpServer *m_server;
-
- QTcpSocket *m_socket;
-
- QProcess* process;
-
- QString tempDir;
-
- QString tempFilename;
-
- };
-
-
-
-
-
- #define TEMP_DIR "/temp/"
-
- #define TEMP_FILE "QTFLEX/QTFLEX.html"
-
- #define REPLACE_STRING "{#AAA}"
-
- Widget::Widget(QWidget *parent) :
-
- QWidget(parent),ui(new Ui::Widget)
-
- {
-
- ui->setupUi(this);
-
- tempDir = qApp->applicationDirPath() + TEMP_DIR;
-
- qDebug()<< "tempDir: " << tempDir;
-
- tempFilename = tempDir + TEMP_FILE;
-
- qDebug() << "tempFilename" << tempFilename;
-
- initSocket();
-
- openSwf();
-
- }
-
- Widget::~Widget()
-
- {
-
- m_server->close();
-
- delete m_server;
-
- delete process;
-
- }
-
- void Widget::initSocket()
-
- {
-
- process = new QProcess(this);
-
- m_server = new QTcpServer(this);
-
- if(true == (m_server->listen(QHostAddress::LocalHost,5150)))
-
- {
-
- qDebug() << "band success!";
-
- }
-
- connect(m_server, SIGNAL(newConnection()),this, SLOT(FlexConnected()));
-
- }
-
-
-
- void Widget::FlexConnected()
-
- {
-
- qDebug() << "flash连接成功!";
-
- m_socket = m_server->nextPendingConnection();
-
- connect(m_socket,SIGNAL(readyRead()),this,SLOT(readPendingDatagrams()));
-
- }
-
- QByteArray CreatePolicy()
-
- {
-
- char szPolicy[1024] = "";
-
- memset(szPolicy,'\0',1024);
-
- strcat(szPolicy,"<?xml version='1.0'?>");
-
- strcat(szPolicy,"<cross-domain-policy>");
-
- strcat(szPolicy,"<site-control permitted-cross-domain-policies='all'/>");
-
- strcat(szPolicy,"<allow-access-from domain='*'to-ports='5150'/>");
-
- strcat(szPolicy,"</cross-domain-policy>");
-
- return QByteArray(szPolicy);
-
- }
-
- void Widget::readPendingDatagrams()
-
- {
-
- qDebug() << "准备接受!";
-
- QByteArray datagram;
-
- datagram = m_socket->readAll();
-
- m_socket->seek(0);
-
- if(0 ==(strcmp("<policy-file-request/>",datagram.constData() )))
-
- {
-
-
-
- qDebug() << "安全认证完毕!";
-
- QByteArray strPolicy = CreatePolicy();
-
- m_socket->write(strPolicy);
-
- m_socket->close();
-
- }
-
- else
-
- {
-
- processTheDatagram(datagram);
-
- }
-
- }
-
- void Widget::openSwf()
-
- {
-
-
-
- QWebSettings *set = QWebSettings::globalSettings();
-
- set->setAttribute(QWebSettings::JavascriptEnabled,true);
-
- set->setAttribute(QWebSettings::PluginsEnabled,true);
-
- set->setAttribute(QWebSettings::JavascriptCanOpenWindows,true);
-
- set->setAttribute(QWebSettings::DeveloperExtrasEnabled,true);
-
- QString dirPath = qApp->applicationDirPath();
-
- dirPath = dirPath.left(dirPath.lastIndexOf('/'));
-
- qDebug() << dirPath;
-
- ui->webView->load(QUrl(tempFilename));
-
- ui->webView->show();
-
- }
-
- void Widget::processTheDatagram(QByteArrayarr)
-
- {
-
- qDebug()<<"comming";
-
- QString s="";
-
- s.append(arr);
-
- ui->textEdit->append(s);
-
- }
-
- void Widget::on_pushButton_clicked()
-
- {
-
- QString str=ui->lineEdit->text();
-
- if(str==NULL || str=="")
-
- {
-
- return;
-
- }
-
- else
-
- {
-
- str="QT:"+str;
-
- ui->textEdit->append(str);
-
- m_socket->write(str.toAscii());
-
- m_socket->close();
-
- }
-
- }
Flex部分代码,在Flex3下编译通过:
阿科
2013年1月6日于北京邮电大学新科研楼302室