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




  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值