0

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;
}
s4ilor
  • 29
  • 3
  • 1
    In `changeUrlBar(QUrl)`, wouldn't it make sense to use the parameter as the new URL instead of `browserView->url()`? Depending on when the signal is emitted, maybe the value returned from `url()` is the old one (although in the doc, it doesn't look like it, but it's worth a try) – Karsten Koop Nov 29 '18 at 08:29
  • void MainWindow::changeUrlBar(QUrl newPageUrl) { QString m_newPageUrl = newPageUrl.toString(); urlBar->setText(m_newPageUrl); } If that's what you mean (I guess) then it doesn't work. :( That makes sense, of course, but as you said the value should be updated already. – s4ilor Nov 29 '18 at 08:40

2 Answers2

0

I feel like an idiot now because I managed to solve this issue and the only thing to do was to delete following lines:

QLineEdit *urlBar = new QLineEdit;
QWebEngineView *browserView = new QWebEngineView(parent);

As these objects were already initialised.

s4ilor
  • 29
  • 3
0

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.

cbuchart
  • 10,847
  • 9
  • 53
  • 93