Question

After close MessageBox the focus self does not return to the window. I tried so:

QMessageBox msgBox;
msgBox.setText("Ok");
msgBox.exec();
this->setFocus();

How to return focus to the main window (this)?

//dialog.h
#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    void loadFile();


    ~Dialog();

private slots:
    void on_buttonLoad2_clicked();


private:
    Ui::Dialog *ui;
};

#endif // DIALOG_H

//dialog.cpp
//
#include "dialog.h"
#include "ui_dialog.h"

#include <QDebug>
#include <QXmlStreamReader>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QEvent>
#include <QDomDocument>
#include <QDomElement>
#include <QFile>
#include <QMessageBox>
#include <QMediaPlayer>
#include <QListWidgetItem>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    this->setFocusPolicy(Qt::StrongFocus);
    ui->setupUi(this);
}

void Dialog::loadFile()
{
    QString urlForXML = "http://apifree.forvo.com/key//format/xml/action/word-pronunciations/word/";
    urlForXML += ui->textHttp->toPlainText() + "/language/de";

    QNetworkAccessManager m_NetworkMngr;
    QNetworkReply *reply = m_NetworkMngr.get(QNetworkRequest(urlForXML));
    QEventLoop loop;
    connect(reply, SIGNAL(finished()),&loop, SLOT(quit()));
    loop.exec();

    QXmlStreamReader xmlReader;
    xmlReader.addData(reply->readAll());
    QString pathmp3Url;
    while(!xmlReader.atEnd()) // пока не конец потока...
    {
        xmlReader.readNextStartElement(); // читаем следующий открывающий элемент (тег)
        if (xmlReader.name() == "pathmp3") // если имя элемента "pathmp3"
        {
            pathmp3Url = xmlReader.readElementText();
            break;
        }
    }
    delete reply;

    if(pathmp3Url.count())
    {
        QNetworkAccessManager m_NetworkMngr2;
        QNetworkReply *reply2 = m_NetworkMngr2.get(QNetworkRequest(pathmp3Url));
        QEventLoop loop2;
        connect(reply2, SIGNAL(finished()), &loop2, SLOT(quit()));
        loop2.exec();

        QString qstring_mp3file = "/users/derkode/documents/forvoclient-mp3/" + ui->textHttp->toPlainText() + ".mp3";
        QFile mp3_file(qstring_mp3file);
        mp3_file.open(QIODevice::WriteOnly);
        mp3_file.write(reply2->readAll());

        QMediaPlayer *player = new QMediaPlayer;
        player->setMedia(QUrl::fromLocalFile(qstring_mp3file));
        player->setVolume(50);
        player->play();

        mp3_file.close();
        delete reply2;
    }
    else
    {
        QMessageBox *msgBox = new QMessageBox(this);
        msgBox->setText("Ok");
        msgBox->exec();
        this->setFocus();


    }

}

Dialog::~Dialog()
{
    delete ui;
}


void Dialog::on_buttonLoad2_clicked()
{
    loadFile();
}
Was it helpful?

Solution

this->parent()->setFocus(); after msgBox->exec() (near end of your code)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top