QTextBrowser::forward() and backward()
Does anybody know if there's anything special that needs to be done for QTextBrowser::forward(), backward(), and home() to work? How/where does it store the history? I'm trying to implement a simple HTML browser with only forward, backward, and home buttons. I'm able to load the application fine and open the "index.html" file. i'm able to click on links and have them load fine, but when I trigger the forward and backward actions, the TextBrowser->forward() and TextBrowser->backward() slots don't seem to work. So I didn't know if there was anything "history-wise" I needed to do. Based on the documentation, it seems pretty straightforward, which is why I'm stumped!
Code:
HelpWindow
::HelpWindow(QWidget *parent, Qt
::WFlags fl
){
setupUi( this );
setupActions();
setupToolbar();
openFile("index.html");
QString helpDir
= appDir.
currentPath() + "/doc/";
QString helpImages
= helpDir
+ "images";
helpPaths<<helpDir<<helpImages;
helpBrowser->setSearchPaths(helpPaths);
}
HelpWindow::~HelpWindow()
{
}
void HelpWindow::setupActions()
{
forwardAct
= new QAction(QIcon(":MyApp/images/forward_arrow.png"), tr
("Forward"),
this);
forwardAct->setStatusTip(tr("Go Forward"));
connect(forwardAct, SIGNAL(triggered()), this, SLOT(goForward()));
backAct
= new QAction(QIcon(":MyApp/images/back_arrow.png"), tr
("Back"),
this);
backAct->setStatusTip(tr("Go Back"));
connect(backAct, SIGNAL(triggered()), this, SLOT(goBack()));
homeAct
= new QAction(QIcon(":MyApp/images/home.png"), tr
("Home"),
this);
homeAct->setStatusTip(tr("Go Home"));
connect(homeAct, SIGNAL(triggered()), this, SLOT(helpBrowser->home()));
}
void HelpWindow::setupToolbar()
{
navToolBar = addToolBar(tr("Nav"));
navToolBar->addAction(backAct);
navToolBar->addAction(forwardAct);
navToolBar->addAction(homeAct);
}
void HelpWindow
::openFile(const QString &fileName
) {
QString fileToLoad
= appDir.
currentPath() + "/doc/"+fileName;
if (fileInfo.exists()) loadFile(fileToLoad);
}
void HelpWindow
::loadFile(const QString &fileName
) {
.arg(fileName).arg(file.errorString()));
return;
}
helpBrowser->setHtml(in.readAll());
}
void HelpWindow::goForward()
{
helpBrowser->forward();
}
void HelpWindow::goBack()
{
helpBrowser->backward();
}
Re: QTextBrowser::forward() and backward()
Hello,
did you declare your class properly? Is Q_OBJECT macro present and are goForward/Back defined as slots? Why don't you just connect to the forward/backward slot directly?
Re: QTextBrowser::forward() and backward()
Hey, Thanks for the response.
I did try connecting the forward() and backward() slots directly, as you see with the homeAct action and the home() slot, but this didn't work either. So I broke them out in order to use qDebug() to make sure the trigger() action was happening, which it is.
Here's my header
Code:
#ifndef HELPWINDOW_H
#define HELPWINDOW_H
#include "ui_HelpWindow.h"
class HelpWindow
: public QMainWindow,
private Ui
::HelpWindow{
Q_OBJECT
public:
HelpWindow
(QWidget* parent
= 0, Qt
::WFlags fl
= 1);
~HelpWindow();
private slots:
void goForward();
void goBack();
private:
void openFile
(const QString &fileName
);
void loadFile
(const QString &fileName
);
void setupActions();
void setupToolbar();
};
#endif //helpwindow_h
Re: QTextBrowser::forward() and backward()
This works:
Code:
#include <QApplication>
#include <QTextBrowser>
#include <QWidget>
#include <QVBoxLayout>
#include <QPushButton>
#include <QUrl>
int main(int argc, char **argv){
layout->addWidget(frwd);
layout->addWidget(bck);
layout->addWidget(&brws);
w.setLayout(layout);
QObject::connect(frwd,
SIGNAL(clicked
()),
&brws,
SLOT(forward
()));
QObject::connect(bck,
SIGNAL(clicked
()),
&brws,
SLOT(backward
()));
QObject::connect(&brws,
SIGNAL(forwardAvailable
(bool)), frwd,
SLOT(setEnabled
(bool)));
QObject::connect(&brws,
SIGNAL(backwardAvailable
(bool)), bck,
SLOT(setEnabled
(bool)));
brws.
setSource(QUrl::fromLocalFile("file1.html"));
w.show();
return app.exec();
}
The magic seems to be in using setSource() instead of setHtml().
Re: QTextBrowser::forward() and backward()
Yep! it was in the setSource()...thanks!