这里使用QSharedMemory可以实现信号与槽中跨界面传输数据的功能!
原理是:
发送界面获取到数据就写到SharedMemory中
而获取端,用个记时器,时时去多,奇淫技巧啊!
运行截图如下:
源码如下:
sharedwidget.h
1 #ifndef SHAREDWIDGET_H
2 #define SHAREDWIDGET_H
3
4 #include <QWidget>
5
6 QT_BEGIN_NAMESPACE
7 class QSharedMemory;
8 QT_END_NAMESPACE
9
10 namespace Ui {
11 class SharedWidget;
12 }
13
14 class SharedWidget : public QWidget
15 {
16 Q_OBJECT
17
18 public:
19 explicit SharedWidget(QWidget *parent = 0);
20 ~SharedWidget();
21
22 protected slots:
23 void LineEditTextChanged(QString text);
24
25 private:
26 Ui::SharedWidget *ui;
27 QSharedMemory *m_sharedMemory;
28 };
29
30 #endif // SHAREDWIDGET_H
widget.h
1 #ifndef WIDGET_H
2 #define WIDGET_H
3
4 #include <QWidget>
5
6 QT_BEGIN_NAMESPACE
7 class QSharedMemory;
8 QT_END_NAMESPACE
9
10 namespace Ui {
11 class Widget;
12 }
13
14 class Widget : public QWidget
15 {
16 Q_OBJECT
17
18 public:
19 explicit Widget(QWidget *parent = 0);
20 ~Widget();
21
22 protected:
23 void timerEvent(QTimerEvent *event) Q_DECL_OVERRIDE;
24
25 protected slots:
26 void btnClicked();
27
28 private:
29 Ui::Widget *ui;
30 QSharedMemory *m_sharedMemory;
31 int m_timerId;
32 };
33
34 #endif // WIDGET_H
main.cpp
1 #include "widget.h"
2 #include "sharedwidget.h"
3 #include <QApplication>
4
5 int main(int argc, char *argv[])
6 {
7 QApplication a(argc, argv);
8
9 Widget w;
10 w.show();
11
12 return a.exec();
13 }
sharedwidget.h
1 #include "sharedwidget.h"
2 #include "ui_sharedwidget.h"
3 #include <QSharedMemory>
4 #include <QDataStream>
5 #include <QBuffer>
6 #include <QDebug>
7
8 SharedWidget::SharedWidget(QWidget *parent) :
9 QWidget(parent),
10 ui(new Ui::SharedWidget)
11 {
12 ui->setupUi(this);
13 m_sharedMemory = new QSharedMemory("123456");
14 connect(ui->lineEdit, SIGNAL(textChanged(QString)),
15 this, SLOT(LineEditTextChanged(QString)));
16 }
17
18 SharedWidget::~SharedWidget()
19 {
20 delete ui;
21 }
22
23
24 void SharedWidget::LineEditTextChanged(QString text)
25 {
26 if(m_sharedMemory->isAttached()){
27 if(!m_sharedMemory->detach()){
28 return;
29 }
30 }
31
32 QBuffer buffer;
33 buffer.open(QBuffer::ReadWrite);
34 QDataStream out(&buffer);
35 out << text;
36
37 if(!m_sharedMemory->create(buffer.size())){
38
39 qDebug()<< "create failed!";
40 return;
41 }
42
43 m_sharedMemory->lock();
44 char *to = static_cast<char*>(m_sharedMemory->data());
45 const char *from =text.toStdString().c_str();
46 memcpy(to, from, qMin(m_sharedMemory->size(), (int)buffer.size()));
47 m_sharedMemory->unlock();
48 }
widget.cpp
1 #include "widget.h"
2 #include "ui_widget.h"
3 #include "sharedwidget.h"
4 #include <QDebug>
5 #include <QBuffer>
6 #include <QDataStream>
7 #include <QSharedMemory>
8
9 Widget::Widget(QWidget *parent) :
10 QWidget(parent),
11 ui(new Ui::Widget)
12 {
13 ui->setupUi(this);
14
15 m_sharedMemory = new QSharedMemory;
16 m_sharedMemory->setKey("123456");
17
18 m_timerId = startTimer(0);
19
20 connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(btnClicked()));
21 }
22
23 Widget::~Widget()
24 {
25 delete ui;
26 delete m_sharedMemory;
27 }
28
29
30 void Widget::timerEvent(QTimerEvent *event)
31 {
32 if(event->timerId() == m_timerId){
33
34 if(!m_sharedMemory->attach()){
35
36 return;
37 }
38
39 QBuffer buffer;
40 m_sharedMemory->lock();
41 buffer.setData((char*)m_sharedMemory->constData(), m_sharedMemory->size());
42 buffer.open(QBuffer::ReadOnly);
43 QString data = buffer.readAll();
44 m_sharedMemory->unlock();
45 m_sharedMemory->detach();
46 ui->label->setText(data);
47
48 }
49 }
50
51 void Widget::btnClicked()
52 {
53 SharedWidget *sharedWidget = new SharedWidget;
54 sharedWidget->show();
55 }