Search code examples
c++qtqlineeditqwebengineview

Qt - QLineEdit doesn't update the page and URL


I've got a small problem with my web browser project. Whenever I enter the URL address (via QLineEdit), the browser doesn't show the page, and whenever I change the page (via click on-site with starting page included) the address doesn't show up on the URL bar.

Here's my mainwindow.cpp code. The program executes and exits with code 0. I tried using qDebug inside the functions (changeUrlBar(QUrl) and setUrl()) and it turns out that the program enters these functions but they don't do anything. Every advice would be very appreciated.

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QDebug>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    browserView(new QWebEngineView),
    urlBar(new QLineEdit)
{
    ui->setupUi(this);

    //
    // initialization of widgets and layouts

    // widgets
    QWidget *browserWindow = new QWidget(this);
    QLineEdit *urlBar = new QLineEdit;
    QProgressBar *progressBar = new QProgressBar;
    // WebEngineView - actual web browser
    QWebEngineView *browserView = new QWebEngineView(parent);
    // layouts
    QVBoxLayout *mainLayout = new QVBoxLayout;
    QHBoxLayout *topBarLayout = new QHBoxLayout;
    // push buttons
    QPushButton *buttonBack =  new QPushButton("Back");
    QPushButton *buttonForward = new QPushButton("Forward");
    QPushButton *buttonReload = new QPushButton("Reload");

    //
    // creating the widgets and layouts

    // top bar
    topBarLayout->addWidget(buttonBack);
    topBarLayout->addWidget(buttonForward);
    topBarLayout->addWidget(buttonReload);
    topBarLayout->addWidget(urlBar);

    // main layout of the browser
    mainLayout->addLayout(topBarLayout);
    mainLayout->addWidget(progressBar);
    mainLayout->addWidget(browserView);
    browserWindow->setLayout(mainLayout);
    setCentralWidget(browserWindow);

    //
    // connecting slots and signals

    // internal connections
    connect(buttonBack, SIGNAL(clicked()), browserView, SLOT(back()));
    connect(buttonForward, SIGNAL(clicked()), browserView, SLOT(forward()));
    connect(buttonReload, SIGNAL(clicked()), browserView, SLOT(reload()));
    connect(browserView, SIGNAL(loadProgress(int)), progressBar, SLOT(setValue(int)));

    // browser connections
    connect(browserView, SIGNAL(urlChanged(QUrl)), this, SLOT(changeUrlBar(QUrl)));
    connect(urlBar, SIGNAL(editingFinished()), this, SLOT(setUrl()));


    // set starting page
    browserView->load(QUrl("https://www.wikipedia.org"));
}
void MainWindow::setUrl()
{
    browserView->load(QUrl::fromUserInput(urlBar->text()));
}
void MainWindow::changeUrlBar(QUrl)
{
    urlBar->setText(browserView->url().toString());
}
MainWindow::~MainWindow()
{
    delete ui;
    delete browserView;
    delete urlBar;
}

Solution

  • Your actual problem is that you've defined two local variables (urlBar and browserView) that are hiding the declaration of MainWindow::urlBar and MainWindow::browserView.

    Those local objects are the ones added to the user interface, but in the slots you are using the member objects (those that were not included in the UI). Even when they are initialized in the constructor, they are not neither receiving user input nor being displayed on the user interface.

    MainWindow::MainWindow(QWidget *parent) :
    // ...
        QLineEdit *urlBar = new QLineEdit; // <-- local variable hiding member declaration
        QProgressBar *progressBar = new QProgressBar;
        // WebEngineView - actual web browser
        QWebEngineView *browserView = new QWebEngineView(parent); // <-- local variable hiding member declaration
    // ...
    
    void MainWindow::changeUrlBar(QUrl)
    {
        urlBar->setText(browserView->url().toString()); // <-- urlBar and browserView are members
    }
    

    Moral: avoid hiding or be conscious about it ;). Some tricks used to reduce this are to always access member through this (this->urlBar), or using a different notation for members (like m_urlBar or urlBar_). Also, many compilers should warn about this.