Qt 5.6 how to use QWebEngineView in my custom widget plugin? - c++

I'm trying to create a Qt custom widget plugin to wrap a QWebEngineView. But I found QWebEngineView seems does not work with Qt Designer.
The demo code is attached as below. After build and place this plugin in Qt plugins folder, then Qt Designer will not launch correctly (No GUI window).
If I remove the line m_web = new QWebEngineView();, then the plugin can be loaded by Qt Designer correctly.
How to solve this issue?
#define WEBVIEWWRAPPER_H
#include <QWidget>
#include <QWebEngineView>
class WebViewWrapper : public QWidget
{
Q_OBJECT
public:
WebViewWrapper(QWidget *parent = 0);
private:
QWebEngineView* m_web;
};
#endif
// webviewwrapper.cpp
#include "webviewwrapper.h"
WebViewWrapper::WebViewWrapper(QWidget *parent) :
QWidget(parent)
{
m_web = new QWebEngineView(); // if I remove this line, the plugin will be loaded correctly
}

Related

Error in template generated by Qt in Visual Studio

So I've been trying to play around with Qt in Visual Studio. However, when I create a GUI application, the automatically generated template already contains a compiler error, namely:
Error (active) E1696 cannot open source file "ui_QtGuiApplication1.h" QtGuiApplication1 E:\visual_studio_projects\gui_test\QtGuiApplication1\QtGuiApplication1.h 4
I couldn't locate the ui_QtGuiApplication1.h header anywhere in the solution.
Removing the include statement results in the Ui class not being found. As far as I can tell, the Ui class is supposed to be located in ui_QtGuiApplication1.h.
QtGuiApplication1.h:
#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication1.h"
class QtGuiApplication1 : public QMainWindow
{
Q_OBJECT
public:
QtGuiApplication1(QWidget *parent = Q_NULLPTR);
private:
Ui::QtGuiApplication1Class ui;
};
QtGuiApplication1.cpp:
QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
}
Any help is appreciated.
EDIT: The ui_QtGuiApplication1.h was generated automatically by uic after the corresponding .ui file is saved in Qt Designer.
The file #include "ui_QtGuiApplication1.h is generated from your "QtGuiApplication1.ui" Designer file. Seems, that you didn't call the User Interface Compiler Tool (uic).

How to add Switch Qml to qt widgets?

I have a qml file ToggleSwitch.qml which I want to import to my widgets. Basically I want to integrate qml with widget
ToggleSwitch.qml
Switch
{
checked:true
}
In Mainwindow.cpp I want do something like this
Mainwindow.cpp
QQuickWidget *quickWidget = new QQuickWidget;
quickWidget->setSource(QUrl("qrc:/Resources/ToggleSwitch.qml"));
QVBoxLayout *vBox = new QVBoxLayout;
vBox->addWidget(quickWidget);
I tried this above code but it does not work. It throws error unresolved external symbol
To use QQuickWidget you must add the module quickwidgets, add the following to the .pro:
QT += quickwidgets
Also do not forget to include the header:
#include <QQuickWidget>
If you are in windows you must use windowdeployqt to obtain all the necessary dlls to execute your application, more information the following link:
http://doc.qt.io/qt-5/windows-deployment.html

Referencing WebKit or WebEngine in .ui file depending on Qt version

I am attempting to upgrade a Qt application from 5.3.1 to 5.7.0. The main change is the migration from WebKit to WebEngine.
http://doc.qt.io/qt-5/qtwebenginewidgets-qtwebkitportingguide.html
We are not yet ready to stop using 5.3.1, so I created a preprocessor define based on the Qt version. This works well for the project file, class source, and class header. However, I don't see a way to do this in the .ui file.
The Qt version is used to define a preprocessor define in the .pro file:
greaterThan(QT_MAJOR_VERSION, 4) {
QT += widgets
greaterThan(QT_MINOR_VERSION, 5) {
QT += webengine webenginewidgets
DEFINES += _WEBENGINE_
} else {
QT += webkit webkitwidgets
}
}
This works well in the .cpp file:
#ifdef _WEBENGINE_
#include <QWebEngineView>
#include <QWebEnginePage>
#include <QWebEngineFrame>
#include <QWebEngineElement>
#else
#include <QWebView>
#include <QWebPage>
#include <QWebFrame>
#include <QWebElement>
#endif // _WEBENGINE_
These are the offending references in the .ui file:
<widget class="QWebView" name="webView" native="true">
...
</widget>
...
<customwidget>
<class>QWebView</class>
<extends>QWidget</extends>
<header>QtWebKit/QWebView</header>
</customwidget>
Which results in the ui_*.h file:
#include <QtWebKitWidgets/QWebView>
...
public:
QWebView *webView;
How can I use the correct include paths and class names in the .ui file depending on the Qt version?
You cannot do that in the .ui file. The usual way to solve this would be by creating your own wrapper widget, and using that in the .ui file.
class MyWebView : public QWidget
{ .... }
MyWebView::MyWebView(QObject *parent) : QWidget(parent)
{
#ifdef _WEBENGINE_
// add webengineview to layout so it takes all space
#else
// add webview to layout so it takes all space
#endif
...
}

Qt creator cannot find, QTcpServer and QTcpSocket

Eventhough, I put 'network' in .pro file,
My Qt creator cannot find QTcpSocket and QTcpServer.
What should I do?
I remove the Qtcreator and redownloaded already but it also did not work.
My Linux version is Ubuntu 14.04 LTS
and I downloaded QT Creator by linux commands.
sudo apt-get install qtcreator
I even do the linus updates because I worried that I missed something.
sudo get-apt update
sudo get-apt upgrade
Why Qtcreator cannot perceive QTcpServer and QTcpSocket headers?
--------------*.pro -----------------------------------
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = server11
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
-------------------mainwindow.h--------------------------------------
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTcpSocket>
#include <QTcpServer>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Did you install the complete framework? Or just the IDE?
Try downloading the Qt offline installer and downloading the framework through it. Download and install it from here.
Qt Open Source
Instructions for Ubuntu

Qt Widgets FullScreen Margin

I want to create a program that loads google in literally full screen, so I achieved opening my qt program in full screen using w.showFullScreen(); and it works perfectly, however when I add the QWebView and set it to centralWidget like this:
but when I run the program, I get some margins on the sides of the window, in other words the QWebView isn't literally in fullScreen harmoniously with the window which is, it looks like this:
I don't think my code is mistaken but here it is
untitled.pro:
#-------------------------------------------------
#
# Project created by QtCreator 2015-02-07T21:25:42
#
#-------------------------------------------------
CONFIG += release
QT += core gui
QT += webkitwidgets
main.cpp
w.showFullScreen();
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWebKitWidgets>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->webView->load(QUrl("http://www.google.com"));
}
MainWindow::~MainWindow()
{
delete ui;
}
Thank you.
Your central widget have a layout. It's possible that you have layoutLeftMargin, layoutRightMargin, layoutTopMargin, layoutBottomMargin with others values than 0 got in you QtDesigner click on your central widget and at the down of you properties set the margins to 0.