Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Simulating iput type="file" process



  • Hi all,

    I have a webpage where I have an input type="file" section to upload selected images from the computer. So, as you know, user clicks on the button and a dialog box opens to select the images from the computer and whenever user presses ok button on the dialog box the image is uploaded to a certain location.
    I have a QWebView to load the page and I can simulate the button click already. However, I couldn't figure out how I can fill the opened dialog box. I tried to use keyboard events but couldn't get it working as well. Could anyone guide me a bit?

    My target html line is the following:
    @<input id="fileupload" type="file" name="files[]" data-url="/server/images/e1b2d17b7b7eccef21a0a0ba1756d35c/upload/" multiple="">@

    My code to trigger the user click where element is the element.findFirst("input[id='fileupload']");
    @ QRect elGeom=element.geometry();
    QPoint elPoint=elGeom.center();
    int elX=elPoint.x();
    int elY=elPoint.y();
    int webWidth=ui->webView->width();
    int webHeight=ui->webView->height();
    int pixelsToScrolRight=0;
    int pixelsToScrolDown=0;
    if (elX>webWidth) pixelsToScrolRight=elX-webWidth+elGeom.width()/2+10; //the +10 part if for the page to scroll a bit further
    if (elY>webHeight) pixelsToScrolDown=elY-webHeight+elGeom.height()/2+10; //the +10 part if for the page to scroll a bit further
    ui->webView->page()->mainFrame()->setScrollBarValue(Qt::Horizontal,pixelsToScrolRight);
    ui->webView->page()->mainFrame()->setScrollBarValue(Qt::Vertical,pixelsToScrolDown);
    QPoint pointToClick(elX-pixelsToScrolRight,elY-pixelsToScrolDown);

    QMouseEvent pressEvent(QMouseEvent::MouseButtonPress,pointToClick,Qt::LeftButton,Qt::LeftButton,Qt::NoModifier);
    QCoreApplication::sendEvent(ui->webView, &pressEvent);
    QMouseEvent releaseEvent(QMouseEvent::MouseButtonRelease,pointToClick,Qt::LeftButton,Qt::LeftButton,Qt::NoModifier);
    QCoreApplication::sendEvent(ui->webView, &releaseEvent);@
    

    Thanks a lot.



  • Can anyone tell me how I can set the focus on the opened Dialog Box where user selects the image when clicked to the button? Why doesn't it type whatever I send from keyboard event? For example,

    @QKeyEvent key(QEvent::KeyPress, Qt::Key_Escape, Qt::NoModifier);
    QApplication::sendEvent(ui->webView, &key);@

    I would expect that the dialog box will close since Escape button is simulated. What is wrong here?


Log in to reply