how to integrate qml file into qt-widgets app? - c++

I have an qt widgets application. I want to add the switch control to my form.
I added to my form QDeclarative widget and add this line to the code but nothing shown.
ui->declarativeView->setSource(QUrl::fromLocalFile("test.qml"));
This is the content of the qml file (I added this file to the resources)
It display in qtcreator under resources/[project_name.qrc]/test.qml
import QtQuick 2.4
import QtQuick.Controls 1.3
Button{
text:aaaa
}
I added the pro file : qt += declarative
What am i doing wrong??
I'm using Qt 5.4.1 QtCreator 3.3.1.

1. QDeclarativeView is for older Qt versions. If you are porting an application to Qt 5 then you can refer this documentation.
2. For your application you can use the new class in Qt 5.x QuickView as shown below.
Create a layout in your ui. Or do it via code. Then add the view to the layout as shown below:
QQuickView *view = new QQuickView();
QWidget *container = QWidget::createWindowContainer(view, this);
container->setMinimumSize(300, 200);
container->setMaximumSize(300, 200);
view->setSource(QUrl("qrc:/test.qml")); // Fetch this url by right clicking on your resource file.
ui->verticalLayout->addWidget(container);
3. In the .pro file just add quick module:
+quick
4. Reference: Introducing QWidget::createWindowContainer()
5. Note: If you have to add a url to a resource file you have to use :/ refer here for more details.

Related

Unreal Engine 4 + Qt5 integration

I'm a bit new in UE and i'm searching a way to make UE works with Qt for some of my projects ( like robotic simulation / sea simulation ), btw I don't want to use Slate.
I'm currently working on Windows with VS2015.
Is anyone knows where to start, I mean, where to start looking for flags or lib adds ?
Or a better solution should be to add Qt as a static library ? as this.
if you use C++ for unreal & qt, this won't help you.
But if you use Python for unreal & qt :
You can add the unreal_qt module to your project. It works with unreal 5, and 4. The readme has step by step instructions on how to use it, with a python qt example.
Quickstart
Add the unreal_qt folder in your python path. See unreal docs
Use the following code snippet to create sample.py and add it to unreal python path.
# 1. SETUP - this step can automatically run on editor startup when added to your init_unreal.py
import unreal_qt
unreal_qt.setup()
# 2. CREATE WIDGET - create your qt widget
# every widget you make after setup won't block the editor & have unreal styling
from PySide2.QtWidgets import QLabel, QWidget, QVBoxLayout
w = QWidget()
layout = QVBoxLayout()
w.setLayout(layout)
layout.addWidget(QLabel("Hello World!"))
# 3. WRAP WIDGET - (optional) manage garbage collection, add darkbar, stay on top
unreal_qt.wrap(w)
# 4. SHOW WIDGET - if using stay on top this needs to run after the wrap stage
w.show()
import script in unreal with the Python terminal to run it.
import sample

QT resource is missing

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...

Qt - QFile not opening a .qss file

I'm trying to use an external stylesheet for my project and I'm having trouble opening it using the QFile class. I've imported it into the .qrc file, a portion of it looks like this:
<qresource prefix="stylesheets">
<file>Resources/Stylesheet.qss</file>
</qresource>
This is how I am opening and using the file:
QFile stylesheet(":/stylesheets/Resources/Stylesheet.qss");
if (stylesheet.open(QIODevice::ReadOnly | QIODevice::Text))
{
newGameDialog.setStyleSheet(stylesheet.readAll());
stylesheet.close();
}
What could be wrong with this? I am using Visual Studio 2013 with the latest Qt and the VS Qt Add-in. I've also set the project to support QML in "Qt Project Settings".
The .qss file looks like this (it works if set directly as a QString). I'm not sure if the "import" line is needed:
import Qt 5.3.1
QDialog
{
background-color: 'white';
}
It is reading it fine, but the style is not applying. Here it is in debug mode:
QDialog does not support "background-color", only "backgroud:".
(Further ideas if that doesnt work:
Maybe forgot to specify Q_OBJECT for newGameDialog class?
Alternatively use Qt Designer to create a QDialog, copy your stylesheet source in the stylesheet property field and see if it works or if the designer shows an error or does apply the style correctly when test-instanciating the dialog (Ctrl+R i think).)

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.

How to set application icon in a Qt-based project?

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.