Cannot change QuickStyle in Qt 5.11 - qt

Hey I am using Qt Creator because there is no qml implementation in visual studio yet.
I am not able to change the style of quick components no matter what. I have tried it with global constants and as well as C++ API call but it still throws errors. I don't know what else to try.
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQuickControls2/QQuickStyle>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQuickStyle::setStyle(QLatin1String("Material"));
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
I stopped at this, I get these errors:
main.qml:6: error: Expected token `{'
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: static void __cdecl QQuickStyle::setStyle(class QString const &)" (__imp_?setStyle#QQuickStyle##SAXAEBVQString###Z) referenced in function main
debug\RandomGenerator.exe:-1: error: LNK1120: 1 unresolved externals
I don't know what to do, there are no more tutorials explaining this.

You can try to do this in qml.
enter this in qtquickcontrols2.conf
[Controls]
Style=Material
[Material]
Theme=Dark
If you want to change color or another properetys enter this in main.qml in AplicationWindow
Material.accent: Material.color(Material.Blue)
More info about styles and there properetys you can find here

Related

QT VS Tools how to use qt static class library correctly

I am trying to work on qt class libraries and I want to include a qt class library in a qt application.
Platform is visual studio 2022, Qt tools and Qt 6.4.1.
Application and class library I created via project wizard.
I have included the class library via "Verweise" and added the path to the header and library path in the QT project settings.
VS Project:
QT project settings
Testlib.h
#pragma once
#include "testlib_global.h"
class TESTLIB_EXPORT TestLib
{
public:
TestLib();
void SayHello();
};
Testlib_global.h
#pragma once
#include <QtCore/qglobal.h>
#ifndef BUILD_STATIC
# if defined(TESTLIB_LIB)
# define TESTLIB_EXPORT Q_DECL_EXPORT
# else
# define TESTLIB_EXPORT Q_DECL_IMPORT
# endif
#else
# define TESTLIB_EXPORT
#endif
Testlib.cpp
#include "TestLib.h"
TestLib::TestLib()
{
}
void TestLib::SayHello()
{
}
Main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtCore/qglobal.h>
#include "testlib.h"
int main(int argc, char *argv[])
{
#if defined(Q_OS_WIN)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
TestLib* lib = new TestLib();
lib->SayHello();
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
After compiling I get the following errors
Severity Code Description Project File Line Suppression State
Error LNK2019 Reference to unresolved external symbol ""__declspec(dllimport) public: __cdecl TestLib::TestLib(void)" (__imp_??0TestLib##QEAA#XZ)" in function "main". TestApp C:\Users\arne\workspace4\TestApp\main.obj 1
Severity Code Description Project File Line Suppression state
Error LNK2019 Reference to unresolved external symbol ""__declspec(dllimport) public: void __cdecl TestLib::sayHello(void)" (__imp_?sayHello#TestLib##QEAAXXZ)" in function "main". TestApp C:\Users\arne\workspace4\TestApp\main.obj 1
Two unresolved external
Probably this is a beginner's problem - I'm just switching from c# to c++ due to the project and it's much more complex.
Since the application itself is already very complex i wanted to at least create the structures correctly right at the beginning and for that i need several libraries
Thanks for any support
Testlib project settings

QML start external program: use QProcess fail build [duplicate]

I wrote a little program with a my own class within the main.cpp. Here the code:
#include <QApplication>
#include <QPushButton>
#include <QLabel>
class MyWidget : public QWidget {
//Q_OBJECT
public:
MyWidget(QWidget* parent = 0);
QLabel* label;
QString string;
signals:
public slots:
void setTextLabel();
};
void MyWidget::setTextLabel() {
label->setText("Test");
}
MyWidget::MyWidget(QWidget* parent)
: QWidget(parent) {
}
int main(int argc, char** argv) {
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
it seems work but not "completely". My slot doens't work. I suppose i have to put Q_OBJECT. BUT, doing so, I got a list of errors, like this:
undefined reference to `vtable for MyWidget'
........................................
collect2: error: ld returned 1 exit status
make: *** [mywidget] Error 1
I can I manage that? Where the problem?
Signals and slots in Qt are managed through the moc: meta object compiler. Basically, the moc generates additional C++ code for each class containing the Q_OBJECT macro in order to implement effectively the signals and slots mechanisms. The additional code is then linked to the original class declaration.
The problem here is that your class is declared in main.cpp: this conflicts with how the moc is working with your code. You should declare your class in a separate header.
More about the moc
Edit: as hyde pointed, an alternative is to include in your cpp the file generated by the moc: Why is important to include “.moc” file at end of a Qt Source code file?
just append the line #include"main.moc" to your cpp source file should be enough.
More information:
Why is important to include ".moc" file at end of a Qt Source code file?

