Qt QTextBrowser的一個小例子

Large applications may require more online help than tooltips, status tips, and "What's This?" help can reasonably show. A simple solution to this is to provide a help browser. Applications that include a help browser typically have a Help entry in the main window's Help menu and a Help button in every dialog.

In this section, we present the simple help browser shown in figure 1 and explain how it can be used within an application. The window uses aQTextBrowser to display help pages that are marked up with an HTML-based syntax. QTextBrowser can handle a lot of HTML tags, so it is ideal for this purpose.


上面那段英文想看就看看,不想看就不要看了,關於QTextBrowser是什麼這裏就不說了,Qt官網上肯定有的,谷歌百度都可以找到的。

這個只是一個很小例子。希望能對大家學習Qt能有幫助。

main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"
#include "helpbrowser.h"
#include <QString>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    /*MainWindow w;
    w.show();*/
    const QString path("/root/hjm/wroking/Help/helpFile1130/Help_File1130");
    const QString page("home.html");
    helpbrowser h(path,page);
    h.setGeometry(500,300,500,400);
    h.show();

    
    return a.exec();
}

helpbrowser.h

#ifndef HELPBROWSER_H
#define HELPBROWSER_H

#include <QWidget>
#include <QPushButton>
#include <QTextBrowser>
class QPushButton;
class QTextBrowser;

class helpbrowser : public QWidget
{
    Q_OBJECT
public:
    explicit helpbrowser(const QString &path,const QString &page,QWidget *parent = 0);
    //static void showpage(const QString &page);
private slots:
    void updateWindowTitle();
private:
    QTextBrowser *textBrowser;
    QPushButton  *homeButton;
    QPushButton  *backButton;
    QPushButton  *closeButton;

    
signals:
    
public slots:
    
};

#endif // HELPBROWSER_H

helpbrowser.cpp


#include "helpbrowser.h"
#include <QtGui>
#include <QString>

helpbrowser::helpbrowser(const QString &path,const QString &page,QWidget *parent) :
    QWidget(parent)
{
   setAttribute(Qt::WA_DeleteOnClose);
   setAttribute(Qt::WA_GroupLeader);
   textBrowser = new QTextBrowser;
   homeButton =new QPushButton(tr("&Home"));
   backButton =new QPushButton(tr("&Back"));
   closeButton =new QPushButton(tr("Close"));
   closeButton->setShortcut(tr("Esc"));
   QHBoxLayout *buttonLayout =new QHBoxLayout;
   buttonLayout->addWidget(homeButton);
   buttonLayout->addWidget(backButton);
   buttonLayout->addStretch();
   buttonLayout->addWidget(closeButton);

   QVBoxLayout *mainLayout =new QVBoxLayout;
   mainLayout->addLayout(buttonLayout);
   mainLayout->addWidget(textBrowser);
   setLayout(mainLayout);
   connect(homeButton,SIGNAL(clicked()),textBrowser,SLOT(home()));
   connect(backButton,SIGNAL(clicked()),textBrowser,SLOT(backward()));
   connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));
   connect(textBrowser,SIGNAL(sourceChange(const QUrl&)),this,SLOT(updateWindowTitle()));
   textBrowser->setSearchPaths(QStringList()<<path<<":/images");
   textBrowser->setSource(page);
}

void helpbrowser::updateWindowTitle()
{
    setWindowTitle(tr("Help:%1").arg(textBrowser->documentTitle()));
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章