如何将 QWebView 放置在 QTabWidget 中?
Posted
技术标签:
【中文标题】如何将 QWebView 放置在 QTabWidget 中?【英文标题】:How to place QWebView in QTabWidget? 【发布时间】:2013-06-19 16:21:46 【问题描述】:我是 Python 和 pyQt 的新手。我正在为某个论坛制作桌面应用程序。我的应用程序工作正常,但我不知道如何将 QWebView 元素放置在选项卡“QTabWidget”中
这是我的完整代码:
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui, QtWebKit
from PyQt4.QtWebKit import QWebView
import http.client as http_c
import sys, os, webbrowser #re, html5lib
import lxml.html
from lxml import etree
class BaseWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self, parent)
self.centralWidget = QtGui.QWidget()
self.resize(800, 500)
self.setWindowTitle('php-Forum.ru')
#self.menubar = QtGui.QMenuBar()
#file = self.menubar.addMenu('Файл')
self.tabs = QtGui.QTabWidget()
self.tabs.addTab(QtGui.QWidget(),"Темы");
self.tabs.addTab(QtGui.QWidget(),"СМС");
exit = QtGui.QAction(QtGui.QIcon('icons/exit.png'), 'Выход', self)
exit.setShortcut('Ctrl+Q')
self.connect(exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))
menubar = self.menuBar()
file = menubar.addMenu('Файл')
file.addAction(exit)
settings = menubar.addMenu('Установки')
class Themes(BaseWindow):
def __init__(self, parent = None):
BaseWindow.__init__(self, parent)
self.webview = QWebView()
con = http_c.HTTPConnection('phpforum.ru')
con.request('GET', '/ssi.php?a=news&show=25')
res = con.getresponse()
html_code = res.read().decode('cp1251')
path = os.getcwd()
rpath = os.path.normpath(path + '/resources/').replace('\\', '/')
self.setWindowIcon(QtGui.QIcon(rpath + '/images/favicon.ico'))
doc = lxml.html.document_fromstring(html_code)
topics = doc.xpath('/html/body/table[@class="topic"]')
data = []
i = 0
for topic in topics:
i += 1
t_str = lxml.html.document_fromstring(etree.tostring(topic))
author_name = t_str.xpath('//a[@class="author"]/text()')
author_link = t_str.xpath('//a[@class="author"]/@href')
last_post = t_str.xpath('//span[@class="post_date"]/text()[1]')
title = t_str.xpath('//span[@class="topic_title"]/text()')
topic_link = t_str.xpath('//a[@class="topic_link"]/@href')
topic_text = t_str.xpath('//table[1]//tr[3]/td/text()')
try:
author_name = author_name[0]
except IndexError:
author_name = 'Guest'
author_link = '#'
else:
author_link = author_link[0]
try:
topic_text = topic_text[0]
except IndexError:
topic_text = None
data.append(
'title': title[0],
'author_name': author_name,
'author_link': author_link,
'last_post': last_post[0],
'topic_link': topic_link[0],
'topic_text': topic_text,
)
html_str = """
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="C:/Python33/scripts/pqt/phpforum/resources/css/style.css">
</head>
<body>
"""
for info in data:
html_str += """
<div class="topic">
<span class="title"><a href="topic_link">title</a></span>
<span class="author"><a href="author_link">author_name</a></span>
<span class="time">last_post</span>
</div>
<br>
""".format(**info)
html_str += """
</body>
</html>
"""
self.webview.page().setLinkDelegationPolicy(QtWebKit.QWebPage.DelegateAllLinks)
self.webview.connect(self.webview.page(), QtCore.SIGNAL("linkClicked(const QUrl&)"), self.link_clicked)
self.webview.setHtml(html_str)
centralLayout = QtGui.QVBoxLayout()
centralLayout.addWidget(self.tabs, 1)
centralLayout.addWidget(self.webview, 2)
self.centralWidget.setLayout(centralLayout)
self.setCentralWidget(self.centralWidget)
def link_clicked(self, url):
webbrowser.open(str(url.toString()))
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Themes()
window.show()
sys.exit(app.exec_())
看起来像这样
但我需要它
提前致谢!
【问题讨论】:
【参考方案1】:你得到了你想要的:
centralLayout.addWidget(self.webview, 2)
您必须将self.webview
添加到QtGui.QTabWidget
选项卡内的布局中。
【讨论】:
你能帮帮我吗?请告诉我,如何在选项卡中添加 webview?我需要使用什么方法? 我明白了!!!我需要这样做self.tabs.addTab(self.webview, "Themes");
谢谢!
太好了,您可以自己找到确切的解决方案。您将学到的不仅仅是通过预先准备好的解决方案:-)以上是关于如何将 QWebView 放置在 QTabWidget 中?的主要内容,如果未能解决你的问题,请参考以下文章