I have created a Qt interface, add a Qlabel and set pixmap to an image. imagelabel =
new QLabel(centralwidget);
imagelabel->setObjectName(QString::fromUtf8("imagelabel"));
imagelabel->setGeometry(QRect(20, 10, 371, 311));
imagelabel->setPixmap(QPixmap(QString::fromUtf8(":/liqi/kinect.png")));
this is the code displayed in ui_mainwindow.h
when i preview it using the Qtdesigner, the image can be displayed. But when i run using codeblocks, everything like buttons etc is fine but the image does not appear. Do i need to add anything into the section below ?
MainWindow::MainWindow(Tqt_interface* tqt, QWidget *parent ) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
":/liqi/kinect.png" ':' means you are trying to open image that is included as resource. Make sure u have .qrc file and it is added in .pro project file. Like:
RESOURCES += res.qrc
Also make sure that image is in the .qrc file.
Related
I have just started using Qt and I've been trying to get a single button to display an icon but for some reason I can't get it to work. This is my code
QPushButton* button = new QPushButton();
button->setIcon(QIcon("icon.png"));
button->show();
I don't know what I'm missing here, the icon.png and the .cpp file are both in the same folder.. What am I doing wrong?
Add icon folder to your resources.qrc and try this one:
setIcon(QIcon(QPixmap(":/icons/yourIcon")));
It works in my project
Description
I wrote following Qt code. And Make executable file for Windows (*.exe). But Image (JPG) is not shown in QGraphicsScene. And I had already checked whether the path to image is correct. In following code, MyQGraphicsScene is derived class inherits QGraphicsScene class.
I compiled same code in macOS. Then, Executable file (ELF) for macOS ran correctly. Image file was shown in its component. I feel starange that source code is same. But the result is different by environment.
Development environment
Windows 10
Qt version : 5.7.0
C++ Compiler : Microsoft Visual C++ Ver.14.0 (MSVC2015)
Source code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsRectItem>
#include <QMessageBox>
#include <QIcon>
#include "TrainingData.h"
#include "trainingDataMaker.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QGraphicsItem *curGItem;
QListWidgetItem *listItem;
ui->setupUi(this);
scene = new MyQGraphicsScene(QRectF(0, 0, 400, 400));
ui->graphicsView->setScene(scene);
listItem = new QListWidgetItem();
listItem->setIcon(QIcon("data/000001.jpg"));
ui->imgListWidget->addItem(listItem);
listItem = new QListWidgetItem();
listItem->setIcon(QIcon("data/000276.jpg"));
ui->imgListWidget->addItem(listItem);
ui->imgListWidget->setIconSize(QSize(300,100));
QPixmap pixmap;
QImage img("data/000001.jpg");
QTransform transForm;
pixmap = QPixmap::fromImage(img);
QGraphicsPixmapItem *imgPixmap = new QGraphicsPixmapItem(pixmap);
transForm = QTransform();
transForm.scale(0.1, 0.1);
imgPixmap->setTransform(transForm);
scene->addItem(imgPixmap);
//connect(scene, SIGNAL(changed(const QList<QRectF> &)), imgPixmap, SLOT(chgSize(const QList<QRectF> &)));
}
I compiled same code in macOS. Then, Executable file (ELF) for macOS
ran correctly. Image file was shown in its component. I feel starange
that source code is same. But the result is different by environment.
Since it works under MacOS/X but not under Windows, one likely explanation is that it's a distribution/environmental problem. In particular, perhaps your program can't find Qt's imageformat plugin (e.g. qjpeg4.dll or qjpeg5.dll) under Windows, and that is why the image is not being displayed.
To test that theory, you could copy the qjpeg*.dll plugin file into the same folder as your .exe file, and then re-run your program and see if that makes it behave better. Alternatively, if you don't want to mess with image format plugins, you could convert your .jpg file to another file format that Qt includes native (i.e. non-plugin-based) support for, such as BMP or PNG, and use that instead.
As result, Cause of this issue is my misunderstanding of current directory in case that the application is launched from IDE. Current directory is (a) in following diagram.
Directory structure before
[Output directory is specified in build configuration] - (a)
|- debug - (b)
| |- test.exe // executable file of this application
| |- data
| |-000001.jpg
|- release
Therefore, I should have located "data" directory includes image files in directory (a) as following diagram. I'm feeling strange this specification of IDE. Of course, Current directory is same diretory of exe file in case that application launch directly(Doublu-clicking exe file) .
Directory structure after
[Output directory is specified in build configuration] - (a)
|- debug - (b)
| |- test.exe // executable file of this application
|- release
|- data
|-000001.jpg
I made a simple QT application that display a gif image only. Althought I created a qrc file to store my gif image and icon. Then I set icon for the widget using Qt creator. Then I load gif using following code:
Welcome::Welcome(QWidget *parent) :
QWidget(parent),
ui(new Ui::Welcome)
{
ui->setupUi(this);
QMovie *movie = new QMovie(":/icon/rc/login.gif");
movie->scaledSize();
ui->label->setMovie(movie);
movie->start();
}
I tried to run this in QT Creator and everything is fine.
Then, I try to run it directly (I copy all neccessary libraries to the folder offcourse) but it is missing both gif and icon image.
Please help me to solve this. I appreciate your answer. Thanks
This is my .pro file
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QSSHTool
TEMPLATE = app
SOURCES += main.cpp\
welcome.cpp
HEADERS += welcome.h
FORMS += welcome.ui
RESOURCES += \
qsshresource.qrc
I found the solution.
Firstly, add this line into .pro file to allow gif plugin to be loaded
QTPLUGIN += gif
Then, when deploying your application, beside neccessary libraries, copy the qgif.dll to directory:
<executable_dir>/plugins/imageformats/qgif.dll
And it is done :) Hope it can help someone.
Simple! I have the same problem!
For me i just clicked on Qt Menu->Build->Run qmake.
After this i clicked on Qt Menu->Build->Build Project "..." Ctrl + B
It works for me...
I'm using QTCreator to build an application on Mac. At this time the application icon is the default one set by Qt Creator.
I would like to set one custom specific to my app.
What I have in in the .qrc file, I have added
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>images/logo.icns</file>
</qresource>
</RCC>
I have made a try on my main.cpp by adding
QIcon icon(":images/logo.icns");
QApplication app(argc, argv);
app.setWindowIcon(icon);
it doesn't work... I have tried to do it in another class in which I define how the window of the app is done
QIcon icon(":images/logo.icns");
QMainWindow *window = new QMainWindow();
window->setWindowTitle(QString::fromUtf8("PULS"));
window->resize(600, 600);
QWidget *centralWidget = new QWidget(window);
centralWidget->setWindowIcon(icon);
It's also not working.
The icon's path may be incorrect.
try to change
QIcon icon(":images/logo.icns");
to
QIcon icon(":/images/logo.icns");
cause of your .qrc file define the prefix:
<qresource prefix="/">
To set the icon for the executable file you need:
ICON = logo.icns
In your .pro file.
To set the window icon you need to call
QIcon icon(":/images/logo.icns");
centralWidget->setWindowIcon(icon);
Or try using a .png
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.