如何使用QtSingleApplication在其他窗口前激活应用程序?
我使用QtSingleApplication使我的qml应用程序成为单实例应用程序。 但是,我无法在其他窗口前激活应用程序 我使用来自github的最新应用程序代码,以下是我的主要方法:如何使用QtSingleApplication在其他窗口前激活应用程序?,qt,qml,Qt,Qml,我使用QtSingleApplication使我的qml应用程序成为单实例应用程序。 但是,我无法在其他窗口前激活应用程序 我使用来自github的最新应用程序代码,以下是我的主要方法: #include <QQmlApplicationEngine> #include <QApplication> #include <QtQml> #include <QtSingleApplication> int main(int argc, char *a
#include <QQmlApplicationEngine>
#include <QApplication>
#include <QtQml>
#include <QtSingleApplication>
int main(int argc, char *argv[])
{
QtSingleApplication app(argc, argv);
if (app.isRunning())
{
// I think I should do something here
return 0;
}
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
return app.exec();
}
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
应用程序应用程序(argc、argv);
如果(app.isRunning())
{
//我想我应该在这里做点什么
返回0;
}
qqmlaplicationengine;
engine.load(QUrl(QStringLiteral(“qrc:/qml/main.qml”));
返回app.exec();
}
如何使用qml实现它或以其他方式完美地实现单实例应用程序?
谢谢 否,isRunning()
表示程序已经在运行另一个实例,因此该实例应该再次退出
您需要向正在运行的实例发送一条消息,它必须将自身向前推进
请参见否,isRunning()
表示程序已在运行另一个实例,因此该实例应再次退出
您需要向正在运行的实例发送一条消息,它必须将自身向前推进
请参见main.cpp
QQmlApplicationEngine engine;
QObject* qmlObject = engine.rootObjects().at(0);
WindowsPlatformSpecific wps(qmlObject);
QObject::connect(&app, SIGNAL(messageReceived(const QString&)),
&wps, SLOT(handleMessage(const QString&)));
//////////////////////
void WindowsPlatformSpecific::handleMessage(const QString& message)
{
QObject* pp = this->parent();
QWindow* pWnd = qobject_cast<QWindow*>(pp);
if (!pWnd)
qInfo() <<"The winId is null";
WId winId = pWnd->winId();
SetForegroundWindowByWId(winId);
}
bool WindowsPlatformSpecific::SetForegroundWindowByWId(WId winId)
{
HWND hWnd = (HWND)winId;
if ( hWnd )
{
ShowWindow(hWnd, SW_SHOWNORMAL);
WINDOWINFO wi;
GetWindowInfo(hWnd, &wi);
bool bTopMost = ((wi.dwExStyle & WS_EX_TOPMOST) == WS_EX_TOPMOST);
SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
if (!bTopMost)
{
SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
}
SetForegroundWindow(hWnd);
return true;
}
return false;
}
main.cpp
QQmlApplicationEngine engine;
QObject* qmlObject = engine.rootObjects().at(0);
WindowsPlatformSpecific wps(qmlObject);
QObject::connect(&app, SIGNAL(messageReceived(const QString&)),
&wps, SLOT(handleMessage(const QString&)));
//////////////////////
void WindowsPlatformSpecific::handleMessage(const QString& message)
{
QObject* pp = this->parent();
QWindow* pWnd = qobject_cast<QWindow*>(pp);
if (!pWnd)
qInfo() <<"The winId is null";
WId winId = pWnd->winId();
SetForegroundWindowByWId(winId);
}
bool WindowsPlatformSpecific::SetForegroundWindowByWId(WId winId)
{
HWND hWnd = (HWND)winId;
if ( hWnd )
{
ShowWindow(hWnd, SW_SHOWNORMAL);
WINDOWINFO wi;
GetWindowInfo(hWnd, &wi);
bool bTopMost = ((wi.dwExStyle & WS_EX_TOPMOST) == WS_EX_TOPMOST);
SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
if (!bTopMost)
{
SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
}
SetForegroundWindow(hWnd);
return true;
}
return false;
}
谢谢你的回答。但它不起作用。因为我使用QML而不是QWidgt。我无法设置activeWindows,因此无法在其他窗口前激活应用程序。你的意思是你不能使用激活窗口帮助器方法,但你仍然可以对消息作出反应,并自己激活窗口<例如,代码>QWindow::requestActivate()谢谢您的回答。但它不起作用。因为我使用QML而不是QWidgt。我无法设置activeWindows,因此无法在其他窗口前激活应用程序。你的意思是你不能使用激活窗口帮助器方法,但你仍然可以对消息作出反应,并自己激活窗口<代码>QWindow::requestActivate()例如