QtWebEngine, How to Save Cookies in C++/Qt6.2.4? - c++

Since I change Qt5 to Qt6 for my app,
What worked for Qt5 (to save the cookies, I followed this thread QT 5.6 QWebEngine doesn't save cookies),
Doesn't work now :
QWebEngineProfile::defaultProfile()->setPersistentCookiesPolicy(QWebEngineProfile::ForcePersistentCookies);
QWebEngineProfile* defaultProfile = QWebEngineProfile::defaultProfile();
defaultProfile->setHttpCacheType(QWebEngineProfile::DiskHttpCache);
defaultProfile->setPersistentCookiesPolicy(QWebEngineProfile::ForcePersistentCookies);
QHttpPart* header = new QHttpPart;
header->setRawHeader("X-Frame-Options", "ALLOWALL");
defaultProfile->setCachePath(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
defaultProfile->setPersistentStoragePath(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation));
I need to save Cookies.

The Solution is :
QWebEngineProfile *profile = new QWebEngineProfile(QString::fromLatin1("MyApplication.%1").arg(qWebEngineChromiumVersion())); // unique profile store per qtwbengine version
QWebEnginePage *page = new QWebEnginePage(profile); // page using profile
QWebEngineView *view = new QWebEngineView();
view->setPage(page);
view->setUrl(AccueilUrl);
view->setZoomFactor(1.2);
setCentralWidget(view);

Related

QObject::findChild returns 0 for QLabels added to statusbar

I created a application running in a QMainWindow using qtcreator, so the typical way.
I added two 'manually' (meaning: not with the Form editor) created qlabels to the statusbar:
in the header:
QLabel *label_timestamp;
QLabel *contentLabel_timestamp;
in the constructor:
MainWin::MainWin(const CmdLineOptions &opts, QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWin),
m_connectionStatusLabel(new QLabel),
m_client(new QMqttClient),
m_mqttmanager(new MQTTManager(m_client)),
m_mqttServerName("localhost")
{
ui->setupUi(this);
label_timestamp = new QLabel(this);
contentLabel_timestamp = new QLabel(this);
label_timestamp->setText("system time");
contentLabel_timestamp->setText("dd.mm.yyyy, hh:mm:ss:zzz"); /* just testing output */
statusBar()->addPermanentWidget(label_timestamp);
statusBar()->addPermanentWidget(contentLabel_timestamp);
}
If I do a
Label *label = findChild<QLabel *>(QString("contentLabel_")+objName);
elsewhere in this class implementation with objName being 'timestamp', of course, findChild() returns 0. It's working fine with other QLabels created using QtCreator in the form editor, findChild() finds them all. Isn't the statusbar widget and its content also a child of ui? Does somebody eventually know a way out of there?
I want to use findChild to generically fill my labels following a naming scheme with content I receive over MQTT, this is the background. Would be great if the statusbar content would need a special handling but could also be handled in this dynamic approach.
Thanks a lot
findChild uses the objectName, in the case of Qt Creator this establishes it in the MOC, but in your case you must establish it:
label_timestamp = new QLabel(this);
contentLabel_timestamp->setObjectName("label_timestamp");
contentLabel_timestamp = new QLabel(this);
contentLabel_timestamp->setObjectName("contentLabel_timestamp");
And then you can recover it with:
QLabel *label_1 = findChild<QLabel *>("label_timestamp");
if(label_1){
// some code
}
QLabel *label_2 = findChild<QLabel *>("contentLabel_timestamp");
if(label_2){
// some code
}

Add QMdiSubWindow to current QStackedLayout

So I have this code:
QStackedLayout *layout = new QStackedLayout;
QMdiArea *mdi1 = new QMdiArea;
mdi1->addSubWindow(new QMdiSubWindow);
layout->addWidget(mdi1);
QMdiArea *a = (QMdiArea *) layout->currentWidget();
a->addSubWindow(new QMdiSubWindow);
Which for some reason doesn't work. What I want to do is get the widget that is being displayed in layout - as a QMdiArea, then add a sub window to it.
P.S. this is a simplified version on the full app. adding a sub window directly to mdi1 will work but it is NOT what I'm looking for (as there are many QMdiArea's in the QStackedLayout).
So the answer was that I needed to use QStackedWidget instead of QStackedLayout.

Get webview document title when HTML is loaded

