当前位置: 首页>>代码示例>>C++>>正文


C++ QWebElement::setFocus方法代码示例

本文整理汇总了C++中QWebElement::setFocus方法的典型用法代码示例。如果您正苦于以下问题:C++ QWebElement::setFocus方法的具体用法?C++ QWebElement::setFocus怎么用?C++ QWebElement::setFocus使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在QWebElement的用法示例。


在下文中一共展示了QWebElement::setFocus方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: click

int PhpWebView::click(const QString & xpath, bool samewnd)
{
	QWebElement elem = getElementByXPath(QString(xpath));
	
	if (elem.isNull())
		return 0;

	elem.setFocus();

	if (samewnd)
		elem.removeAttribute("target");

	QString js = "var node = this; var x = node.offsetLeft; var y = node.offsetTop; ";
	js += "var w = node.offsetWidth/2; ";
	js += "var h = node.offsetHeight/2; ";
	js += "while (node.offsetParent != null) { ";
	js += "   node = node.offsetParent; ";
	js += "   x += node.offsetLeft; ";
	js += "   y += node.offsetTop; ";
	js += "} ";
	js += "[x+w, y+h]; ";
	QList<QVariant> vlist = elem.evaluateJavaScript(js).toList();
	QPoint point;
	point.setX(vlist.at(0).toInt());
	point.setY(vlist.at(1).toInt());

	QRect elGeom = elem.geometry();
	QPoint elPoint = elGeom.center();
	int elX = point.x(); //elPoint.x();
	int elY = point.y(); //elPoint.y();
	int webWidth = width();
	int webHeight = height();
	int pixelsToScrollRight=0;
	int pixelsToScrollDown=0;
	if (elX > webWidth)
		pixelsToScrollRight = elX-webWidth+elGeom.width()/2+50; //the +10 part if for the page to scroll a bit further
	if (elY > webHeight)
		pixelsToScrollDown = elY-webHeight+elGeom.height()/2+50; //the +10 part if for the page to scroll a bit further
	
	/*pixelsToScrollRight = elX-elGeom.width()/2-50;
	pixelsToScrollDown = elY-elGeom.height()/2-50;

	if (pixelsToScrollRight < 0)
		pixelsToScrollRight = 0;
	if (pixelsToScrollRight > page()->mainFrame()->scrollBarMaximum(Qt::Horizontal))
		pixelsToScrollRight = page()->mainFrame()->scrollBarMaximum(Qt::Horizontal);
	if (pixelsToScrollDown < 0)
		pixelsToScrollDown = 0;
	if (pixelsToScrollDown > page()->mainFrame()->scrollBarMaximum(Qt::Vertical))
		pixelsToScrollDown = page()->mainFrame()->scrollBarMaximum(Qt::Vertical);*/

	int oldHoriz = page()->mainFrame()->scrollBarValue(Qt::Horizontal);
	int oldVert = page()->mainFrame()->scrollBarValue(Qt::Vertical);

	page()->mainFrame()->setScrollBarValue(Qt::Horizontal, pixelsToScrollRight);
	page()->mainFrame()->setScrollBarValue(Qt::Vertical, pixelsToScrollDown);
	QPoint pointToClick(elX-pixelsToScrollRight, elY-pixelsToScrollDown);

	QEventLoop loop;
	isNewViewCreated = false;
	isNewViewBegin = false;
	
	//QMouseEvent *pressEvent = new QMouseEvent(QMouseEvent::MouseButtonPress, pointToClick, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
	//QApplication::postEvent(browser, pressEvent);
	//QApplication::processEvents();

	//QMouseEvent releaseEvent(QMouseEvent::MouseButtonRelease,pointToClick,Qt::LeftButton,Qt::LeftButton,Qt::NoModifier);
	//QApplication::sendEvent(browser, &releaseEvent);

	QString js2 = "var e = document.createEvent('MouseEvents');";
    //js2 += "e.initEvent( 'click', true, true );";
	//js2 += "e.initMouseEvent('click', true, true, window, 0, 0, 0, "+QString::number(elX)+", "+QString::number(elY)+", false, false, false, false, 0, null);";
	js2 += "e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);";
    js2 += "this.dispatchEvent(e);";
	elem.evaluateJavaScript(js2);
	
	QTimer::singleShot(1000, &loop, SLOT(quit()));
	loop.exec();
	
	while (browser && isNewViewBegin && !isNewViewCreated)
		loop.processEvents();

	//page()->mainFrame()->setScrollBarValue(Qt::Horizontal, oldHoriz);
	//page()->mainFrame()->setScrollBarValue(Qt::Vertical, oldVert);
	return 1;
}
开发者ID:scraperlab,项目名称:browserext,代码行数:86,代码来源:phpwebview.cpp


注:本文中的QWebElement::setFocus方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。