Qt - QLineEdit 不更新页面并且 URL

Qt - QLineEdit doesn't update the page and URL

我的网络浏览器项目有一个小问题。每当我输入 URL 地址(通过 QLineEdit)时,浏览器都不会显示该页面,每当我更改页面(通过包含起始页的现场点击)时,该地址都不会显示在URL吧。

这是我的 mainwindow.cpp 代码。程序执行并退出,代码为 0。我尝试在函数(changeUrlBar(QUrl) 和 setUrl())中使用 qDebug,结果发现程序进入了这些函数,但它们什么也没做。每一个建议将不胜感激。

#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;
}

我现在觉得自己像个白痴,因为我设法解决了这个问题,唯一要做的事情就是删除以下行:

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

因为这些对象已经初始化。

你的实际问题是你定义了两个局部变量(urlBarbrowserView),它们隐藏了 MainWindow::urlBarMainWindow::browserView 的声明。

那些本地对象是添加到用户界面的对象,但在槽中您使用的是成员对象(那些未包含在 UI 中的对象)。即使在构造函数中初始化它们,它们既不会接收用户输入,也不会显示在用户界面上。

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
}

道德:避免隐藏或意识到这一点 ;)。用于减少这种情况的一些 技巧 是始终通过 this (this->urlBar) 访问成员,或者对成员使用不同的表示法(如 m_urlBarurlBar_)。此外,许多编译器应该对此发出警告。