赞
踩
Windows平台下进程间通信常用方式有管道、文件映射、Socket通信和共享内存等,这里详细介绍一下Qt的共享内存机制。
Qt官方的例子主要是一个客户端加载图片后,将图片存储到共享内存中,另一个客户端从共享内存中获取图片显示。
加载图片客户端效果:
获取图片客户端效果:
加载图片的相关代码:
void Dialog::loadFromFile() { if (sharedMemory.isAttached()) detach(); ui.label->setText(tr("Select an image file")); QString fileName = QFileDialog::getOpenFileName(0, QString(), QString(), tr("Images (*.png *.xpm *.jpg)")); QImage image; if (!image.load(fileName)) { ui.label->setText(tr("Selected file is not an image, please select another.")); return; } ui.label->setPixmap(QPixmap::fromImage(image)); //! [1] //! [2] // load into shared memory QBuffer buffer; buffer.open(QBuffer::ReadWrite); QDataStream out(&buffer); out << image; int size = buffer.size();</
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。