Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to add widgets to QWebEngineView?



  • Hi,
    I am customizing a web browser,
    but when I want to add some widgets to my QWebENgineVIew,
    I have to resize the widget every time before it shows.

    
    MyWebEngineView::MyWebEngineView(*parent) : QWebEngineView(parent)
    {
    	QVBoxLayout* layout = qobject_cast<QVBoxLayout*>(this->layout());
    	qDebug() << layout;
    	if (nullptr == layout) {
    		layout = new QVBoxLayout(this);
    	}
    	layout->setSpacing(0);
    	QToolBar* toolBar = createToolBar();
    	layout->addWidget(toolBar);
    	progressBar = new QProgressBar(this);
    	progressBar->setMaximumHeight(1);
    	progressBar->setTextVisible(false);
    	progressBar->setStyleSheet(QStringLiteral("QProgressBar {border: 0px} QProgressBar::chunk {background-color: #da4453}"));
    	layout->addWidget(progressBar);
    
    	this->extendToolWidget = new QWidget(this);
    	this->extendToolWidget->hide();
    	layout->addWidget(this->extendToolWidget);
    }
    
        MyWebView *webView = new MyWebView;
        MyWebPage *webPage = new MyWebPage(QWebEngineProfile::defaultProfile(), webView);
        webView->setPage(webPage);
    
        setupView(webView);
        webView->resize(currentWidget()->size());
        webView->show();
    

    and when I show this widget , I can see my tool bar move from center to top, rather than always on the top.

    so what can i do to make my tool ba stay on the top, Thank you.



  • @Mozzie
    I could be quite mistaken here, but

        webView->resize(currentWidget()->size());
        webView->show();
    

    You are calling size()/resize() before the widget/webview has been shown. Sometimes (always?) sizing info is not ready before show. You should at least debug out what size that is doing? Do you need to do it later, after showing has happened (e.g. https://doc.qt.io/qt-5/qwebenginepage.html#loadFinished)??



  • @JonB
    hi,
    I have debuged this code

        webView->resize(currentWidget()->size());
        webView->show();
    

    and the currentWidget is already shown before I create a new webView, so the size is correct.
    and actually my situation is before I load a url, everytime when I create a blank webview with my toolbar and show it, I have to call this code up there, but I can see my tool bar move from center to top, if not, my toolbar just stay on the center,

    It is not like other widget if you create a new widget when you show it, you don't need to resize and the widgets in it's layout is right on their position.


Log in to reply