Qt - QWidget: Cannot create a QWidget when no GUI is being used - c++

I'm trying to run a simple Qt program, and when doing so, I get a console window mentioning: QWidget: Cannot create a QWidget when no GUI is being used, and the second line This application has requested the Runtime to terminate....., and the .exe file thus stops working.
My .pro file looks as follows:
#-------------------------------------------------
#
# Project created by QtCreator 2011-04-02T07:38:50
#
#-------------------------------------------------
QT += core
QT += gui
TARGET = Hello
CONFIG += console
CONFIG += qt
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
Any ideas on that?
Thanks.

The problem is not with this .pro; it is most likely in main.cpp. Qt requires you to create a QApplication before creating any QWidget subclasses (as well as certain other classes, such as QPixmap). Your main function should begin with the line:
QApplication app(argc, argv);
and will probably end with a line like:
return app.exec();
In between these calls, you should create and show your main window.

I found that you can do it with a Qt Console project, but ofcourse it will not have the functionality of a console program when you are done with my edits.
First of all you need to exchange #include <QtCoreApplication> with #include <QApplication> in your main.cpp (where you start your application)
In the main(int,char**){
exchange QCoreApplication a(argc, argv); with QApplication a(argc, argv);
and in between QApplication and return a.exec you have your widget and other gui related stuff
and in the end you use return a.exec();}

I think I found where the issue is.
Since I'm using Qt Creator, and when creating a new project, I was choosing Qt Console Application instead of Qt Gui Application.

"QWidget: Cannot create a QWidget when no GUI is being used" happens when you application isn't QApplication instance.
From Qt docs:
QApplication specializes QGuiApplication with some functionality
needed for QWidget-based applications. It handles widget specific
initialization, finalization, and provides session management.
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.

From the docs,
the QApplication class manages the GUI application's control flow and main settings whilst
the QCoreApplication class provides an event loop for console Qt applications
I had the same problem, the default QT Console App uses the QCoreApplication instead of the QApplication to run the application.
Here is what i did to make it work
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget widget;
widget.show();
return a.exec();
}
And i did not change anything in my project file
QT += core
QT += gui
TARGET = Layouts
CONFIG += gui
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp

Related

qt simple browser with flash enabled

I am using the simple browser example and trying to add the flash player from my source folder instead of using the one from my computer.
I have modified the code as per the document
in my main.cpp before creating the QApplication I do the following:
argc = 3;
argv[1] = "--ppapi-flash-path=./PepperFlashPlayer/libpepflashplayer.so";
argv[2] = "--ppapi-flash-version=26.0.0.151";
QApplication app(argc, argv);
QWebEngineSettings::defaultSettings()->setAttribute(QWebEngineSettings::PluginsEnabled, true);
This works fine for Linux and flash loads from my source folder (I made sure of this by removing flash .so file from my linux)
BUT my problem is on mac when I do
argc = 3;
argv[1] = "--ppapi-flash-path=./PepperFlashPlayer/PepperFlashPlayer.plugin";
argv[2] = "--ppapi-flash-version=26.0.0.151";
QApplication app(argc, argv);
QWebEngineSettings::defaultSettings()->setAttribute(QWebEngineSettings::PluginsEnabled, true);
This does not load flash at all - I use this URL to check.
I have both the .plugin and .so in the PepperFlashPlayer folder in my source folder where main.cpp and the .pro files are.
What else do I need to do?
NOTE It does not even load the default flash player located at
/Library/Internet \Plug-Ins/PepperFlashPlayer/PepperFlashPlayer.plugin

How can I stop a QApplication from showing up in the dock?

I'm making a console application on OS X that interacts with specific parts of the Desktop Environment (mainly the mouse using QCursor), so I cannot use a QCoreApplication (despite how much I want to).
The application works fine, it is just that it shows up in the dock whenever I run it from the command line. I looked at several other questions online, but none fixed the problem I'm having.
I looked into QSystemTrayIcon, and I would be fine with using it IF it would get rid of the pesky window that pops up. Here is my code narrowed down to a minimum that still has the problem I addressed above.
The .pro:
TARGET = project
QT += core
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
QT -= gui
CONFIG += c++11
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
main.cpp:
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QCursor cur;
cur.setPos(0,0);
return a.exec();
}
A workaround would be doing it manually as describe here.
Luckily, if it is a cocoa application, you can hide the Dock icon yourself. To see if it is possible, right-click (Control-click) on the application icon. If "Show Package Contents" is in the menu that appears, you can hide the icon in the Dock.
If this is the case, select "Show Package Contents" and look for the "Info.plist" file inside the Contents folder. Open this file using TextEdit by right-clicking on it and choosing "Open With - Other" from the menu.
In the file, paste the following two lines just after on the 6th line:
<key>LSUIElement</key>
<string>1</string>
Save the file and close it. For the changes to take effect, you need to move the application to the desktop and them back to its original location (OS X keeps a cache of the file, so you need to trick it into checking it again).
Now when you open the application, no icon will appear in the Dock.
Source: http://www.macosxtips.co.uk/index_files/disable-the-dock-icon-for-any-application.php

