How make a window with QT 6,0? - qt

Hi to everyone Im beguinner with QT 6.0 Im started to practice with but I get this error message
C:\Users\blabla\Documents\Programming\VentanaPrincipal\main.cpp:6: error: undefined reference to `__imp__ZN7QWidgetC1EPS_6QFlagsIN2Qt10WindowTypeEE'
CMakeFiles/VentanaPrincipal.dir/main.cpp.obj: In function `main':
C:/Users/blabla/Documents/Programming/VentanaPrincipal/main.cpp:6: undefined reference to `__imp__ZN7QWidgetC1EPS_6QFlagsIN2Qt10WindowTypeEE'
literally this is the code what I made and As well I'm saw at https://doc.qt.io/qt-5/qtwidgets-tutorials-widgets-toplevel-example.html
#include <QCoreApplication>
#include <QtWidgets/QtWidgets>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QWidget window;
window.resize(320,240);
window.show();
return a.exec();
}

Based on comments to your question, your CMakeLists.txt file has this for the library requirements:
target_link_libraries(VentanaPrincipal Qt${QT_VERSION_MAJOR}::Core)
While core brings in a large chunk of Qt, it does not bring in everything. In particular, if you want to use widgets, you'll need Qt*::Widgets as well.
A good way to ensure you have everything you need is to look at the documentation for the classes you wish to use. For example, the QWidget documentation page (for Qt5, which is what I use) has this in the table at the top:
+-----------+--------------------+
| Header: | #include <QWidget> |
+-----------+--------------------+
| qmake: | QT += widgets |
+-----------+--------------------+
That means you need to include the QWidget header file and link with the widgets library. Were you using qmake, that would simply involve putting that exact line QT += widgets into your project file.
The equivalent in cmake would result in the following changes to your CMakeLists.txt file:
target_link_libraries(VentanaPrincipal
Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Widgets
)
The equivalent Qt6 documentation page is even better since it now shows the cmake stuff as well, meaning you don't need to think about translating the qmake variant:
+-----------+------------------------------------------------------+
| Header: | #include <QWidget> |
+-----------+------------------------------------------------------+
| CMake: | find_package(Qt6 COMPONENTS Widgets REQUIRED) |
| | target_link_libraries(mytarget PRIVATE Qt6::Widgets) |
+-----------+------------------------------------------------------+
| qmake: | QT += widgets |
+-----------+------------------------------------------------------+

How about using QApplicacion instead of QCoreApplication?
QApplication a(argc, argv);
You also don't need cmake. You should create simple project, and use qmake to generate makefile.
Chcek this thread: Simple Qt program builds but doesn't show output.

Related

Assignment needs exactly one word on the left hand side

I was trying to centralize a variable from .pro file into my project by trying to define it and print in another .qml file
.pro file
VERSION=2.4
DEFINE APPVERSION=VERSION;
.qml file
text: qsTr(APPVERSION)
this seems to give me the above subjective error. Must there be any other way.
DEFINE works for C++, it is not for qml so a possible solution is to convert the DEFINE to string and then export it to qml using setContextProperty:
*.pro
VERSION = 2.4
DEFINES += APPVERSION=$$VERSION
main.cpp
// ...
#define QUOTE_(x) #x
#define QUOTE(x) QUOTE_(x)
// ...
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("APPVERSION", QUOTE(APPVERSION));
// ...
*.qml
text: APPVERSION

Including Eigen with QtCreator - Red errors showing in source file even though the code works?

I have just started using QtCreator (and C++ after 15 years away from it). I want to do some linear algebra stuff so I have included the library Eigen in my project file as follows (I have placed the Eigen library directly in my project for the moment):
INCLUDEPATH = "/home/Software/QtProjects/MyProject/eigen/"
My source file is:
#include <QCoreApplication>
#include <iostream>
#include <Eigen/Dense> % 'Eigen/Dense' file not found
using Eigen::MatrixXd; % use of undeclared identifier 'Eigen'
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MatrixXd m(2,2); % unknown type name 'MatrixXd'
m(0,0) = 3;
m(1,0) = 2.5;
m(0,1) = 8;
m(1,1) = m(1,0) + m(0,1);
std::cout << m << std::endl;
return a.exec();
}
This code works and outputs the values of the matrix m in the console. However, in the QtCreator IDE, I have several error messages which I have included above as comments on the lines at which they appear.
So is there some way to fix this and make QtCreator stop showing these lines as errors when the code is actually working fine?
Try without quotation marks; and with += instead of =; INCLUDEPATH += /home/Software/QtProjects/MyProject/eigen/.
Go to Build > Run qmake to validate changes in the pro file.
INCLUDEPATH in qmake project file doesn't work
Qt creator Adding external library
How to add include path in Qt Creator?
On Linux, run
sudo apt-get install libeigen3-dev
Then add this to your .pro file
INCLUDEPATH += /usr/include/eigen3
And then run qmake

Qt/qmake supress warnings/issues from third party header file

I am using the nlohmann json (https://github.com/nlohmann/json) library (1 single json.h file) with Qt. I realize Qt has it's own json functionality, so assume that any third party header may be used. Qt generates a whole lot of issues/warnings and I would like to try suppress issues/warnings for just the json.h file.
[EDIT] Interestingly the json third party library has been updated from v3.3.0 to v3.4.0 and all warnings gone (probably best outcome). Nonetheless, being able to suppress third party warnings would still be great.
I tried the suggestions here, by putting the .h in a sub directory as well as adding the sub directory to the INCLUDEPATH. $$PWD will apparently give the current directory of the .pro file. But there is no change after doing this.
INCLUDEPATH += $$PWD/thirdparty
QMAKE_CXXFLAGS += -isystem $$PWD/thirdparty/
The warnings are easy to reproduce by the simple hello world example below:
#include <QCoreApplication>
#include <QDebug>
#include "json.h"
using namespace nlohmann;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "hello world";
return a.exec();
}
[EDIT]
Still hoping for some help. From the comments I have tried wrapping the inclusion, which didn't work, with what is seen below:
#pragma warning(push, 0)
#include "json.h"
#pragma warning(pop)
I am using qmake but thought that maybe gcc solutions might apply, seems they don't.
Any help would be greatly appreciated.

Gtk-CRITICAL **: IA__gtk_widget_style_get: assertion `GTK_IS_WIDGET (widget)' failed

