Unable to save settings with QSettings - qt

I have a very simple test project under Windows 7 with the following code only on the main window:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSettings>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSettings::setDefaultFormat(QSettings::IniFormat);
QSettings settings("./conf.ini");
settings.setValue("testconf", 123);
settings.sync();
}
MainWindow::~MainWindow()
{
delete ui;
}
I just made a project from scratch and the MainWindow is empty. For reference, I add the .pro content, though it should not be relevant:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = qsettingstest
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
I tried different paths for the settings file, using QStandardPaths::writableLocation(QStandardPaths::DataLocation), existing directories e.g. "c:\mydir". I tried also replacing QSettings settings("./conf.ini"); with QSettings *psettings = new QSettings("./conf.ini");, call sync or not call it, but:
The file is not created or modified (in case I create it manually before). Why?

You use 1 QString constructor for QSettings. This will set the Organization Name, not the file name (see https://doc.qt.io/qt-5/qsettings.html#QSettings, application name is defaulted to an empty QString).
This will store/read the settings from the windows registry. Check for a ./conf.ini key there
You want to use the QSetting constructor taking a filename and a format (see https://doc.qt.io/qt-5/qsettings.html#QSettings-3):
QSettings settings("./conf.ini", QSettings::IniFormat);

Related

Application name is displayed with its extension when QSystemTrayIcon's showMessage is used in Windows 10

I'm using QSystemTrayIcon to display notification in Windows 10.
Along with the notification, the application name is also displayed.
The problem here is the app name is displayed along with the extension(.exe).
How can the extension(.exe) be removed from the notification?
Try adding this line to your .pro file:
QMAKE_TARGET_DESCRIPTION = "Whatever"
It should change the process name (both in the notifications and in the task manager) to "Whatever".
More variables like this can be found here: Qmake Variables Documentation
Note from the documentation:
This is only utilized if the VERSION or RC_ICONS variable is set and the RC_FILE and RES_FILE variables are not set.
Step-by-step instructions for creating a test application
Create Qt Widgets Application project, containing a QWidget based widget
Create images directory in the project folder, put an icon file into it (for this example let it be icon.ico)
Add a resource file to the project
To that resource file add prefix /, then "Add Files", selecting ./images/icon.ico
In main.cpp change the code to following:
#include "widget.h"
#include <QApplication>
#include <QIcon>
int main(int argc, char *argv[])
{
QCoreApplication::setApplicationName(APP_NAME);
QApplication a(argc, argv);
a.setWindowIcon(QIcon(":/images/icon.ico"));
Widget w;
w.setWindowTitle(qApp->applicationName());
w.setWindowIcon(qApp->windowIcon());
w.show();
return a.exec();
}
In widget.cpp change code to following:
#include "widget.h"
#include "ui_widget.h"
#include <QSystemTrayIcon>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QSystemTrayIcon *trayIcon = new QSystemTrayIcon(qApp->windowIcon(), this);
trayIcon->show();
connect(trayIcon, &QSystemTrayIcon::activated, [=]() {
trayIcon->showMessage("Title", "Message");
});
}
Widget::~Widget()
{
delete ui;
}
To the bottom of the project (.pro) file add following:
DEFINES += APP_NAME=\\\"AppName\\\"
QMAKE_TARGET_DESCRIPTION = "Whatever"
win32:RC_ICONS += images/icon.ico
Save, run qmake (Build -> Run qmake), rebuild project
Start the application. Now the window title should be "AppName", which came from APP_NAME define, both window and tray icon - icon.ico, and the process name in task manager and notifications - "Whatever". You can make the app display a notification by clicking on the tray icon. Notification should look like this:

QML Components Library [duplicate]

I have 4 qml files and one main.cpp to load qml file.
Is it possible for me to create 1 dll file for those 4 qml file.
And use it in different application if so how to do that.
As already said, there is no need for embedding qml files only in a library. But of course you have the right to do all you want, even that. I know at least 2 ways to do that:
1. Create binary resource file
Prepare resource file containing qml files and then compile it:
rcc -binary plugin.qrc -o plugin.rcc
Now you can include this file into your application :
QResource::registerResource("plugin.rcc");
and use it as regular qrc file:
QResource::registerResource(qApp->applicationDirPath() + "/plugin.rcc");
QQuickView *view = new QQuickView();
view->setSource(QUrl("qrc:/qml/myfile.qml"));
Here qml/ is prefix in resource file.
2. Shared libraryAnother way is to create a shared library containing the same resource file. For example your plugin's shared library implements following interface:
interface.h
#ifndef PLUGIN_INTERFACE_H
#define PLUGIN_INTERFACE_H
#include <QString>
#include <QObject>
class PluginInterface
{
public:
virtual ~PluginInterface() {}
virtual QByteArray getQML(const QString &name) = 0;
};
#define PluginInterface_iid "org.qt-project.PluginInterface"
Q_DECLARE_INTERFACE(PluginInterface, PluginInterface_iid)
#endif
and its implementation is:
QByteArray PluginImpl::getQML(const QString &name)
{
QFile file(":/qml/" + name);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return QByteArray();
return file.readAll();
}
Now, in your application you load your plugin and get its resource as a string:
QDir pluginsDir(qApp->applicationDirPath());
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath("plugin.dll"));
QObject *plugin = pluginLoader.instance();
if (plugin) {
PluginInterface *pluginInstance = qobject_cast<PluginInterface *>(plugin);
if (pluginInstance) {
QByteArray content = pluginInstance->getQML("file1.qml");
QQuickView *view = new QQuickView();
QQmlComponent component(view->engine());
component.setData(content, QUrl());
QQuickItem *childItem = qobject_cast<QQuickItem*>(component.create());
childItem->setParentItem(view->contentItem());
QWidget *container = QWidget::createWindowContainer(view);
container->setFocusPolicy(Qt::TabFocus);
ui->verticalLayout->addWidget(container);
}
}
But pay attention, when you deploy your application you anyway have to copy all qml system files, like #QTPATH/qml/QtQml, #QTPATH/qml/QtQuick.2, #QTPATH/qml/QtQuick.2 etc.
Links:
Resource compiler
Same theme
Plugin example
Have a look at the documentation for QML Modules
There are options for QML-only modules, C++ only and mixed mode.

