I am unable to reference QSettings using Qt - c++

I am running a project that uses Qt. I have installed version 5.10.1 (msvc2017_64). I receive an error that the QSettings is undefined. I cannot find the reference in the libraries installed.
#include "mainwindow.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QCoreApplication::setApplicationName("Circuit Editor");
QSettings settings;
MainWindow w;
w.setWindowTitle("Circuit Editor");
w.show();
return a.exec();
}

My two cents:
1) You forgot to include the header:
#include <QSettings>
2) You declared a QSetting object but you are not doing anything with it. Is that what you want to do?

Never use #include <QtModule/QClass> format of includes. They hide project misconfiguration. Use #include <QClass> instead.
If you're unsure of what to include, include the entire module and deal with details later: #include <QtModule>.
Thus:
#include "mainwindow.h"
#if 1
// entire module
#include <QtWidgets>
#else
// needed classes
#include <QApplication>
#include <QSettings>
#endif
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QCoreApplication::setApplicationName("Circuit Editor");
QSettings settings;
MainWindow w;
w.setWindowTitle("Circuit Editor");
w.show();
return a.exec();
}

Related

QSettings iniFormat

I am trying to create an ini file from my application (Qt5.9.6):
#include <QCoreApplication>
#include <QSettings>
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
QSettings settings(QStringLiteral("C:\\test.ini"), QSettings::IniFormat);
settings.setValue(QStringLiteral("global/mykey"), 1234);
settings.sync();
app.exec();
}
The file is created, however the content is not what I expect:
[global]
mykey="\x1\x1\x1\x1"
Looks like it was a bug introduced by a colleage into our custom built Qt library.

How do I use Objective-C variables and methods for a Qt window on macOS?

I have the following code in a Qt project, and I want to set the titlebarAppearsTransparent variable for a window to true in Objective-C. The program compiles correctly, but it crashes when it reaches [&w titlebarAppearsTransparent:YES]; Is what I'm trying to do even possible, and if so how do I fix it?
#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QDebug>
#include <QDir>
#include "globals.h"
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <AppKit/NSWindow.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QApplication::setOrganizationName("Siddha Tiwari");
QApplication::setApplicationName("NarwhalEdit");
MainWindow *w = new MainWindow();
[&w titlebarAppearsTransparent:YES];
setTheme(true);
w->show();
return a.exec();
}
It is possible to accomplish this using the native API, as reported here, obtaining the NSWindow pointer from QWidget::window()::winId().
I would also suggest to wrap the code with conditional compiling directives, so it is ignored when compiling for other platforms.
Here is a snippet (assuming w is the pointer to your QMainWindow):
#ifdef Q_OS_MAC
QCoreApplication::setAttribute( Qt::AA_DontCreateNativeWidgetSiblings );
NSView *nsview = ( __bridge NSView * )reinterpret_cast<void *>( w->window()->winId() );
NSWindow *nswindow = [nsview window];
nswindow.titlebarAppearsTransparent = YES;
#endif

How to create and display the MainWindow outside the Main()?

I've made LONG research on the web but can't find anything clear.
I think the answer is obvious but i'm beginer in Qt.
Why this code doesn't work ? My windows just popup realy fast.
Main.cpp
#include <QApplication>
#include "test.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Test test;
return a.exec();
}
Test.cpp
#include "test.h"
Test::Test()
{
MainWindow w;
w.show();
}
And this one work (the window keep open) :
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Thank you !
The window closes because it is local variable of the Test constructor, and when the constructor exits, its destructor gets called, which closes it. You need to make the window object a member variable of the Test class.

Undefined reference to QDeclarativePropertyMap in qt

I am new to Qt.
Qml is facing issue while passing QDeclarativePropertyMap from c++ to Qml.
Here is my code:
main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QtDeclarative/qdeclarativepropertymap.h>
#include <QVariant>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
///giving error in below line
QDeclarativePropertyMap *m_propertymap = new QDeclarativePropertyMap();
m_propertymap->insert("time" ,QVariant(QString("yet to do")));
m_propertymap->insert("area" ,QVariant(QString("india")));
engine.rootContext()->setContextProperty("M_propertymap", m_propertymap);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
Error:
undefined reference to QDeclarativePropertyMap QObject
Use QtQml/QQmlPropertyMap instead
undefined reference to QDeclarativePropertyMap QObject
is a linker error, cause you trying to mix different versions of qtquick

C++:QX11EmbedContainer not working on Qt

Given below is the code that I have written for opening VLC from Qt. I want to use the QX11EmbedContainer so that I can use it to "contain" VLC.
#include "mainwindow.h"
#include <QApplication>
#include <QProcess>
#include <QX11EmbedContainer>
using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString prog="C:/Users/Administrator/Desktop/... .exe";
QProcess *proc=new QProcess();
proc->start(prog);
return a.exec();
}
On adding the header file QX11EmbedContainer I get the following error:
C:\Users\Administrator\Documents\HA\main.cpp:4: error: C1083: Cannot open include file: 'QX11EmbedContainer': No such file or directory
Is QX11EmbedContainer an add-on? If not then where am I going wrong?