Image is not shown in QGraphicsScene - c++

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

Related

QT 5.4, Unable to access Resource from code

I try to include style images of my app into a q-resource file.
When I include the file directly in the code, it work, but when i try to use QResource, it fail (do not load the file).
I have the resource file in the main directory:
AppFolder
|- main.cpp
|- darkstyle.qrc
|- darkstyle
|- WindowTitleBar.png
The following example print:
failed1
failed2
#include <QApplication>
#include <QResource>
#include <Qfile>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
bool ok= QResource::registerResource("darkstyle.qrc");
if (!ok) qDebug()<<"failed1";
QFile file(":/darkstyle/WindowTitleBar.png");
//QFile file("../AppFolder/darkstyle/WindowTitleBar.png"); //that work
if(!file.open(QFile::ReadOnly | QFile::Text)) qDebug()<<"failed2";
else file.close();
//return a.exec();
return 0;
}
Note: Qt creator by default create binaries (.exe) in a top folder: ../build-AppFolder_Qt_5_4_1_MSVC2013_64bit-Debug/debug/AppFolder.exe
The execution root path seem to be: ../build-AppFolder_Qt_5_4_1_MSVC2013_64bit-Debug
I tried most of possible combinations with execution paths.
Note2: Some examples use a .rcc file format, I have none of these, but that could be a clue.
Summary:
How to access a QResource file from inside a QT app?
EDIT 1: Content of qrc file:
<RCC>
<qresource prefix="/">
<file>darkstyle/WindowTitleBar.png</file>
<file>darkstyle/WindowTitleButton.png</file>
<file>darkstyle/WindowTitleButton1.png</file>
<file>darkstyle/WindowTitleButton2.png</file>
<file>darkstyle/WindowTitleButton3.png</file>
</qresource>
</RCC>
QResource::registerResource("darkstyle.qrc") registers the resource description. If you want to use resources dynamically like this you need to register the compiled resources themselves. Run rcc -binary darkstyle.qrc -o darkstyle.rcc and use QResource::registerResource("darkstyle.rcc")
Alternatively, compile the resources into your binary directly. Do do so, use RESOURCES += darkstyle.qrc in your .qrc, and leave out the QResource::registerResource.
The problem is related to an incompatibility of the given version of QT with MSVS2013.
The problem is solved by downloading another version of QT or visual studio.

Cannot open file in resources directory

I've tested every natural iteration that I can imagine right now but still can not successfully open a file with a QFile object. I've also tested placing the file in the ./resources directory and in the directory with the source file.
I've cleaned my project in between each test and restarted Qt Creator as well. I've also deleted my *.pro.user project configuration file and tested with new versions of that.
#include <QFile>
#include <QDebug>
...
//QFile input_file("./resources/testfile.txt");
//QFile input_file("qrc:/10_graph.txt");
//QFile input_file("testfile.txt");
QFile input_file(":/testfile.txt");
if (!input_file.exists()) {
qDebug() << "File does NOT exist";
exit(11);
}
Qt Creator >> About
Qt Creator 3.1.1 (opensource)
Based on Qt 5.2.1 (Clang 5.0 (Apple), 64 bit)
You should add a resource file to your project and add the testfile.txt to the resources. After that you can access the file from resources by :
QFile input_file(":/testfile.txt");
If you add a prefix to the resources it can be accessed by :
QFile file(":/somePrefix/new.txt");
If you put the file in a folder alongside the source directory and add it to the resources, you can access it by :
QFile file(":/folderName/new.txt");

C++ GUI Text Not Displaying

Basically I made a simple program to search a text file for a certain word. However my text file does not display in the textEdit Object it just displays it blank. I properly placed the text file into the resources and everything. I would really appreciate some input.
#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include "find.h"
#include "ui_find.h"
Find::Find(QWidget *parent) : QWidget(parent), ui(new Ui::Find)
{
ui->setupUi(this); //sets up user interface
getTextFile();
}
Find::~Find()
{
delete ui;
}
void Find::on_goButton_clicked()
{
QString word = ui->lineEdit->text(); //gets text and stores in word
ui->textEdit->find(word, QTextDocument::FindWholeWords);
}
void Find::getTextFile()
{
QFile myFile(":/textfile.txt"); //what file
myFile.open(QIODevice::ReadOnly); //opens file
QTextStream textStream(&myFile); //convert to stream
QString line = textStream.readAll(); //store into variable
myFile.close(); //close file
ui->textEdit->setPlainText(line); //display text
QTextCursor textCursor = ui->textEdit->textCursor(); //moves cursor into position
textCursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1); //moves cursor into position
}
The application output also has this in red letters QIODevice::read: device not open
Qt Creator's clean rebuild are broken. Just permanently delete the kit specific folder where qmake has kept the project specific makefiles.
Attempt to do build again. rcc will be invoked specific to .qrc files added, that were skipped by older makefile files, as qmake has to give new makefile files now.
I have Qt 5.4 and my code gave the same error, when it was working with MSVC2010 OpenGL as a compiler. I manually added MinGW 32bit to use it as compiler and it worked.
P.S. I have not installed MSVC2013 for my Qt 5.4., and it works sometimes with MSVC2010 OpenGL without error, but not in this case.
Make sure that you added qrc file in project. If you use qrc file as external source, you should register it by call QResource::registerResource("path to file") in main function.
I just solved this with same error/symptoms. The issue was that I did not have a working compiler for my build kit. I ended up having to manually select my Microsoft Visual C++ Compiler 11.0 (x86). I guess it wasn't able to auto-detect it. This seems to be a common error message for new windows users beginning Qt5.

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

qtcreator tray icon under window does not show up when deployed

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.