How create shared library in QT/QML

I have 4 qml files and one main.cpp to load qml file.
Is it possible for me to create 1 dll file for those 4 qml file.
And use it in different application if so how to do that.
As already said, there is no need for embedding qml files only in a library. But of course you have the right to do all you want, even that. I know at least 2 ways to do that:
1. Create binary resource file
Prepare resource file containing qml files and then compile it:
rcc -binary plugin.qrc -o plugin.rcc
Now you can include this file into your application :
QResource::registerResource("plugin.rcc");
and use it as regular qrc file:
QResource::registerResource(qApp->applicationDirPath() + "/plugin.rcc");
QQuickView *view = new QQuickView();
view->setSource(QUrl("qrc:/qml/myfile.qml"));
Here qml/ is prefix in resource file.
2. Shared libraryAnother way is to create a shared library containing the same resource file. For example your plugin's shared library implements following interface:
interface.h
#ifndef PLUGIN_INTERFACE_H
#define PLUGIN_INTERFACE_H
#include <QString>
#include <QObject>
class PluginInterface
{
public:
virtual ~PluginInterface() {}
virtual QByteArray getQML(const QString &name) = 0;
};
#define PluginInterface_iid "org.qt-project.PluginInterface"
Q_DECLARE_INTERFACE(PluginInterface, PluginInterface_iid)
#endif
and its implementation is:
QByteArray PluginImpl::getQML(const QString &name)
{
QFile file(":/qml/" + name);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return QByteArray();
return file.readAll();
}
Now, in your application you load your plugin and get its resource as a string:
QDir pluginsDir(qApp->applicationDirPath());
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath("plugin.dll"));
QObject *plugin = pluginLoader.instance();
if (plugin) {
PluginInterface *pluginInstance = qobject_cast<PluginInterface *>(plugin);
if (pluginInstance) {
QByteArray content = pluginInstance->getQML("file1.qml");
QQuickView *view = new QQuickView();
QQmlComponent component(view->engine());
component.setData(content, QUrl());
QQuickItem *childItem = qobject_cast<QQuickItem*>(component.create());
childItem->setParentItem(view->contentItem());
QWidget *container = QWidget::createWindowContainer(view);
container->setFocusPolicy(Qt::TabFocus);
ui->verticalLayout->addWidget(container);
}
}
But pay attention, when you deploy your application you anyway have to copy all qml system files, like #QTPATH/qml/QtQml, #QTPATH/qml/QtQuick.2, #QTPATH/qml/QtQuick.2 etc.
Links:
Resource compiler
Same theme
Plugin example
Have a look at the documentation for QML Modules
There are options for QML-only modules, C++ only and mixed mode.

In Qt, accessing extern variable from plugin, that was defined in main application

It gives undefined symbol error when accessing the extern variable from plugin
I have header file globals.h
#ifndef GLOBALS_H
#define GLOBALS_H
extern int globalNumber;
#endif // GLOBALS_H
which is included in both main application and plugin;
in main application mainwindow.cpp
#include "mainwindow.h"
int globalNumber=12345;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
qDebug()<<"read variable from main app:"<<globalNumber;
loadPlugin();/*loads plugin correctly with QPluginLoader using an interface.h containing virtual void run()=0;
and calls run() after the plugin loading;*/
}
The plugin also have same globals.h
Inside plugin.cpp
#include<plugin.cpp>
void plugin::run()
{
qDebug()<<"read variable from plugin:"<<globalNumber;//if i comment this plugin get loaded without error
}
I get the application output as
read variable from main app:12345
Cannot load load library .../libplugin.so undefined symbol: globalNumber
Is there any other way to achieve this? Can it be done with singleton pattern or qglobalstatic? An example will be really helpfull. :)

Nokia Qt: How to Play video from Phone Memory?

Can anyone tell me how to play video from phone memory??
EDITED :i have use this code for video Playing...
include "playvideo.h"
include "ui_playvideo.h"
include QFileDialog
include phonon/backendcapabilities.h
include phonon/videoplayer
include "mainwindow.h"
PlayVideo::PlayVideo(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::PlayVideo)
{
ui->setupUi(this);
videoPlay();
}
void PlayVideo::videoPlay()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"), QDir::homePath());
Phonon::VideoPlayer *player = new Phonon::VideoPlayer(Phonon::VideoCategory,ui->graphicsView );
connect(player, SIGNAL(finished()), player, SLOT(deleteLater()));
player->play(fileName);
}
but it gives me error:
undefined reference to -> Phonon::VideoPlayer(Phonon::VideoCategory,QWidget*)
undefined reference to -> Phonon::VideoPlayer(Phonon::Mediasource const&)
Any idea?
Thanks..
Use either Phonon or QtMultimediaKit APIs.
For Phonon, there is a demo application in the Qt source tree (demos/qmediaplayer).
QtMultimediaKit is part of the QtMobility project, so in order to use it you require both Qt and QtMobility to be installed (in your SDK, and on the target device). There is a demo application in the QtMobility source tree (demos/mediaplayer).

Resources