Background-image in Qt stylesheet doesn't work

This should set the background to cats.jpg, but it doesn't do anything:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.setStyleSheet("background-image: url(images/ricepaper.png);");;
w.show();
return a.exec();
}
I thought that it could be a problem with the image location since changing the widget's background-color works fine, but the images folder is in the build-debug directory which I believe is the correct place. I've tried changing the image path, syntax, and styleSheet class and nothing has worked, any suggestions?
From Qt Style Sheets Reference:
It should work. Probably the image file is not to be found where the program expects. If you're using Qt Creator, you should be aware that it builds the target binary in a separate directory (usually with a name like build-yourprojectname-qtversion-Debug or so). That's called shadow build.
You'll need to copy over your image(s) to the proper location relative to that shadow build directory, otherwise the program will not be able to find the file.
Your best bet is to embed the image in the binary as a resource.
I just tried this and seems to be working for me:
In main.cpp:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *w = new QWidget;
w->setStyleSheet("background-image: url(:/resources/pixmaps/close.png);");
w->show();
return app.exec();
}
Note the :/ part in url(:/resources/pixmaps/close.png). That's needed for embedded resources.
In resources.qrc:
<RCC>
<qresource prefix="/">
<file>resources/pixmaps/close.png</file>
... other resource files go here
</qresource>
At the end of testproject.pro:
RESOURCES += \
resources.qrc
Of course you'll need to put the images in the proper location in your project directory so that the resource compiler can find it.
In my example,
resources/
├── pixmaps
│   ├── application.png
│   ├── cancel.png
│   ├── close.png <--Here
...

Confusion QLabel and QPixmap

I want to display a Pixmap into a Label on Qt.
Here is the problem, the above code doesn't display the image that I want.
Please correct me if something is wrong. I have checked again and again (even it's few lines) but it didn't lead to results!!!
QLabel *label;
label= new QLabel(this);
QPixmap jet("C:/images/image.jpg");
label->setPixmap(jet);
One other problem, it seems that it works in another machine!
By default, qt builds with “-system-libjpeg”, so sudo apt-get install libjpeg if you are on linux, and to check supporting image formats, add
qDebug() << QImageReader::supportedImageFormats ();
to add plugins to your project, go to QtSDK/Desktop/Qt/4.8.1/gcc/plugins or something like this, chose the version you use, and copy "plugins" folder to root of your project(more precisely - you need only "imageformats" folder, that lays within plugins), and in your main.cpp file add line addLibraryPath (by the way, plugin's folder contain not only dlls for release, but for debugging also, they are a bit heavy, so i would remove dlls with *d.dll, for example QtCore.dll - for release, QtCored.dll - for debugging)
QApplication a(argc, argv);
MainWindow w;
a.addLibraryPath("plugins");
w.show();

Does Meego Nokia N950 support C++ QML binding?

I want to communicate between C++ and QML on Meego. It happens using C++ QML binding as described in this link.
When I run an app on the Symbian platform it works, and data from C++ is available to manipulate in some QML file.
But on Meego it always says myItem.qml file not found.
See the following code snippet:
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QDeclarativeView view;
MyClass myClass;
view.rootContext()->setContextProperty("myObject", &myClass);
view.setSource(QUrl::fromLocalFile("MyItem.qml"));
view.show();
return app.exec();
}
Does Meego does not support QML C++ binding?
I also tried to give the exact path of the QML file but never found that file, even myItem is in project structure.
Is there some thing associated with path settings in .pro file etc
Please help on this as I am stuck here with this and cannot move forward in my app using Qt Quick.
Yes, but your problem isn't related to C++ and QML bindings. Your application (specifically the view) can't find the .qml file to load.
By default, I think on meego the default path to look in is ../qml. So by default you should be packaging into /opt/YOURNAME with the binary in /opt/YOURNAME/bin and the qml files in /opt/YOURNAME/qml.
However, you can also put the qml files into a resource and use the :/ paths to access a resource instead.
IE, in your resource file:
<RCC>
<qresource prefix="/">
<file>qml/foo.qml</file>
</qresource>
</RCC>
And in C++:
viewer.setSource(QUrl("qrc:/qml/foo.qml"));