I've been trying to use freeglut in a Qt project. Unfortunately when I use some glut function like 'glutWireSphere' or 'glutWireTorus' I get an error:
freeglut ERROR: Function called without first
calling 'glutInit'.
And when I try to run an application it immediately quits itself.
I don't know where exactly should I call 'glutInit'. I've installed freeglut according to this tutorial:
https://www.youtube.com/watch?v=M4fm-cHGoYU&index=1&list=LLkYBBRyDu3gfOojsRQOM3JQ
I've figured it out. It was quite simple actualy. I needed to use 'glutInit( & argc, argv )' in my main.cpp like this:
int main(int argc, char *argv[])
{
glutInit( & argc, argv );
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Related
I am using vtk QT, i've installed VTK, PCL libraries well, after run the project the realese mode; i found the following error.
QWidget: Must construct a QApplication before a QWidget
As the error message says, you will need to create a QApplication before you can instantiate and use a QWidget. For example:
#include <QApplication>
#include <QDialog>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDialog dialog;
dialog.setModal(true);
dialog.show();
return app.exec();
}
I am developing a GUI application, but whenever I am trying to close the application, it throws an error that "Must construct QGuiapplication first". My main is not returning exit code 0, so it's not exiting normally. I think some destructor is getting called twice but need some help here. I am attaching main.cpp code here for reference.
#include <QGuiApplication>
#include <QFontDatabase>
#include <QtWebEngine>
#include "ApplicationManager.h"
#include "AppLogger.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);
QGuiApplication app(argc, argv);
QtWebEngine::initialize();
app.setApplicationName("MCS3.0");
QFontDatabase::addApplicationFont(":/Fonts/Roboto.ttf");
#ifdef VERSION
app.setApplicationVersion(QString("%1").arg(VERSION));
logInfoMessage(app.applicationName()+app.applicationVersion()+" Started");
#endif
ApplicationManager::instance().run();
return app.exec();
}
The relevant part of the problem is inside ApplicationManager.h which was not exposed by OP.
I bet that it makes another instance of QApplication (or QGUIApplication or QCoreApplication).
How can I know this? It's partly a guess (as the name looks like) and partly result of the following test:
testQApp.cc:
#include <QtWidgets>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
{ QApplication app(argc, argv);
QLabel qLbl("The app in app");
qLbl.show();
app.exec();
}
return app.exec();
}
testQApp.pro:
SOURCES = testQApp.cc
QT = widgets
Compiled and tested in cygwin64 on Windows 10:
$ qmake-qt5 testQApp.pro
$ make
$ ./testQApp
When I quit the application, the issue occurs:
QApplication::exec: Please instantiate the QApplication object first
Segmentation fault (core dumped)
$
To make this complete, the relevant paragraph of doc. about QApplication:
For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time. For non-QWidget based Qt applications, use QGuiApplication instead, as it does not depend on the QtWidgets library.
Please, note that the emphasize is not done by me.
I just did a fresh install of Qt Creator so I have the latest version. I opened my project that I was working on from the previous install of Qt Creator. It complies without errors but seams to hang on this line:
QApplication a(argc, argv);
Here is the full main.cpp file:
#include "mainwindow.h"
#include <QApplication>
#include <QMouseEvent>
#include<QTextStream>
#include<QString>
int main(int argc, char *argv[]){
qDebug()<<1;
QApplication a(argc, argv);
qDebug()<<2;
MainWindow w;
w.show();
return a.exec();
}
When it runs my application will open displaying the window but nothing works.
Here is the applciation's output:
Starting /home/bandito/programming/build-etf-ipo-Unnamed-Debug/etf-ipo...
1
As you can see qDebug()<<1; is called but the application never makes it to qDebug()<<2;
I also tried replacing QApplication with QGuiApplication and QCoreApplication but both request the QApplication be called also.
I've made LONG research on the web but can't find anything clear.
I think the answer is obvious but i'm beginer in Qt.
Why this code doesn't work ? My windows just popup realy fast.
Main.cpp
#include <QApplication>
#include "test.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Test test;
return a.exec();
}
Test.cpp
#include "test.h"
Test::Test()
{
MainWindow w;
w.show();
}
And this one work (the window keep open) :
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Thank you !
The window closes because it is local variable of the Test constructor, and when the constructor exits, its destructor gets called, which closes it. You need to make the window object a member variable of the Test class.
when I compile my project, I get the following error.
error:Multiple definition of main()
what is the problem?
Here is the code:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
myserver server;
server.startserver();
return a.exec();
}
That means in your program you have at least two functions named main. Search for main in your source files and eliminate one or more (by renaming/refactoring for example).
You can have only one function called main in a C/C++ program.