Qt framework undifined reference when linking

I am quite new to Qt framework. I try to code a class that contains a 2D array whose data is supposed to be displayed in the main window (using QTableWidget, for example). The class has the following declaration:
// gridgame.h
template <int gsize_r,int gsize_c>
class GridGame : public QObject
{
Q_OBJECT
private:
char field[gsize_r][gsize_c];
public:
GridGame();
void setupConnect(QMainWindow & win);
signals:
void onPrintStatus(char[gsize_r][gsize_c]);
};
The definition of the class is of course given in cpp file as follows:
//gridgame.cpp
#include "gridgame.h"
template <int gsize_r,int gsize_c>
GridGame<gsize_r,gsize_c>::GridGame()
{
int i,j;
for (i=0;i<gsize_r;++i)
for (j=0;j<gsize_c;++j)
field[i][j] = '-';
}
template <int gsize_r,int gsize_c>
void GridGame<gsize_r,gsize_c>::setupConnect(QMainWindow &win)
{
connect(this,SIGNAL(onPrintStatus()),&win,SLOT(onRefresh()));
}
template <int gsize_r,int gsize_c>
void GridGame<gsize_r,gsize_c>::onPrintStatus(char[gsize_r][gsize_c]){
emit field;
}
The class is used in main as follows:
#include <QtCore>
#include <QApplication>
#include <QObject>
#include "mainwindow.h"
#include "gridgame.cpp"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
const int sizer = 5, sizec = 5;
GridGame<sizer,sizec> gg;
w.show();
gg.printStatus();
return a.exec();
}
Question: The code seems to compile ok, but I get the linker errors even if I do not call any function of the class (just have a declaration of a variable)
-1: error: undefined reference to `GridGame<5, 5>::metaObject() const'
-1: error: undefined reference to `GridGame<5, 5>::qt_metacast(char const*)'
-1: error: undefined reference to `GridGame<5, 5>::qt_metacall(QMetaObject::Call, int, void**)'
-1: error: error: ld returned 1 exit status
What might be the problem? I am using Qt Creator 3.6.1 and Qt library 5.6.0.
Thanks in advance for any help.

QSqlDatabase LNK2019 error

I tried to compile the following code in Qt 5.0.0:
#include <QApplication>
#include <QtSql/QSql>
#include <Qtsql/QSqlDatabase>
#include <QStringList>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStringList db = QSqlDatabase::drivers();
return a.exec();
}
and I received this error:
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: static class QStringList __cdecl QSqlDatabase::drivers(void)" (__imp_?drivers#QSqlDatabase##SA?AVQStringList##XZ) referenced in function _main
debug\test.exe:-1: error: LNK1120: 1 unresolved externals
I have added QT += sql in my .pro. What's the problem?
You should add QtSql.lib ( you can find the name of Qtsql exactly in QT/lib in your computer) by go to Project/Properties/Configuration properties/Linker/Input, add QtSql.lib to Additional Dependencies
P.S. I used to face this error, and the error is fixed by this way. Good luck

Undefined reference to fp_init() Using fprint in Qt

I am trying to use the libfprint in my Qt application
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <libfprint/fprint.h>
int main(int argc, char *argv[])
{
int r = 1;
struct fp_dscv_dev *ddev;
struct fp_dscv_dev **discovered_devs;
struct fp_dev *dev;
struct fp_print_data *data;
r = fp_init();
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Compiling throws this error
/concept/main.cpp:31: undefined reference to `fp_init()'
I have been battling with this for a while now. Any idea what I can do to get past this point? Thanks in advance!
It's a problem with your linker - it cannot find necessary libraries. Did you pass necessary linker switches (something like -lfoo) instructing it to link with library you are trying to use?

Resources