【问题标题】:Qt - QLineEdit doesn't update the page and URLQt - QLineEdit 不更新页面和 URL
【发布时间】:2018-11-29 08:19:16
【问题描述】:

我的网络浏览器项目有一个小问题。每当我输入 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;
}

【问题讨论】:

  • changeUrlBar(QUrl) 中,使用参数作为新URL 代替browserView-&gt;url() 是否有意义?根据信号发出的时间,可能url()返回的值是旧的(虽然在文档中看起来不像,但值得一试)
  • void MainWindow::changeUrlBar(QUrl newPageUrl) { QString m_newPageUrl = newPageUrl.toString(); urlBar->setText(m_newPageUrl);如果这就是你的意思(我猜)那么它就行不通了。 :(当然,这是有道理的,但正如你所说,值应该已经更新了。

标签: c++ qt qlineedit qwebengineview


【解决方案1】:

您的实际问题是您定义了两个局部变量(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-&gt;urlBar) 访问成员,或者为成员使用不同的表示法(如m_urlBarurlBar_)。此外,许多编译器应该对此提出警告。

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多