PDA

View Full Version : QTextBrowser::forward() and backward()



derrickbj
13th October 2006, 19:33
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!



HelpWindow::HelpWindow(QWidget *parent, Qt::WFlags fl)
: QMainWindow(parent, fl)
{
setupUi( this );
setupActions();
setupToolbar();
openFile("index.html");
QDir appDir;
QString helpDir = appDir.currentPath() + "/doc/";
QString helpImages = helpDir + "images";
QStringList helpPaths;
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)
{
QDir appDir;
QString fileToLoad = appDir.currentPath() + "/doc/"+fileName;
QFileInfo fileInfo(fileToLoad);
if (fileInfo.exists()) loadFile(fileToLoad);
}

void HelpWindow::loadFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, "Program Help", QString("Cannot read file %1: \n%2. ")
.arg(fileName).arg(file.errorString()));
return;
}
QTextStream in(&file);
helpBrowser->setHtml(in.readAll());
}

void HelpWindow::goForward()
{
helpBrowser->forward();
}

void HelpWindow::goBack()
{
helpBrowser->backward();
}

wysota
13th October 2006, 20:03
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?

derrickbj
13th October 2006, 20:08
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



#ifndef HELPWINDOW_H
#define HELPWINDOW_H

#include "ui_HelpWindow.h"

class QToolBar;

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();

QToolBar *navToolBar;
QAction* backAct;
QAction* forwardAct;
QAction* homeAct;

};
#endif //helpwindow_h

wysota
13th October 2006, 20:40
This works:


#include <QApplication>
#include <QTextBrowser>
#include <QWidget>
#include <QVBoxLayout>
#include <QPushButton>
#include <QUrl>

int main(int argc, char **argv){
QApplication app(argc, argv);
QWidget w;
QVBoxLayout *layout = new QVBoxLayout();
QPushButton *frwd = new QPushButton("Forward");
QPushButton *bck = new QPushButton("Back");
layout->addWidget(frwd);
layout->addWidget(bck);
QTextBrowser brws;
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().

derrickbj
13th October 2006, 21:22
Yep! it was in the setSource()...thanks!