Qt의 WebView를 이용하여 웹 브라우저를 만들어 보았다.

주소창과 뒤로, 앞으로 버튼, 새로 고침과 중지 버튼이 있으며 따로 브라우저 창의 투명도를 조절할 수 있는 가로 슬라이더도 추가했다.

setWindowOpacity(value) 함수로 윈도우 창의 투명도를 조절할 수 있으며, value의 타입은 0~1 사이의 float이다.



실행 후 네이버에 접속한 모습이다.



네이버에 접속 후 투명도를 낮춘 모습이다.


  [ Source ]


- Main.py


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
 
# Form implementation generated from reading ui file 'Main.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
 
from PyQt4 import QtCore, QtGui
 
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s
 
try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)
 
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(593395)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.webView = QtWebKit.QWebView(self.centralwidget)
        self.webView.setGeometry(QtCore.QRect(040591331))
        self.webView.setUrl(QtCore.QUrl(_fromUtf8("about:blank")))
        self.webView.setObjectName(_fromUtf8("webView"))
        self.horizontalLayoutWidget = QtGui.QWidget(self.centralwidget)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0059141))
        self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
        self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.button_prev = QtGui.QPushButton(self.horizontalLayoutWidget)
        self.button_prev.setObjectName(_fromUtf8("button_prev"))
        self.horizontalLayout.addWidget(self.button_prev)
        self.button_stop = QtGui.QPushButton(self.horizontalLayoutWidget)
        self.button_stop.setObjectName(_fromUtf8("button_stop"))
        self.horizontalLayout.addWidget(self.button_stop)
        self.button_reload = QtGui.QPushButton(self.horizontalLayoutWidget)
        self.button_reload.setObjectName(_fromUtf8("button_reload"))
        self.horizontalLayout.addWidget(self.button_reload)
        self.button_next = QtGui.QPushButton(self.horizontalLayoutWidget)
        self.button_next.setObjectName(_fromUtf8("button_next"))
        self.horizontalLayout.addWidget(self.button_next)
        self.lineEdit_url = QtGui.QLineEdit(self.horizontalLayoutWidget)
        self.lineEdit_url.setObjectName(_fromUtf8("lineEdit_url"))
        self.horizontalLayout.addWidget(self.lineEdit_url)
        self.horizontalSlider_opacity = QtGui.QSlider(self.horizontalLayoutWidget)
        self.horizontalSlider_opacity.setMaximum(99)
        self.horizontalSlider_opacity.setSliderPosition(99)
        self.horizontalSlider_opacity.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider_opacity.setObjectName(_fromUtf8("horizontalSlider_opacity"))
        self.horizontalLayout.addWidget(self.horizontalSlider_opacity)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0059321))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)
 
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
 
    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow""WebBrowser", None))
        self.button_prev.setText(_translate("MainWindow""<", None))
        self.button_stop.setText(_translate("MainWindow""Stop", None))
        self.button_reload.setText(_translate("MainWindow""Reload", None))
        self.button_next.setText(_translate("MainWindow"">", None))
 
from PyQt4 import QtWebKit
 
cs


- Start.py


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import sys
from Main import Ui_MainWindow
from PyQt4 import QtCore, QtGui, QtWebKit
 
