Now, I have 1 application, but I don't want to open application twice, so I using QShareMemory to detect application when open twice.
And my question is: how I show current application in screen when user open application the second ?
int main(int argc, char *argv[]) {
Application a(argc, argv);
/*Make sure only one instance of application can run on host system at a time*/
QSharedMemory sharedMemory;
sharedMemory.setKey ("Application");
if (!sharedMemory.create(1))
{
qDebug() << "123123Exit already a process running";
return 0;
}
/**/
return a.exec();
}
Thanks.
Here's another approach in pure Qt way:
Use QLocalServer and QLocalSocket to check the existence of application and then use signal-slot mechanism to notify the existing one.
#include "widget.h"
#include <QApplication>
#include <QObject>
#include <QLocalSocket>
#include <QLocalServer>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
const QString appKey = "applicationKey";
QLocalSocket *socket = new QLocalSocket();
socket->connectToServer(appKey);
if (socket->isOpen()) {
socket->close();
socket->deleteLater();
return 0;
}
socket->deleteLater();
Widget w;
QLocalServer server;
QObject::connect(&server,
&QLocalServer::newConnection,
[&w] () {
/*Set the window on the top level.*/
w.setWindowFlags(w.windowFlags() |
Qt::WindowStaysOnTopHint);
w.showNormal();
w.setWindowFlags(w.windowFlags() &
~Qt::WindowStaysOnTopHint
);
w.showNormal();
w.activateWindow();
});
server.listen(appKey);
w.show();
return a.exec();
}
But if you're using Qt 5.3 on Windows, there's a bug for QWidget::setWindowFlags and Qt::WindowStaysOnTopHint, see https://bugreports.qt.io/browse/QTBUG-30359.
Just use QSingleApplication class instead of QApplication:
https://github.com/qtproject/qt-solutions/tree/master/qtsingleapplication
int main(int argc, char **argv)
{
QtSingleApplication app(argc, argv);
if (app.isRunning())
return 0;
MyMainWidget mmw;
app.setActivationWindow(&mmw);
mmw.show();
return app.exec();
}
It is part of Qt Solutions: https://github.com/qtproject/qt-solutions
Related
I am building a home security system with RPi and WebRTC. I simply need a way to trigger a browser to open at a given URL and to auto-grant access to the Microphone and Camera. I had hoped to use the WebEngine library with PyQt but WebEngine is not supported in PyQt for RPi. So I am trying Qt itself now. Unfortunately I am not familiar with C++, so i am struggling.
The example here has 90% of what I need. The code is replicated below. I just need to tweak it to grant access to the mic and camera when it is requested. I am hoping someone can assist me with this?
#include <QApplication>
#include <QWebEngineView>
QUrl commandLineUrlArgument()
{
const QStringList args = QCoreApplication::arguments();
for (const QString &arg : args.mid(1)) {
if (!arg.startsWith(QLatin1Char('-')))
return QUrl::fromUserInput(arg);
}
return QUrl(QStringLiteral("https://www.qt.io"));
}
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QWebEngineView view;
view.setUrl(commandLineUrlArgument());
view.resize(1024, 750);
view.show();
return app.exec();
}
I answered this question but for PyQt5: Grant access to Cam & Mic using Python for PyQt WebEngine, I will only do a C ++ translation to Python, the base is the same.
#include <QApplication>
#include <QUrl>
#include <QWebEngineView>
class WebEnginePage: public QWebEnginePage{
Q_OBJECT
public:
WebEnginePage(QObject *parent = Q_NULLPTR):QWebEnginePage(parent){
connect(this, &WebEnginePage::featurePermissionRequested, this, &WebEnginePage::onFeaturePermissionRequested);
}
private Q_SLOTS:
void onFeaturePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature){
if(feature == QWebEnginePage::MediaAudioCapture
|| feature == QWebEnginePage::MediaVideoCapture
|| feature == QWebEnginePage::MediaAudioVideoCapture)
setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser);
else
setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionDeniedByUser);
}
};
QUrl commandLineUrlArgument()
{
const QStringList args = QCoreApplication::arguments();
for (const QString &arg : args.mid(1)) {
if (!arg.startsWith(QLatin1Char('-')))
return QUrl::fromUserInput(arg);
}
return QUrl(QStringLiteral("https://www.qt.io"));
}
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QWebEngineView view;
view.setPage(new WebEnginePage);
view.setUrl(commandLineUrlArgument());
view.resize(1024, 750);
view.show();
return app.exec();
}
#include "main.moc"
I am using the Urho3D engine with Qt for an application. The problem is that both Urho3D and QApplication require to be ran from main(). For now I am using it in separate processes but IPC makes it complicated.
Is there any way to solve this issue? Thanks
My platform is Urho3D 1.5, Qt 4.71 and Windows 7 x64 and VS2015 (C++)
I'm new to both c++ and Urho3D, but I've successfully achieved it.
Simple code, haven't had further test:
awidget.h:
#ifndef AWIDGET_H
#define AWIDGET_H
#include <QWidget>
#include <QPushButton>
#include <Urho3D/Engine/Application.h>
class aWidget : public QWidget
{
Q_OBJECT
public:
explicit aWidget(QWidget *parent = 0)
{
QPushButton *button = new QPushButton(this);
connect(button, SIGNAL(clicked()), this, SLOT(pressed()));
}
public slots:
void pressed()
{
Urho3D::Context* context = new Urho3D::Context();
Urho3D::Application *application = new Urho3D::Application(context);
application->Run();
}
};
#endif // AWIDGET_H
main.cpp:
#include <QApplication>
#include <awidget.h>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
aWidget *widget = new aWidget();
widget->show();
return app.exec();
}
By the way, I'm using Qt 5.9.0
So the answer is quite simple. Instead of running QApplication by calling
app->exec();
it is needed to manually and regularily call this from your main loop:
app->processEvents();
This will take care that all events used by Qt are processed and QApplication will respond accordingly.
Example:
#include <QApplication>
#include <awidget.h>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
bool shallrun = true;
aWidget *widget = new aWidget();
widget->show();
while (shallrun)
{
app->processEvents();
...
}
...
}
I tried to use qApp->exit() to exit application and close UI. but I failed the UI is still there after qApp->exit() executed. Anyone can help to figure out why? thanks a lot.
#include "clsDownloadUpdateList.h"
#include <QApplication>
#include <qtranslator.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTranslator translator;
translator.load("en-CN_upgrader");
qApp->installTranslator(&translator);
clsDownloadUpdateList w;
w.show();
return a.exec();
}
clsDownloadUpdateList::clsDownloadUpdateList(QWidget *parent) :
QMainWindow(parent),
_state(STOP),
ui(new Ui::clsDownloadUpdateList)
{
ui->setupUi(this);
this->setWindowTitle("GCS Upgrader");
// other code
// here comes the code to exit application
qApp->exit();
// but the UI is still there.
}
#thuga is right. The problem you have is caused by your wrong code: you call qApp->exit() before right in your constructor, where your application has not started it's message cycle yet (by a.exec()).
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTranslator translator;
translator.load("en-CN_upgrader");
qApp->installTranslator(&translator);
clsDownloadUpdateList w; // <- you call qApp->exit() right here, before a.exec();
w.show();
return a.exec();
}
It will not help you in constructor, because of no event loop started yet.
In such case you can use QTimer::singleShot() with timeout equal zero. It will cause calling what you need when event loop started. Also it is good idea to use initialization method and check it in main:
Window w;
if ( !w.init() )
return 1;
w.show();
return a.exec();
Working code:
#include <QMetaObject>
//...
QMetaObject::invokeMethod(qApp, "quit",
Qt::QueuedConnection);
Or for widgets:
QMetaObject::invokeMethod(this, "close",
Qt::QueuedConnection);
I used in my Qt program code to avoid opening second instance:
#include "mainwindow.h"
#include <QApplication>
#include <QSharedMemory>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
const char* MEM_KEY = "42";
QSharedMemory sharedMem(MEM_KEY);
if (sharedMem.create(1024))
{
qDebug() << "Create shared memory";
}
else
{
if (sharedMem.error() == QSharedMemory::AlreadyExists)
{
qWarning() << "Already create. Exiting process";
return 1;
}
}
MainWindow w;
w.setWindowFlags(Qt::MSWindowsFixedSizeDialogHint);
w.show();
return a.exec();
It works (this code block opening second instance of my aplication [it is automatically close]), but I want to send an message or signal from opened for a moment second instance to first instance to perform for ex. maximalize window of first instance. Could you tell me how to do this simply?
You can use QtSingleApplication for this purpose. For example:
int main(int argc, char *argv[])
{
QtSingleApplication a(argc, argv);
if (a.isRunning())
{
a.sendMessage("42");
qWarning() << "Already create. Exiting process";
return 1;
}
MainWindow w;
a.setActivationWindow(&w);
QObject::connect(&a, SIGNAL(messageReceived(QString))
, &w, SLOT(onMessageReceived(QString)));
w.show();
int ret = a.exec();
QObject::disconnect(&a, 0, &w, 0);
return ret;
}
...
void MainWindow::onMessageReceived(const QString &message)
{
// Do stuff
}
You are looking for IPC (inter process communication) and sadly, i don't think there is a portable way of doing this, except for creating a socket and listening on 127.0.0.1. But if you are on unix i do recommend using qdbus. That is what all the cool linux programs are doing ;)
On windows there is i believe something similar. (But this is all not built into qt.) You can find the handle of your window (HWND) and use sendmessage().
to send a custom message you'll have to declare your own WM_SECONDINSTANCE (or something similar) by:
#define WM_SECONDINSTANCE WM_USER
#define WM_SOMEOTHERMESSAGE WM_USER+1
or use an enum (i'm lazy).
This tells your existing instance that another instance has been opened. To handle it in qt have a look at this.
To find the HWND, i would just put it int your shared memory from your first instance.
(My windows-knowledge is a bit rusty, so sorry for any errors)
i got this main;
#include <QtGui>
#include <iostream>
using namespace std;
#include "tray.h"
void main(int argc, char *argv[])
{
QApplication app(argc, argv);
Tray iets;
app.exec();
}
when i open in tray something like;
QFileDialog *dialog = new QFileDialog;
QString dir;
QString test = dialog->getOpenFileName(NULL, NULL, NULL, "Battlefield (*.exe)", NULL, NULL);
for(int i=0; i<test.split("/").size()-1; i++)
dir+= test.split("/").at(i) + "/";
ui->lePath->setText(test);
and i choosed the file its terminating another thread / the program.
how to fi xit?
I don't know (and can't guess) what your Tray class is.
However, Qt usually terminate the program when the last displayed window (QWidget instance) is closed. Unless specified otherwise.
If Tray is not a window (a child class of QWidget), then app.exec() has no message loop to process and returns immediately, thus terminating the program.
What would you expect/what do you want your program to do at this point exactly ?
Not directly related but still important:
Your main() function really should return an exit status. You can simply change your main() so that it looks like:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Tray iets;
return app.exec(); // app.exec() returns an exit status.
}