Confusion QLabel and QPixmap - c++

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

Related

Can't move/rename/delete folder while some of its subfolders are expanded in QFileSystemModel

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

Reading file from working directory QtWebEngine

Hello I am trying to set the QWebEngine URL to an index.html file that is placed in the working directory.
I am trying to use the file by setting the URL to ./index.html but it cant seem to find the file.
Here is where my files are placed
content (Work directory)
main.cpp
content.pro
index.html
How can i open index.html through the QWebEngine without using the full system path?
here is my code
#include <QApplication>
#include <QWebEngineView>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QWebEngineView view;
view.setUrl(QUrl(QStringLiteral("file:///./index.html")));
view.resize(1024, 750);
view.show();
return app.exec();
}
Try moving the html file to your project build directory (you're currently keeping it inside the source directory). Then you can build your URL this way:
QUrl url = QUrl::fromLocalFile(QDir::currentPath() + "/index.html");
and set it to the view:
QWebEngineView view;
view.setUrl(url);
view.resize(1024, 750);
view.show();
From http://doc.qt.io/qt-5/qurl.html
qDebug() << QUrl("main.qml").isRelative(); // true: no scheme
qDebug() << QUrl("qml/main.qml").isRelative(); // true: no scheme
qDebug() << QUrl("file:main.qml").isRelative(); // false: has "file" scheme
qDebug() << QUrl("file:qml/main.qml").isRelative(); // false: has "file" scheme
Try: view.setUrl(QUrl(QStringLiteral("index.html")));
As p-a-o-l-o pointed out in his answer, you're likely building out-of-source, so your index.html file has to be in the folder where content.exe is created, not in the source folder.
To make this less complicated, and safer, Qt supports embedding files in the .exe via Qt Resource files (.qrc). These can easily be created in Qt Creator, and once added to the project, the embedded files are accessed via a qrc:/// prefix.
So in your sample code, after adding a .qrc file to your project and adding index.html to it, you would adjust your code like this:
view.setUrl(QUrl(QStringLiteral("qrc:///index.html")));
This has the advantage of working regardless of build type or location, and it's a lot simpler than trying to add a file copy step to your project file (or to manually copy the file each time)

Image is not shown in QGraphicsScene

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

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

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.