Qt , how to create QApplication without argc and argv - c++

Hey so I need to export a qt application as a .dll and , so I dont want any arguments like argc and argv , but QApplication needs them , so i tried this
int main()
{
int c=1;
char** v = (char**)("ApplicationName");
QApplication app(c,v);
MainWindow window;
window.show();
return app.exec();
}
but I get segfault from QtCore... Can someone help me bypass the segfault and create the QApplication without needing argc and argv?
This didnt solve the problem cause it needs argv defined ...
QApplication app(argc, argv)

Try this:
int main()
{
char* args[] = { (char*)"AppName" };
QApplication app(1,args);
MainWindow window;
window.show();
return app.exec();
}

Related

How to hide QMainWindow and show splashcreen during startup?

I am trying to hide the MainWindow of my Qt desktop app during startup, and to show a splashscreen. Both only happens after the loading phase, even though I call both splash.show() and window.hide() before the loading phase. I tried to split loading phase and constructor, but result is the same. How can I achieve both before the loading phase ?
Update 1
To display the splash screen, I had to add a call to QApplication::processEvents()
Update 2
The black window was actually not the MainWindow, but a ghost window that popped because scrollArea->setVisible(true) was called in the constructor.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPixmap pixmap(QStringLiteral(":/ressources/icons/icon.png"));
QSplashScreen splash(pixmap);
splash.show();
MainWindow window; // this loads for 5-6 seconds
a.processEvents();
window.showLoginPrompt();
splash.finish(&window);
return a.exec();
}
Based on your code and some example I could make it run like you are trying to do.
You only need to call your promptLogin function instead.
#include <QApplication>
#include <QTimer>
#include <QSplashScreen>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSplashScreen *splash = new QSplashScreen;
splash->setPixmap(QPixmap("D:\\Projects\\SplashScreen\\TestSplashScreen\\splash.png"));
splash->show();
MainWindow mainWin;
QTimer::singleShot(2500, splash, SLOT(close()));
QTimer::singleShot(2500, &mainWin, SLOT(show()));
return app.exec();
}

QApplication Execution Segmentation Fault Error

Can anybodyy help me with this. I am trying run an application using the cmakefiles. on the main file of my program I get a segmentation fault when the program gets to the line of code to execute the QAppication. Here is the fragment code below:
int main(int argc, char** argv)
{
bool viewing;
parse_command_line( argc, argv );
#ifdef _GRAPHICS_
glutInit(&argc, argv);
#endif
if( viewing )
{
#ifdef _GRAPHICS_
QApplication application(argc, argv);
Viewer *viewer = new Viewer( 0, exp, argc, argv );
Interface *render = new Interface( 0, exp, viewer );
render->show();
return application.exec(); //this line causes the segmentation fault
delete viewer;
delete render;
#endif
}
}
Your problem is that the application object is destroyed prior to other objects that use it. You should leverage the C++ semantics to help you with that - and get rid of the abhorrent manual memory management while you're at it:
int main(int argc, char** argv)
{
bool viewing;
parse_command_line( argc, argv );
#ifdef _GRAPHICS_
glutInit(&argc, argv);
#endif
if (viewing) {
#ifdef _GRAPHICS_
QApplication app(argc, argv);
Viewer viewer( 0, exp, argc, argv );
Interface render( 0, exp, &viewer );
render.show();
return app.exec();
#endif
}
}
The above is necessary - thus you must make the change to avoid undefined behavior. But the fix may not be sufficient if your Viewer or Interface implementations have bugs.

How I show application when open application again Qt

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

Library requires QApplication. How to use QApplication in Qt Quick project?

I have a Qt Quick project and I just added some source files. When trying to build I get the error message:
QWidget: Cannot create a QWidget without QApplication
Since I have a Qt Quick project I use the QGuiApplication. QApplication is a subclass of QGuiApplication. How do I make QApplication available to the newly added sources? Or how do one solve it when one have a Qt Quick and a QWidget?
The source files are the QCustomPlot library which shows a graph.
EDIT:
main.cpp:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
//Register C++ classes with QML
qmlRegisterType<Bluetooth>("Bluetooth", 1, 0, "Bluetooth");
//Set start QML file
viewer.setMainQmlFile(QStringLiteral("qml/test/main.qml"));
//New Code:
// generate some data:
QWidget widget;
QCustomPlot * customPlot = new QCustomPlot(&widget);
QVector<double> x(101), y(101); // initialize with entries 0..100
for (int i=0; i<101; ++i)
{
x[i] = i/50.0 - 1; // x goes from -1 to 1
y[i] = x[i]*x[i]; // let's plot a quadratic function
}
// create graph and assign data to it:
customPlot->addGraph();
customPlot->graph(0)->setData(x, y);
// give the axes some labels:
customPlot->xAxis->setLabel("x");
customPlot->yAxis->setLabel("y");
// set axes ranges, so we see all data:
customPlot->xAxis->setRange(-1, 1);
customPlot->yAxis->setRange(0, 1);
customPlot->replot();
//New Code End
//Show GUI
viewer.showExpanded();
return app.exec();
}
Error:
QML debugging is enabled. Only use this in a safe environment.
QWidget: Cannot create a QWidget without QApplication
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
The key concept is QWidget::createWindowContainer(). Try the following code:
#include <QQuickView>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQuickView *view = new QQuickView();
QWidget *container = QWidget::createWindowContainer(view, this);
container->setMinimumSize(200, 200);
container->setMaximumSize(200, 200);
container->setFocusPolicy(Qt::TabFocus);
view->setSource(QUrl("qml/test/main.qml"));
...
}
You can find the details on the following posts:
Introducing QWidget::createWindowContainer()
Combining Qt Widgets and QML with QWidget::createWindowContainer()

How to start a GUI application in the background?

How can I prevent a QApplication with a QMainWindow from stealing the keyboard focus when it starts? I'd like it to become active only after clicking on or alt-tabbing to it.
You could use showMinimized() instead of show() for you main window.
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow w;
w.showMinimized();
return app.exec();
}