QCefView
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
// 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
// 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();
}
Represents the CEF setting. For more details please refer to: https://bitbucket.org/chromiumembedded/...
Definition QCefConfig.h:28
void setBridgeObjectName(const QString &name)
Sets the bridge object name.
void addCommandLineSwitchWithValue(const QString &smitch, const QString &v)
Adds a switch with value to the command line args used to initialize the CEF.
void setLogLevel(const LogLevel lvl)
Sets the log level.
void addCommandLineSwitch(const QString &smitch)
Adds a switch to the commandline args used to initialize the CEF.
void setBackgroundColor(const QColor &color)
Sets the background color of the web page.
void setUserAgent(const QString &agent)
Sets the user agent.
void setRemoteDebuggingPort(short port)
Sets the remote debugging port.
@ LOGSEVERITY_DEFAULT
Default logging (currently INFO logging)
Definition QCefConfig.h:39
void setWindowlessRenderingEnabled(const bool enabled)
Sets the flag to enable/disable OSR mode.
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);
Represents the settings for individual browser.
Definition QCefSetting.h:27
void setBackgroundColor(const QColor &value)
Sets the background color.
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