I have the following Qt webview:
QWebView *view = new QWebView();
view->load(QUrl("http://example.com"));
I want to get the title of document when load is finished, and use it to set the main window title.
From what I suppose view->loadFinished() returns true if page was loaded or not.
For setting the window title I use webView->setWindowTitle(newTitle). So, I need that newTitle variable that I want to be the document title.
How can I do this?
QWebView::loadFinished is a signal. You can subscribe to it to know when the page is loaded:
connect(view, SIGNAL(loadFinished(bool)), this, SLOT(onLoaded()));
To access HTML title you can use QWebView::title property.
void onLoaded()
{
window->setWindowTitle(view->title());
}
Rather then using loadFinished it may be more appropriate to use signal titleChanged(const QString& title) to apply a new title to the window:
connect(view, SIGNAL(titleChanged(QString)), this, SLOT(setWindowTitle(QString)));
EDIT:
Example:
QWebView* webView = new QWebView();
connect(webView, SIGNAL(titleChanged(QString)), webView, SLOT(setWindowTitle(QString)));
webView->load(QUrl("http://yahoo.com"));
webView->show();

Qt - Any guideline on how to implement navigation between UI Forms?

I'm learning Qt right now, trying to make a simple application. What I'm trying to implement is a simple 'Welcome' screen, with two buttons ('Register' and 'Login'). The problem is on the page redirection to the other two pages.
Also, I have the screens already setup using the QtCreator (in .ui Forms format).
The only solution I could come up with, until now, was based on an example from Qt itself, which uses a QStackedWidget, adding QWidgets as pages. The problem is that those pages in the example are mounted programmatically (and I want to use the Forms that I have).
If I try this:
MainWindow::MainWindow() :
ui_home(new Ui::HomeView),
ui_register(new Ui::RegisterView) {
ui_home->setupUi(this);
ui_register->setupUi(this);
pagesWidget = new QStackedWidget;
pagesWidget->addWidget(ui_home->centralWidget);
pagesWidget->addWidget(ui_register->centralWidget);
...
}
It 'kind of works', but the result is horrible. The 'centralWidget' from my Forms is added to the 'pagesWidget', but the 'setupUi' before that really renders the 'Home' and 'Register' pages all at once, messing everything up.
So, real question is:
Is there any guideline on how to implement navigation between UI Forms?
Secondly:
How can I retrieve the QWidget from my UI Form and add to a QStackedWidget, without rendering it?
I am using Ubuntu 12.04, with QtCreator 3.2.1.
Thanks in advance.
You do it wrong. You should create a "MainWidget" with 2 items: a) QStackedWidget, b) navigation panel (your buttons). Then you should set "MainWidget" as a central widget.
After it you can connect signals from navigation panel (clicked signals of "Registed" or "Login" buttons) to corresponding slots, that will select necessary widget on QStackedWidget
So, in your case, you need next 3 ui forms:
MainWindow (QStackWidget + 2 buttons)
LoginWidget
RegisterWidget
Pseudocode:
// RegisterWidget.cpp + you should have RegisterWidget.ui
RegisterWidget::RegisterWidget()
: public QWidget(NULL)
, ui( new Ui::RegisterWidget() )
{}
// LoginWidget.cpp + you should have LoginWidget.ui
LoginWidget::LoginWidget()
: public QWidget(NULL)
, ui( new Ui::LoginWidget() )
{}
// MainWindow.cpp + you should have MainWindow.ui
MainWindow::MainWindow()
: public QMainWindow()
, ui( new Ui::MainWindow() )
{
ui->setupUi(this);
// !!!!!!!!!!!!!!!!!!!
// Creating widgets here.
// Possible - setup communication between widgets with signals / slots
// !!!!!!!!!!!!!!!!!!!
m_loginForm = new LoginWidget();
m_regForm = new RegisterWidget();
ui->stackedWidget->addWidget( m_loginForm );
ui->stackedWidget->addWidget( m_regForm );
connect( ui->loginBtn, &QPushButton::clicked, this, &MainWindow::onLogin );
connect( ui->regBtn, &QPushButton::clicked, this, &MainWindow::onReg );
}
// private slots:
void MainWindow::onLogin()
{
ui->stackedWidget->setCurrentWidget( m_loginForm );
}
void MainWindow::onReg()
{
ui->stackedWidget->setCurrentWidget( m_regForm );
}
And don't forget about layouts. Because you may see nothing if your widgets will have 1x1px size.
How can I retrieve the QWidget from my UI Form and add to a QStackedWidget, without rendering it?
You may use QWidget::hide() method.

How to display a web page using QT/C++

I am trying to display a web page using the below code
QWebView *view = new QWebView();
view->load(QUrl("qrc://images//sample page.html/"));
view->show();
sample page.html is added to project resources/Images. The web page frame is loading, but I can't see any html data.
I tested with the below web url and it loaded the page
view->load(QUrl("http://www.google.com/"));
You will have to go through a few steps as follows:
1) Get the QWebPage object:
QWebPage *page = view->page();
2) Get the QWebFrame object:
QWebFrame *frame = page->currentFrame();
3) Call the toHtml member function on the current frame:
QString html = frame->toHtml();
Of course, you will need to add appropriate error checks in between.