class StartQT4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.start_page = "about:blank"
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.initialize()
        self.ui.webView.page().setLinkDelegationPolicy(QtWebKit.QWebPage.DelegateAllLinks)
 
        QtCore.QObject.connect(self.ui.lineEdit_url, QtCore.SIGNAL("returnPressed()"), self.url_changed)
        QtCore.QObject.connect(self.ui.button_reload, QtCore.SIGNAL("clicked()"), self.reload_clicked)
        QtCore.QObject.connect(self.ui.button_prev, QtCore.SIGNAL("clicked()"), self.prev_clicked)
        QtCore.QObject.connect(self.ui.button_next, QtCore.SIGNAL("clicked()"), self.next_clicked)
        QtCore.QObject.connect(self.ui.button_stop, QtCore.SIGNAL("clicked()"), self.stop_clicked)
        QtCore.QObject.connect(self.ui.webView, QtCore.SIGNAL("linkClicked (const QUrl&)"), self.link_clicked)
        QtCore.QObject.connect(self.ui.webView, QtCore.SIGNAL("loadProgress (int)"), self.load_progress)
        QtCore.QObject.connect(self.ui.webView, QtCore.SIGNAL("titleChanged (const QString&)"), self.title_changed)
        QtCore.QObject.connect(self.ui.horizontalSlider_opacity, QtCore.SIGNAL("sliderMoved(int)"), self.opacity_changed)
 
    def stop_clicked(self):
        self.ui.webView.stop()
 
    def reload_clicked(self):
        self.ui.webView.setUrl(QtCore.QUrl(self.ui.lineEdit_url.text()))
 
    def prev_clicked(self):
        page = self.ui.webView.page()
        history = page.history()
        history.back()
        if history.canGoBack():
            self.ui.button_prev.setEnabled(True)
        else:
            self.ui.button_prev.setEnabled(False)
 
        self.ui.button_next.setEnabled(True)
 
 
    def next_clicked(self):
        page = self.ui.webView.page()
        history = page.history()
        history.forward()
        if history.canGoForward():
            self.ui.button_next.setEnabled(True)
        else:
            self.ui.button_next.setEnabled(False)
 
        self.ui.button_prev.setEnabled(True)
 
    def url_changed(self):
        # url changed by user
        curr_page = self.ui.webView.page()
        history = curr_page.history()
        if history.canGoBack():
            self.ui.button_prev.setEnabled(True)
        else:
            self.ui.button_prev.setEnabled(False)
 
        if history.canGoForward():
            self.ui.button_next.setEnabled(True)
        else:
            self.ui.button_next.setEnabled(False)
 
        self.ui.button_reload.setEnabled(True)
        url = self.ui.lineEdit_url.text()
        self.ui.webView.setUrl(QtCore.QUrl(url))
 
    def link_clicked(self, url):
        # link clicked
        curr_page = self.ui.webView.page()
        history = curr_page.history()
        if history.canGoBack():
            self.ui.button_prev.setEnabled(True)
        else:
            self.ui.button_prev.setEnabled(False)
 
        if history.canGoForward():
            self.ui.button_next.setEnabled(True)
        else:
            self.ui.button_next.setEnabled(False)
 
        self.ui.button_reload.setEnabled(True)
        self.ui.lineEdit_url.setText(url.toString())
        self.reload_clicked()
 
    def load_progress(self, load):
        if load == 100:
            self.ui.button_stop.setEnabled(False)
        else:
            self.ui.button_stop.setEnabled(True)
 
    def title_changed(self, title):
        self.setWindowTitle(title)
 
    def opacity_changed(self, value):
        self.setWindowOpacity((value + 1)/100.0)
 
 
    def initialize(self):
        self.ui.button_prev.setEnabled(False)
        self.ui.button_next.setEnabled(False)
        self.ui.button_stop.setEnabled(False)
        self.ui.button_reload.setEnabled(False)
        self.ui.lineEdit_url.setText(self.start_page)
        self.ui.webView.setUrl(QtCore.QUrl(self.start_page))
 
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())
cs


github : https://github.com/skyclad0x7b7/StudyPyQt4/tree/master/0x07.%20WebBrowser





'Programming' 카테고리의 다른 글

[Python] Pacro : Python 기반 매크로 프로그램  (4) 2016.11.28
[C++] CreateMutex  (2) 2016.11.22
[PyQt4] 0x07. WebBrowser  (0) 2016.11.21
[PyQt4] 0x06. QYolk III  (0) 2016.11.18
[PyQt4] 0x05. Final Text Editor  (0) 2016.11.17
[PyQt4] 0x04. QYolk II  (0) 2016.11.16
블로그 이미지

__미니__

E-mail : skyclad0x7b7@gmail.com 나와 계약해서 슈퍼 하-카가 되어 주지 않을래?

댓글을 달아 주세요