After a static build of my qt application
./configure -static -debug-and-release -confirm-license -nomake demos -nomake examples -nomake tools
it works fine but I get several output messages yelling:
(MyApplication:32030): Gtk-CRITICAL **: IA__gtk_widget_style_get: assertion `GTK_IS_WIDGET (widget)' failed
Is there really a critical problem, should I rebuild qt with different option?
Any help will be appreciated.
This is kind of late, but hopefully it will save someone else some time.
For me, the error was being caused by the combination of two things: QCleanlooksStyle and QTableWidget. On ubuntu, the default style is either QCleanlooksStyle or QGtkStyle (which inherits from QCleanlooksStyle). When a QTableWidget is painted with either one of these styles, I saw that error. My solution was something like this:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyle(new QPlastiqueStyle());
MainWindow w;
w.show();
return a.exec();
}
In PyQt5 you can use the following code to avoid the problem.
app = QApplication()
app.setStyle('Fusion')
I think it is a problem of style 'GTK+'.
I also had this problem with static build of Qt. If you put this code before everything else in main method problem goes away:
#ifdef Q_WS_X11
qputenv("LIBOVERLAY_SCROLLBAR", 0);
#endif

Undefined reference to when including file from separate directory

I have very basic QT application (just create to explain my problem).
So here I go :) I have two folders, f1 and f2, and they are on same level (have same folder for parent). In f1 I have source code from my project, and in f2 another project.
For sake of this example, let's say that in f1 I have only common.h and common.cpp, and in f2 I have:
Test.pro
SOURCES = main.cpp
INCLUDEPATH += "..//f1//"
main.cpp
#include <common.h>
#include <QDebug>
int main(int argc, char *argv[])
{
qDebug()<<CalculateMD5("test");
}
When I try to build this project (Test.pro) I get following error:
f2/main.cpp:7: undefined reference to `CalculateMD5(QString)'
What am I doing wrong ? How should I include code from another project ?
I need CalculateMD5 function to be global.
Here you can download whole example (1kb):
http://www.xx77abs.com/test.rar
Thanks !!
For the simplest change, I think you need to change your .pro file to this:
Test.pro
SOURCES = main.cpp ../f1/common.cpp
INCLUDEPATH += "..//f1//"
If you have other code that will also be linking in common.cpp, then a better change would be to make a .pro file in f1 that generates a library, to be linked in to other applications.
But just manually adding common.cpp to your list of sources should do the trick.

Resources