Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from basebrowser import *
- from PyQt4.QtGui import *
- from PyQt4.QtCore import *
- class JSSelectList(QAbstractListModel):
- def __init__ (self, _id, _jsFunc, parent = None):
- super(JSSelectList, self).__init__(parent)
- self.id = _id
- self.jsFunc = _jsFunc
- def data(self, index, role=Qt.DisplayRole):
- if not index.isValid():
- return QVariant()
- if role == Qt.DisplayRole:
- jsstring = QString("document.getElementById('%1').options[%2].textContent").arg(self.id).arg(index.row())
- jsreturn = self.jsFunc(jsstring)
- return jsreturn.toString().trimmed()
- def rowCount(self, index=QModelIndex()):
- jsstring = QString("document.getElementById('%1').length").arg(self.id)
- jsreturn = self.jsFunc(jsstring)
- ok = False
- count, ok = jsreturn.toInt()
- return count if ok else 0
- def headerData(self, section, orientation, role=Qt.DisplayRole):
- if role != Qt.DisplayRole:
- return QVariant()
- else:
- return self.id
- class JSComboBoxDemo(BaseBrowser):
- def __init__(self, parent = None):
- super(JSComboBoxDemo, self).__init__(parent)
- self.vendorComboBox = QComboBox()
- id = QString("productLine")
- self.vendorListModel = JSSelectList(id, self.webView.page().currentFrame().evaluateJavaScript)
- self.vendorComboBox.setModel(self.vendorListModel)
- self.connect(self.vendorComboBox, SIGNAL("currentIndexChanged(int)"), self.setSelectOnWebPage);
- self.connect(self.webView, SIGNAL("loadFinished(bool)"), self.initComboBox)
- self.layout.addWidget(self.vendorComboBox, 2, 0, 1, 1)
- self.webView.load(QUrl("http://www.amd.com"))
- def setSelectOnWebPage(self, new_id):
- jsstring = QString("document.getElementById('productLine').selectedIndex=%1").arg(new_id)
- self.webView.page().currentFrame().evaluateJavaScript(jsstring)
- def initComboBox(self):
- self.vendorComboBox.setCurrentIndex(0)
- if __name__ == "__main__":
- import sys
- app = QApplication(sys.argv)
- ui = JSComboBoxDemo()
- ui.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement