MSVC2012 Qt supposed to include a directory? - c++

I built a custom Qt5 for msvc2012 using BlueGo.
I was reading the examples and they show this:
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.resize(320, 240);
window.show();
window.setWindowTitle(
QApplication::translate("toplevel", "Top-level widget"));
return app.exec();
}
Problem is, QtGui for me is actually a directory and not a file so it cannot be included. I'm using the include files under /qtbase/include/. Am I doing something wrong?

The QtGui header actually exists and simply includes all headers from the QtGui module. You can find it inside the QtGui directory. The compiler is able to find it because the QtGui directory is specified in the include paths. In other words, it's the same as:
#include <QtGui/QtGui>
It's a terrible practice to include the QtGui header though. You should only include what you actually use, otherwise compilation times will increase for no good reason. However, for quick tests and such, it's quite handy.

I know it is a bit late time now but you can do it like this:
add the gui module and widgets in the pro file :
QT += widgets core gui
And by replaging the include files,
replace
#include <QtGui/QWidget>
#include <QtGui/QApplication>
with
#include <QWidget>
#include <QApplication>
The compiler should recognize it.

Related

How to include webview in existing Qt project?

I am trying to include one of the following libraries:
#include <QtWebView>
#include <QWebView>
#include <QtWebEngineWidgets>
#include <WebEngineCore>
#include <QtWebEngine>
Each time I add one of its includes an error appears in my code. However, I use Qt 6.3.1 and I find files that correspond to the includes in my system installation folder under macOS. I use a cmake in my qt project and not a file.pro or qmake.
Ultimately, I want to display a web form in my UI.
You need to make sure that you install the QtWebEngine module when installing Qt.
Then, in your CMakeLists.txt, you would write something like this below.
Please note that you should use versionless targets as recommended by the Qt Project, i.e. do not use Qt6::WebEngineWidgets as that would have portability issues.
find_package(Qt6 COMPONENTS WebEngineWidgets REQUIRED)
target_link_libraries(YourTarget Qt::WebEngineWidgets)
add_executable(YourTarget main.cpp)
Then, you can just write something like this in your main.cpp:
#include <QApplication>
#include <QWebEngineView>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWebEngineView view;
view.setUrl(QUrl(QStringLiteral("https://www.qt.io")));
view.show();
return app.exec();
}
Please refer to the official examples for further details.
Maybe you need to add WebEngineWidgets module to your cmake file
find_package(Qt6 REQUIRED COMPONENTS WebEngineWidgets)
target_link_libraries(mytarget PRIVATE Qt6::WebEngineWidgets)
then #include <QWebEngineView>
https://doc.qt.io/qt-6/qwebengineview.html

QT 5 [ error: QtGui/QApplication: No such file or directory]

I am using Qt5. I wrote the following code:
#include <QtGui/QApplication>
#include <Qlabel>
int main(int argc, char *argv[]){
QApplication prog(argc, argv);
Qlabel *label = new Qlabel("gametime!");
label->show();
return prog.exec();
}
the following problem occurs:-
error: QtGui/QApplication: No such file or directory
QApplication is part of the Qt5 Widgets library (not the GUI library, which provides lower-level facilities).
Add QT += widgets to your .pro file, and change
#include <QtGui/QApplication>
to
#include <QtWidgets/QApplication>
(You should be able to reduce to just <QApplication>, as QMake will normally add the necessary include paths based on the libraries specified in the QT variable.)

Segmentation Fault when using some Qt5 classes in a QtQuick 2 application

When trying to use some Qt-5 classes I'm experiencing crashes. I first discovered this trying to use QFileSystemModel. Trying to call setRootPath immediately leads to a crash. The callstack isn't of much help (all of it is assembly code) except that QFileIconProvider::icon() is the last function called before the seg fault occurs.
So next I tried using QFileIconProvider manually and -to no surprise- it also crashed the program.
I'm using QtCreator 4 and the type of project is "Qt Quick Application". When I instead create a project of type "Qt Widgets Application", I can use both QFileIconProvider and QFileSystemModel without problems.
Here's where I'm out of ideas. I don't know enough about the Qt environment to know what difference between the two types of projects could lead to the seg fault.
Both projects use the same Kit (same gcc, same Qt 5.6.1) and the default settings as set by QtCreator.
This is my project.pro file:
TEMPLATE = app
QT += qml quick widgets //default .pro file except for widgets
CONFIG += c++11
SOURCES += main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
This is main.cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDir>
#include <QFileSystemModel>
#include <QQmlContext>
#include <QFileIconProvider>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
//If trying to use QFileSystemMode...
QFileSystemModel model;
model.setRootPath("/somefolder/"); //..the crash happens here
//Attempting to use QFileIconProvider also crashes
//QFileIconProvider fip;
//fip.icon( QFileInfo("/somefolder/somefile") ); //<- here
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
I'd appreciate any help or pointers as to how to debug that mess.
As confusing as it may sound, QFileSystemModel is part of QtWidgets and therefore requires you to create and instance of QApplication instead of QGuiApplication.

Cmake link issue: undefined reference to QPushButton

