开发笔记

  • 首页
  • 工具箱
三味线的博客
  1. 首页
  2. Qt
  3. 正文

QWidget实现QDialog exec() 模态效果

2019-03-18 4524点热度 17人点赞 0条评论

要点

1. setWindowModality(Qt::ApplicationModal);//设置Widget模态
2. 使用 QEventLoop 实现事件循环;
3. 重写 closeEvent ,退出事件循环;

实现

//.h
public:
    int exec();
private:
    QPushButton *m_BtnOK;
    QPushButton *m_BtnCancel;
    int m_Result;
    QEventLoop *m_Loop;
protected:
    void closeEvent(QCloseEvent *event);
public slots:
    void btnOKSlot();
    void btnCancelSlot();
//.cpp
this->setWindowModality(Qt::ApplicationModal);
void MessageBox::btnOKSlot()
{
    m_Result=1;
    this->close();
}
void MessageBox::btnCancelSlot()
{
    m_Result=0;
    this->close();
}
int MessageBox::exec()
{
    m_Result=-1;
    this->show();
    m_Loop=new QEventLoop(this);
    m_Loop->exec();
    return m_Result;
}
void MessageBox::closeEvent(QCloseEvent *event)
{
    if(m_Loop!=NULL)
    {
        m_Loop->exit();
    }
    event->accept();
}
标签: Qt
最后更新:2020-06-06

三味线

不吃咸鱼的喵

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

Captcha Code

COPYRIGHT © 2022 voidcat.cn. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

蜀ICP备18010095号-1