How to include webview in existing Qt project? - c++

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

Related

how to add qtvirtualkeyboard to a qt widget project

I have a Qt Widget project that I created using QtCreator and Qt version 5.15.2 to which I'm trying to add the QtVirtualKeyboard as matchbox-keyboard I've already tried using stays under the application when it's in fullscreen mode.
However I'm having trouble getting it to work as it's not appearing at all at the moment. This is how I've tried adding it
Main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
MainWindow w;
//w.showFullScreen();
w.show();
return a.exec();
}
I've tried adding QT += qtvirtualkeyboard or QT += virtualkeyboard in the .pro file but it just gives me an error saying "unknown module"
How can I add the virtual keyboard to the project?
You will need to make sure that the QtVirtualKeyboard library is selected during the installation process.
Also, I would recommend you that you start using cmake for Qt applications as Qt has officially dropped qmake since the release of Qt 6.
It is not to say that you cannot use it, but you will get better support and results by using an actively developed build system rather than an abandoned.

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

MSVC2012 Qt supposed to include a directory?

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.