Results 1 to 5 of 5

Thread: QTextBrowser::forward() and backward()

  1. #1
    Join Date
    Sep 2006
    Posts
    38
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default 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!

    Qt Code:
    1. HelpWindow::HelpWindow(QWidget *parent, Qt::WFlags fl)
    2. : QMainWindow(parent, fl)
    3. {
    4. setupUi( this );
    5. setupActions();
    6. setupToolbar();
    7. openFile("index.html");
    8. QDir appDir;
    9. QString helpDir = appDir.currentPath() + "/doc/";
    10. QString helpImages = helpDir + "images";
    11. QStringList helpPaths;
    12. helpPaths<<helpDir<<helpImages;
    13. helpBrowser->setSearchPaths(helpPaths);
    14.  
    15. }
    16.  
    17. HelpWindow::~HelpWindow()
    18. {
    19. }
    20.  
    21. void HelpWindow::setupActions()
    22. {
    23.  
    24. forwardAct = new QAction(QIcon(":MyApp/images/forward_arrow.png"), tr("Forward"), this);
    25. forwardAct->setStatusTip(tr("Go Forward"));
    26. connect(forwardAct, SIGNAL(triggered()), this, SLOT(goForward()));
    27.  
    28. backAct = new QAction(QIcon(":MyApp/images/back_arrow.png"), tr("Back"), this);
    29. backAct->setStatusTip(tr("Go Back"));
    30. connect(backAct, SIGNAL(triggered()), this, SLOT(goBack()));
    31.  
    32. homeAct = new QAction(QIcon(":MyApp/images/home.png"), tr("Home"), this);
    33. homeAct->setStatusTip(tr("Go Home"));
    34. connect(homeAct, SIGNAL(triggered()), this, SLOT(helpBrowser->home()));
    35.  
    36. }
    37.  
    38. void HelpWindow::setupToolbar()
    39. {
    40. navToolBar = addToolBar(tr("Nav"));
    41. navToolBar->addAction(backAct);
    42. navToolBar->addAction(forwardAct);
    43. navToolBar->addAction(homeAct);
    44. }
    45.  
    46. void HelpWindow::openFile(const QString &fileName)
    47. {
    48. QDir appDir;
    49. QString fileToLoad = appDir.currentPath() + "/doc/"+fileName;
    50. QFileInfo fileInfo(fileToLoad);
    51. if (fileInfo.exists()) loadFile(fileToLoad);
    52. }
    53.  
    54. void HelpWindow::loadFile(const QString &fileName)
    55. {
    56. QFile file(fileName);
    57. if (!file.open(QFile::ReadOnly | QFile::Text)) {
    58. QMessageBox::warning(this, "Program Help", QString("Cannot read file %1: \n%2. ")
    59. .arg(fileName).arg(file.errorString()));
    60. return;
    61. }
    62. QTextStream in(&file);
    63. helpBrowser->setHtml(in.readAll());
    64. }
    65.  
    66. void HelpWindow::goForward()
    67. {
    68. helpBrowser->forward();
    69. }
    70.  
    71. void HelpWindow::goBack()
    72. {
    73. helpBrowser->backward();
    74. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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?

  3. #3
    Join Date
    Sep 2006
    Posts
    38
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default 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

    Qt Code:
    1. #ifndef HELPWINDOW_H
    2. #define HELPWINDOW_H
    3.  
    4. #include "ui_HelpWindow.h"
    5.  
    6. class QToolBar;
    7.  
    8. class HelpWindow : public QMainWindow, private Ui::HelpWindow
    9. {
    10. Q_OBJECT
    11. public:
    12. HelpWindow(QWidget* parent = 0, Qt::WFlags fl = 1);
    13. ~HelpWindow();
    14.  
    15. private slots:
    16. void goForward();
    17. void goBack();
    18.  
    19.  
    20. private:
    21. void openFile(const QString &fileName);
    22. void loadFile(const QString &fileName);
    23. void setupActions();
    24. void setupToolbar();
    25.  
    26. QToolBar *navToolBar;
    27. QAction* backAct;
    28. QAction* forwardAct;
    29. QAction* homeAct;
    30.  
    31. };
    32. #endif //helpwindow_h
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextBrowser::forward() and backward()

    This works:

    Qt Code:
    1. #include <QApplication>
    2. #include <QTextBrowser>
    3. #include <QWidget>
    4. #include <QVBoxLayout>
    5. #include <QPushButton>
    6. #include <QUrl>
    7.  
    8. int main(int argc, char **argv){
    9. QApplication app(argc, argv);
    10. QVBoxLayout *layout = new QVBoxLayout();
    11. QPushButton *frwd = new QPushButton("Forward");
    12. QPushButton *bck = new QPushButton("Back");
    13. layout->addWidget(frwd);
    14. layout->addWidget(bck);
    15. layout->addWidget(&brws);
    16. w.setLayout(layout);
    17. QObject::connect(frwd, SIGNAL(clicked()), &brws, SLOT(forward()));
    18. QObject::connect(bck, SIGNAL(clicked()), &brws, SLOT(backward()));
    19. QObject::connect(&brws, SIGNAL(forwardAvailable(bool)), frwd, SLOT(setEnabled(bool)));
    20. QObject::connect(&brws, SIGNAL(backwardAvailable(bool)), bck, SLOT(setEnabled(bool)));
    21. brws.setSource(QUrl::fromLocalFile("file1.html"));
    22. w.show();
    23. return app.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 

    The magic seems to be in using setSource() instead of setHtml().

  5. The following 2 users say thank you to wysota for this useful post:

    derrickbj (13th October 2006), jml (13th December 2007)

  6. #5
    Join Date
    Sep 2006
    Posts
    38
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QTextBrowser::forward() and backward()

    Yep! it was in the setSource()...thanks!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.