关于解决QT调用外部程序后,本体程序隐藏在后的问题

笔者在QT过程中,其中有一部分调用外部程序,当外部程序运行完成后调用本体程序。

QProcess loginProcess;
        QString exePath = QCoreApplication::applicationDirPath();
        GetInfoFromSharedMemory o_getInfoFromSharedMeory;
        if(!o_getInfoFromSharedMeory.setSqlServer()){
            qCritical()<<"SharedMemory Open Failed!";
        }
        //共享内存没有数据才调用LoginUI再获取一遍
        if(!o_getInfoFromSharedMeory.preLogin()){
            loginProcess.start("\""+exePath+"\\LoginUI.exe\"");
            // 登录成功,启动程序a的主界面
            if (!loginProcess.waitForFinished(-1)) {
                // 登录程序退出后,启动程序a的主界面
                QMessageBox::warning(0,"warning","程序异常!");
                a.quit();
            }
            if(!o_getInfoFromSharedMeory.login()){
                qCritical()<<"登陆失败!";
            }
        }
        MainWindow w;
        w.show();
        return a.exec(); // 程序运行
当登录程序LoginUI完成登录后,主窗口处于底层状态。主程序MainWindow会在其构造函数中,调用对话框。代码如下:
bool MainWindow::showUpdatePrompt() { 
     QMessageBox msgBox;
     msgBox.setText("有新版本发布,是否进行更新?");
     msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
     msgBox.setDefaultButton(QMessageBox::Yes);
     int choice = msgBox.exec();
     return choice == QMessageBox::Yes;
 }

这样调用会导致对话框msgBox处于底层,需要用户手动点击,才能弹到顶层,设计非常不合理,影响用户的体验。

解决方案:

1>指定msgBox的父类

 QMessageBox msgBox(this);  // 将对话框的父窗口设置为 MainWindow

2>设置窗口属性

msgBox.setWindowFlags(msgBox.windowFlags() | Qt::WindowStaysOnTopHint | Qt::Dialog);

Qt::WindowStaysOnTopHint 枚举值会将窗口置顶。

完成(1)、(2) 已经可以将对话框前置,但是这时候我们会发现窗口弹出的位置偏左上角。这个时候还需要对窗口弹出的位置进行设置。

(3)设置窗口弹出的位置:

 QDesktopWidget *desktop = QApplication::desktop();
     QRect screenGeometry = desktop->screenGeometry(this);
     int screenWidth = screenGeometry.width();
     int screenHeight = screenGeometry.height();

     msgBox.show();  // 显示对话框以计算其大小
     msgBox.raise();
     msgBox.activateWindow();

     int msgBoxWidth = msgBox.width();
     int msgBoxHeight = msgBox.height();

     // 计算对话框应该的位置
     int x = (screenWidth - msgBoxWidth) / 2;
     int y = (screenHeight - msgBoxHeight) / 2;

     // 移动对话框到计算的位置
     msgBox.move(x, y);

这样完成(1)、(2)、(3)之后,就可以较好地完成弹窗在前的功能。下附对话框完整代码,以供参考。


 bool MainWindow::showUpdatePrompt() {
     QMessageBox msgBox(this);  // 将对话框的父窗口设置为 MainWindow
     msgBox.setText("有新版本发布,是否进行更新?");
     msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
     msgBox.setDefaultButton(QMessageBox::Yes);
     msgBox.setWindowFlags(msgBox.windowFlags() | Qt::WindowStaysOnTopHint | Qt::Dialog);

     // 调用 exec() 之前获取屏幕和对话框大小
     QDesktopWidget *desktop = QApplication::desktop();
     QRect screenGeometry = desktop->screenGeometry(this);
     int screenWidth = screenGeometry.width();
     int screenHeight = screenGeometry.height();

     msgBox.show();  // 显示对话框以计算其大小
     msgBox.raise();
     msgBox.activateWindow();

     int msgBoxWidth = msgBox.width();
     int msgBoxHeight = msgBox.height();

     // 计算对话框应该的位置
     int x = (screenWidth - msgBoxWidth) / 2;
     int y = (screenHeight - msgBoxHeight) / 2;

     // 移动对话框到计算的位置
     msgBox.move(x, y);

     int choice = msgBox.exec();
     return choice == QMessageBox::Yes;
 }

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值