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
Related
I'm developing a Qt application on Windows. To show the files of a specific folder, I'm using QTreeView together with QFileSystemModel. So far so good, but I came across a very specific issue which is driving me nuts: while I'm with a folder expanded in my application, I can't do anything with its parent folder.
I built a small project only to show this issue. Here's how I define my QFileSystemModel and apply it to my QTreeView:
QFileSystemModel *myModel = new QFileSystemModel;
myModel->setRootPath(myRootPath);
ui->treeView->setModel(myModel);
To exemplify my problem, check out this image
While I have "Test Folder 2" not expanded, I can do what I want to "Test Folder". I can rename, move, or even delete via Windows Explorer, and everything is applied to my program. However, when I expand "Test Folder 2", suddenly my "Test Folder" is not editable anymore. Windows says the folder is "open in another application".
I believe anyone can reproduce this issue with the three lines above, so I don't think it's a project specific problem. Does anyone knows why this is happening?
EDIT: Apparently this is a windows only problem. Just tried on linux and it worked just fine. Is this a NTFS problem? Any ideas?
You should try to set the read only property.
#include <QApplication>
#include <QTreeView>
#include <QFileSystemModel>
int main(int argc, char** args) {
QApplication app(argc, args);
auto myModel = new QFileSystemModel;
myModel->setReadOnly(true);
auto treeView = new QTreeView;
myModel->setRootPath("C:/Temp/A");
treeView->setModel(myModel);
treeView->show();
app.exec();
}
I want to be able to replace the existing standard icon with my own but I am unsure how to do so. I only started Qt one week ago so I don't know much about the application. In addition to this I looked at the Qt website but their method for adding an icon did not work on my computer.
Besides using a RC file (Windows) you may specify the icon directly using qmake.
win32: RC_ICONS = icon.ico
Make sure it detects the path correctly. In the provided case the icon resides in the project root directory right beside the project file.
For window icons you typically, if application wide applicable, use some method of QApplication.
...
QApplication app(argc, argv);
...
app.setWindowIcon(QIcon(":/icon");
...
which is consuming the resource file using the alias icon. Here is an example (res.qrc):
<RCC>
<qresource prefix="/">
<file alias="icon">icon.png</file>
</qresource>
</RCC>
More information on resources are here.
Perhaps try the function setWindowIcon function on the QWidget or QApplication.
i'am trying to deploy a QT application made in QT Creator which goes back to system tray when closed.
I have made a svg tray icon, when i run it from QT Creator, either in debug and in release mode under Window 7, the tray icon shows up, but when i copy everything to another directory to make a distributable archive from it, the tray icon does not show up anymore.
Of course i search already for solutions, but everything i have found yet, i have made.
So what i have:
trayicon.svg file in project root
qrc file created, adding trayicon.svg into the resource files root
in project .pro file: RESOURCES += resources.qrc
copied binary + necessary dll's to target directory
copied QT plugins imageformats/* to target dir imageformats
added QApplication a(argc, argv); a.addLibraryPath(a.applicationDirPath()); to main.cpp
that is everything i found so far, but still the system tray icon is not showing up
What am i missing?
(current qt 4.8 + current qtcreator by the way)
#netrom
code in MainWindow : QMainWindow constructor:
trayIcon = new QSystemTrayIcon(this);
showAction = new QAction(tr("&Show"), this);
connect(showAction, SIGNAL(triggered()), this, SLOT(show()));
quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(showAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
trayIcon->setContextMenu(trayIconMenu);
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
trayIcon->setIcon(QIcon(":trayicon.svg"));
trayIcon->show();
Create iconegines directory in your app directory (for example: c:\MyApp\iconengines).
Copy qsvgicon.dll to this new directory (for example: c:\MyApp\iconengines\qsvgicon.dll) from qt plugins directory (in my case it's c:\qt\5.4\mingw491_32\plugins\iconengines\qsvgicon.dll).
Copy QtSvg.dll to your app directory (for example: c:\MyApp\Qt5Svg.dll) from Qt bin directory (in my case it's c:\qt\5.4\mingw491_32\bin\Qt5Svg.dll).
P.S. I know I'm late, this answer is for those who will google same problem.
Again, way later for DuckDuckGo-ers:
If your icon is not in .svg you might need another solution. For my .ico system tray icon I had to copy <path-to-qt>\plugins\imageformats into the application folder. Actually only qico.dll was needed in that folder in my case, but you get my drift.
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
How do you set application icon for application made using Qt? Is there some easy way? It's a qmake-based project.
For Qt 5, this process is automated by qmake. Just add the following to the project file:
win32:RC_ICONS += your_icon.ico
The automated resource file generation also uses the values of the following qmake variables: VERSION, QMAKE_TARGET_COMPANY, QMAKE_TARGET_DESCRIPTION, QMAKE_TARGET_COPYRIGHT, QMAKE_TARGET_PRODUCT, RC_LANG, RC_CODEPAGE.
For Qt 4, you need to do it manually. On Windows, you need to create a .rc file and add it to your project (.pro). The RC file should look like this:
IDI_ICON1 ICON DISCARDABLE "path_to_you_icon.ico"
The .pro entry should also be win32 specific, e.g.:
win32:RC_FILE += MyApplication.rc
Verified in Linux (Qt 4.8.6) and Windows (Qt 5.6):
1) Add the icon file to your project folder;
2) In the main function call setWindowIcon() method. For example:
QApplication a(argc, argv);
a.setWindowIcon(QIcon("./images/icon.png"));
To extend Rob's answer, you can set an application icon for macOS by adding and modifying the following line onto your .pro file.
macx: ICON = <app_icon>.icns
Note that the ICON qmake variable is only meant to target macOS.
For Windows, use
RC_ICONS = <app_icon>.ico if you're attaching a .ico file
or RC_FILE = <app_icon>.rc if you want to attach your icon through a .rc file. (Be sure to add IDI_ICON1 ICON DISCARDABLE "myappico.ico" into the rc file. Indentation not mine.)
For further reading, see Setting the Application Icon.
For Qt5 on Windows, move your ico file to project folder and add this line to your .pro file:
RC_ICONS = myappico.ico
Offical link: https://doc.qt.io/qt-5/appicon.html
Now that Qt has upgraded to 5.0.1, there is new method to add an application icon. First, you need prepare a resource file, named as .qrc
1) Without Qt Designer, I assume there is a QMainWindow instance whose name is MainWin. You can use:
QIcon icon(":icon/app.icon");
MainWin.setWindowIcon(icon);
2) With Qt Designer, you can modify the property of QMainWindow. Choose icon resource from .qrc and insert it into the row of windowIcon.
The above method can be used in Qt4.7, Qt4.8.x.