I just started using Qt. I have a problem with compiling the first example.
main.cpp:
#include <QCoreApplication>
#include <QPushButton>
int main(int argc, char** argv)
{
QCoreApplication app(argc, argv);
QPushButton button ("Hello world !");
return app.exec();
}
CMake.txt:
cmake_minimum_required(VERSION 2.6)
project(new)
find_package(Qt4 REQUIRED)
enable_testing()
include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
set(source_SRCS main.cpp)
qt4_automoc(${source_SRCS})
add_executable(new ${source_SRCS})
target_link_libraries(new${QT_QTCORE_LIBRARY})
add_subdirectory(tests)
install(TARGETS new RUNTIME DESTINATION .)
The error I get upon building is:
undefined reference to `QPushButton::QPushButton(QString const&,QWidget*)'
It is a linking problem, but how can I solve it?
Here is what I think you are missing:
find_package(Qt4 REQUIRED QtGui)
looking at your cmake you probably want to change the target_link_libraries for the following:
target_link_libraries(new ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY})
You have three problems:
You're not linking with the Gui module (Widgets module in Qt 5). This is covered in the other answer.
You you must use QApplication in widgets-based applications. Since QPushButton comes from the Gui module (Widgets in Qt5), you can't merely use QCoreApplication nor QGuiApplication: your program will crash as soon as you attempt to instantiate a QWidget.
You're not showing the button, so when your program starts you'll see nothing once you fix the above.
Your main.cpp should look like:
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
#include <QtGui>
#else
#include <QtWidgets>
#endif
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QPushButton button ("Hello world !");
button.show();
return app.exec();
}
You're also going to need to link against the QtGui and QtWidgets library. Within qmake it handles which of the many libraries make up Qt, you'll have to do this by hand in cmake.
If you look at the documentation for QPushButton (http://doc.qt.io/qt-5/qpushbutton.html), the "qmake" line shows what library you need.
Consider using qmake instead of cmake with Qt.
Anyway, QCoreApplication (see docs) is the console version of main application class and will not work in GUI application. QPushButton is a widget class and can exist alone and will make a window (although you must show() it explicitely for that) but only with QApplication.
When using qmake in your *.pro file you need to include widgets like so:
CONFIG += widgets
and make sure you don't have
CONFIG -= gui
If you insist on using cmake then see here.
this answer solves my same problem :)
ok,
i had can solve it by myself.
For all they have they problem too:
The error is sourced from the use of "Q_OBJECT".
To solve the error, right-cklick on the Project and choose "Run qmake" and after >this: "Rebuild".
Then the error should be disappeared ;-)
-casisto
https://forum.qt.io/topic/52439/get-undefined-reference-error-but-don-t-know-why/2

(C++)Code:: blocks does not recognize QT4 classes

So far I have worked in the console and a few days ago decided to try the QT GUI.
I downloaded the QT SDK , install it, adjust the location of QT and
set up the PATH Environment Variable -> per the instructions on the site.
I opened a new Qt4 project in Code:: Blocks-in and it seemed that everything was OK.
There is by default an example:
#include <QApplication>
#include <QFont>
#include <QPushButton>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QPushButton quit("Quit");
quit.resize(75, 30);
quit.setFont(QFont("Times", 18, QFont::Bold));
QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));
quit.show();
return app.exec();
}
Started it an it was all OK.
After that I went to a tutorial on the official site and there is a final example.
Some kind of simple game.I have done copy-paste of all .h and .cpp files and then put
them in current project to see how it works but then problems arise.
Code::Blocks does not recognize some classes.
For example :: #include QTimer : No such file or directory
#include QRect : No such file or directory
I uninstall QT and re-installed and configured everything again but the problem does not go out.
These classes are not working nor in the default example ::
#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QTimer> does not have real purpose , just for illustration
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QPushButton quit("Quit");
quit.resize(75, 30);
quit.setFont(QFont("Times", 18, QFont::Bold));
QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));
quit.show();
return app.exec();
}
ba\107\main.cpp|4|QTimer: No such file or directory|
||=== Build finished: 1 errors, 0 warnings ===|
I dont now how much classes don work properly , this is just some of them.
Not to reveal hot water for days on google looking for a solution, maybe for some of you
, this is a bizarrely easy problem.
Thanks
You need to either spend time monkeying with the default include search path, or else just provide a more explicit path the header you want to include. I was able to reproduce your problem with Code::Blocks 10.05 (with bundled gcc) on Windows XP/32 and a previously installed Qt 4.6. Here is the slightly changed version of your code that I was able to build without any problem:
#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QtCore/QTimer>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QPushButton quit("Quit");
quit.resize(75, 30);
quit.setFont(QFont("Times", 18, QFont::Bold));
QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));
quit.show();
return app.exec();
}
Take a look in your Qt install directory. You'll be able to see the include directory, and where all the headers are within it if you run into this problem with any other headers. It looks like the Code::Blocks projects sets up the QtGui directory as an include search path by default, which is why you didn't need to explicitly mention it for including QPushButton and whatnot.
Code::Blocks is only an IDE not a Compiler/Linker toolchain, so it is not Code::blocks that cant find the files, you have simply not configured your project to use them.
"No such file or directory" is a pre-processor error message; you still have to tell the compiler where to find your third-party header files. Moreover when it comes to linking, you will need to tell the linker where to find the libraries.
Whenever you have an #include <blah> (with angle bracktes <>) the compiler looks in the default include path. You need to put the Qt include directory into the include path for your project. I'm not sure how this is done in Code::Blocks. It's probably somewhere in the project settings.