| QCefView 1.0.7 A QWidget-based WebView Component Integrated with CEF

Loading...
Searching...
No Matches
First Project

This guide walks you through creating a basic Qt GUI application that integrates QCefView. The complete source code for this example is available in the QCefViewTest directory.

# Initializing the QCefContext Instance

The first step in using QCefView is to initialize a `QCefContext` instance. Think of this as the QCefView equivalent of `QApplication`: you should have only one instance of `QCefContext` throughout your application's lifecycle.

#include <QApplication>
#include <QCefContext.h>
#include "MainWindow.h"
int
main(int argc, char* argv[])
{
// 1. Create a QApplication instance
QApplication a(argc, argv);
// 2. Build a QCefConfig object
QCefConfig config;
// Set user agent
config.setUserAgent("QCefViewTest");
// Set log level
config.setLogLevel(QCefConfig::LOGSEVERITY_DEFAULT);
// Set JSBridge object name (default value is QCefViewClient)
config.setBridgeObjectName("CallBridge");
// Port for remote debugging (default is 0, disabling remote debugging)
config.setRemoteDebuggingPort(9000);
// Set background color for all browsers
// (QCefSetting.setBackgroundColor will overwrite this value for a specific browser instance)
config.setBackgroundColor(Qt::lightGray);
// WindowlessRenderingEnabled is true by default, disable OSR mode by setting it to false
config.setWindowlessRenderingEnabled(true);
// Add command line arguments (any CEF-supported switches or parameters)
config.addCommandLineSwitch("use-mock-keychain");
// config.addCommandLineSwitch("disable-spell-checking");
// config.addCommandLineSwitch("disable-site-isolation-trials");
// config.addCommandLineSwitch("enable-aggressive-domstorage-flushing");
config.addCommandLineSwitchWithValue("renderer-process-limit", "1");
// config.addCommandLineSwitchWithValue("disable-features", "BlinkGenPropertyTrees,TranslateUI,site-per-process");
// 3. Create the QCefContext instance, passing the QApplication instance and config
// The lifecycle of cefContext must match the QApplication instance's lifecycle
QCefContext cefContext(&a, argc, argv, &config);
// 4. Create and show your application window
MainWindow w;
w.show();
// 5. Run the application
return a.exec();
}
QCefContext.h
QCefConfig
Represents the CEF setting. For more details please refer to: https://bitbucket.org/chromiumembedded/...
Definition QCefConfig.h:28
QCefConfig::setBridgeObjectName
void setBridgeObjectName(const QString &name)
Sets the bridge object name.
QCefConfig::addCommandLineSwitchWithValue
void addCommandLineSwitchWithValue(const QString &smitch, const QString &v)
Adds a switch with value to the command line args used to initialize the CEF.
QCefConfig::setLogLevel
void setLogLevel(const LogLevel lvl)
Sets the log level.
QCefConfig::addCommandLineSwitch
void addCommandLineSwitch(const QString &smitch)
Adds a switch to the commandline args used to initialize the CEF.
QCefConfig::setBackgroundColor
void setBackgroundColor(const QColor &color)
Sets the background color of the web page.
QCefConfig::setUserAgent
void setUserAgent(const QString &agent)
Sets the user agent.
QCefConfig::setRemoteDebuggingPort
void setRemoteDebuggingPort(short port)
Sets the remote debugging port.
QCefConfig::LOGSEVERITY_DEFAULT
@ LOGSEVERITY_DEFAULT
Default logging (currently INFO logging)
Definition QCefConfig.h:39
QCefConfig::setWindowlessRenderingEnabled
void setWindowlessRenderingEnabled(const bool enabled)
Sets the flag to enable/disable OSR mode.
QCefContext
Represents the CEF context.
Definition QCefContext.h:27

# Create QCefView Instance

Once you have initialized `QCefContext`, you can create a `QCefView` instance.

// Build settings for per QCefView
QCefSetting setting;
// Here we just set the default background to blue
setting.setBackgroundColor(QColor::fromRgb(0, 0, 255));
// Create the QCefView widget and add it to the layout container
cefViewWidget = new QCefView(uri, &setting, this);
ui.cefContainer->layout()->addWidget(cefViewWidget);
layout->addWidget(ui.cefContainer);
QCefSetting
Represents the settings for individual browser.
Definition QCefSetting.h:27
QCefSetting::setBackgroundColor
void setBackgroundColor(const QColor &value)
Sets the background color.
QCefView
Represents the CEF browser view.
Definition QCefView.h:49

# Create a Simple Web Page

Create a simple web page with the following content:

<html>
<head>
</head>
<body id="main" class="noselect">
<h1 align="center" style="font-size: 12pt">Web Area</h1>
</body>
</html>

# Run the Application

Now let's run the application.

First Project