在网上看了好多例子,只有这个是可以的。

 

 

 

#include <QtGui/QApplication>
#include <QTextCodec>
#include <QSharedMemory>
#include "thermometer.h"
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
    QSharedMemory shared("myWeather");
    if(shared.attach())
    {
        QMessageBox::warning(NULL , QObject::tr("警告"),QObject::tr("程序已经运行"));
        return 0;
    }
    shared.create(1);
 
 
    Thermometer w;
    w.show();
 
    return a.exec();
}