QApplication Execution Segmentation Fault Error - c++

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.

Related

Qt , how to create QApplication without argc and argv

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();
}

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

Program crashes when play video with QWebView

Here is the code
#include <QApplication>
#include <QtWebKitWidgets/QWebView>
int main(int argc, char * argv[])
{
QApplication app(argc, argv);
QWebView view;
QCoreApplication::addLibraryPath("./plugins");
view.settings()->setAttribute(QWebSettings::PluginsEnabled, true);
// crash here
view.load(QUrl("http://hon.qq.com/act/20140320video/Aluna.html"));
// OK to show youtube
//view.load(QUrl("http://www.youtube.com/watch?v=KMU0tzLwhbE"));
view.show();
return app.exec();
}
It crashes when play video on hon.qq.com, but works well playing video on Youtube.

Qt Server Client Code

I am a new to the QT programming. my server/client codes are quite simple but they are not working.......
pls have a look to find problems in my codes, thanks.
SERVER:
int main(int argc, char** argv)
{
// QApplication app(argc, argv);
// Server server;
QTcpSocket *client_sock = NULL;
QTcpServer server;
server.listen(QHostAddress::Any,8888);
char buff[100];
while(1)
{
if(server.hasPendingConnections())
{
client_sock = server.nextPendingConnection();
}
if(client_sock)
{
qint64 n_rtn;
n_rtn = client_sock->bytesAvailable();
client_sock->readLine(buff,n_rtn);
std::cout<<buff;
}
}
// return app.exec();
}
CLIENT:
int main(int argc, char** argv)
{
// QApplication app(argc, argv);
QTcpSocket client;
QHostAddress addr("127.0.0.1");
client.connectToHost(addr,8888);
if(client.isWritable())
{
client.write("Hello World!\n");
}
client.close();
// return app.exec();
}
Thanks
Without a QApplication or a QCoreApplication and an app.exec() nothing will work. This is what runs the event loop which handles all the keyboard/mouse/network events.
Take a look at the chat and fortune cookie network server examples to see how to do this - it's almost as simple as the code you have written

qt c++ after close window terminating thread / program

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.
}