Search code examples
c++qtwebviewqtwebengine

How to get the complete(exact) Url / QUrl Address after going to different website?


I'm creating a mock up of a simple Web Browser using QT Creator 5.9, I have a problem in my EditLine/Text Box:

1. How can I update the displayed URL String automatically after I go to a different website/page.

2. How to get the exact/complete URL string

I tried to searched some codes but none of it worked.

.cpp

void MainWindow::QWebViewFunction() {
    _view = new QWebEngineView();
    _view->load(QUrl("https://www.bing.com"));
    ui->tabWidget->addTab(_view, _view->url().toString());
}

QString MainWindow::getUrlText() {
        QString urlEditBox = ui->setUrlTextBox->text();
        return urlEditBox;
}

void MainWindow::getTabTitle() {
    int x = ui->tabWidget->currentIndex();
    ui->tabWidget->setTabText(x, getUrlText());
}

//-----------                                                               Buttons
void MainWindow::on_tabWidget_tabCloseRequested(int index) {
    ui->tabWidget->removeTab(index);
}

void MainWindow::on_AddBtn_clicked() {
    QWebViewFunction();
}

void MainWindow::on_homeBtn_clicked() {
    _view->load(QUrl("https:google.com"));
    int x = ui->tabWidget->currentIndex();
    ui->tabWidget->setTabText(x, "Google");
}
//-------------                                                             LineEdit

void MainWindow::on_pushButton_clicked(){
    _view->load(QUrl(getUrlText()));
    getTabTitle();
}

void MainWindow::on_backButton_clicked()
{
    _view->page()->triggerAction(QWebEnginePage::Back);
}

.h

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

signals:
   void urlChanged(const QUrl &url);

private slots:
    void on_tabWidget_tabCloseRequested(int index);
    void on_AddBtn_clicked();
    void on_homeBtn_clicked();
    void on_pushButton_clicked();

    void QWebViewFunction();
    QString getUrlText();
    QString setUrlText();
    void getTabTitle();


    void on_backButton_clicked();

private:
    Ui::MainWindow *ui;
    QWebEngineView* _view;


};


#endif // MAINWINDOW_H

Solution

  • You are looking for QWebEngineView.loadProgress(): https://doc.qt.io/qt-5/qwebengineview.html#loadProgress

    Connect that signal from the webview widget, into a slot from your main window, and update the url bar.