Advertisement
Guest User

Simple PyQt JSSelect example

a guest
Jan 7th, 2011
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from basebrowser import *
  3. from PyQt4.QtGui import *
  4. from PyQt4.QtCore import *
  5.  
  6. class JSSelectList(QAbstractListModel):
  7.     def __init__ (self, _id,  _jsFunc,  parent = None):
  8.         super(JSSelectList, self).__init__(parent)      
  9.         self.id = _id
  10.         self.jsFunc = _jsFunc
  11.        
  12.     def data(self, index, role=Qt.DisplayRole):
  13.         if not index.isValid():            
  14.             return QVariant()
  15.         if role == Qt.DisplayRole:
  16.             jsstring = QString("document.getElementById('%1').options[%2].textContent").arg(self.id).arg(index.row())
  17.             jsreturn = self.jsFunc(jsstring)
  18.             return jsreturn.toString().trimmed()
  19.        
  20.     def rowCount(self, index=QModelIndex()):
  21.         jsstring = QString("document.getElementById('%1').length").arg(self.id)
  22.         jsreturn = self.jsFunc(jsstring)
  23.         ok = False
  24.         count, ok = jsreturn.toInt()
  25.         return count if ok else 0
  26.        
  27.     def headerData(self, section, orientation, role=Qt.DisplayRole):
  28.         if role != Qt.DisplayRole:
  29.             return QVariant()
  30.         else:
  31.             return self.id
  32.  
  33. class JSComboBoxDemo(BaseBrowser):
  34.     def __init__(self,  parent = None):
  35.         super(JSComboBoxDemo, self).__init__(parent)
  36.         self.vendorComboBox = QComboBox()
  37.         id = QString("productLine")
  38.         self.vendorListModel = JSSelectList(id, self.webView.page().currentFrame().evaluateJavaScript)
  39.         self.vendorComboBox.setModel(self.vendorListModel)
  40.         self.connect(self.vendorComboBox, SIGNAL("currentIndexChanged(int)"), self.setSelectOnWebPage);
  41.         self.connect(self.webView, SIGNAL("loadFinished(bool)"), self.initComboBox)
  42.         self.layout.addWidget(self.vendorComboBox, 2, 0,  1,  1)
  43.         self.webView.load(QUrl("http://www.amd.com"))
  44.  
  45.  
  46.     def setSelectOnWebPage(self, new_id):
  47.         jsstring = QString("document.getElementById('productLine').selectedIndex=%1").arg(new_id)
  48.         self.webView.page().currentFrame().evaluateJavaScript(jsstring)
  49.  
  50.     def initComboBox(self):
  51.         self.vendorComboBox.setCurrentIndex(0)
  52.  
  53. if __name__ == "__main__":
  54.     import sys
  55.     app = QApplication(sys.argv)
  56.     ui = JSComboBoxDemo()
  57.     ui.show()
  58.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement