码迷,mamicode.com
首页 > 其他好文 > 详细

界面关闭的事件响应

时间:2014-07-03 22:19:23      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   使用   

最近使用了窗口关闭事件:void QWidget::closeEvent ( QCloseEvent * event ) [virtual protected]

当用鼠标点击关闭按钮或者在任务栏上右击关闭,都会触发这一事件,实现效果如下:

bubuko.com,布布扣

附上实现代码:

这是主界面的关闭事件(CallTrace为主界面)

 1 void CallTrace::closeEvent(QCloseEvent *e)
 2 {
 3     closeDialog = new CloseDialog;
 4     closeDialog->move((QApplication::desktop()->width() - closeDialog->width())/2,
 5                       (QApplication::desktop()->height() - closeDialog->height())/2);
 6     closeDialog->exec();
 7     if (closeDialog->m_flag)//先判断没有出错
 8     {
 9         if (closeDialog->m_flag == 3)//窗口不关闭
10             {
11             e->ignore();
12         }
13         else if (closeDialog->m_flag == 2)//立即关闭
14             {
15                 //关闭线程
16                 pTaskInstance->m_CDR.StopCdr();
17                 emit mysig();        //发射信号,记录窗口及线程状态
18         }
19         else if (closeDialog->m_flag == 1)//隐藏到后台
20         {
21             //什么都不用做
22         }
23     }
24 }
 1 CloseDialog::CloseDialog()
 2 {
 3     m_flag = 0;//窗口关闭选项的初始化
 4     QGroupBox *closeSelection = new QGroupBox(tr("关闭选项"));
 5     hide = new QRadioButton(tr("隐藏到后台"));
 6     hide->setChecked(true);
 7     holdOn = new QRadioButton(tr("立即关闭"));
 8 
 9     QVBoxLayout *vLayout1 = new QVBoxLayout;
10     vLayout1->addWidget(hide);
11     vLayout1->addWidget(holdOn);
12     closeSelection->setLayout(vLayout1);
13 
14     QHBoxLayout *hLayout1 = new QHBoxLayout;
15     okBtn = new QPushButton(tr("确定"));
16     cancelBtn = new QPushButton(tr("取消"));
17     hLayout1->addStretch();
18     hLayout1->addWidget(okBtn);
19     hLayout1->addSpacing(10);
20     hLayout1->addWidget(cancelBtn);
21     hLayout1->addStretch();
22 
23     QHBoxLayout *hLayout2 = new QHBoxLayout;
24     closeCbox = new QCheckBox;
25     closeCbox->setText(tr("默认此选项,以后不再提示"));
26     hLayout2->addWidget(closeCbox);
27 
28     QVBoxLayout *mainLayout = new QVBoxLayout;
29     mainLayout->addWidget(closeSelection);
30     mainLayout->addLayout(hLayout2);
31     mainLayout->addLayout(hLayout1);
32 
33     setLayout(mainLayout);
34     setWindowTitle(tr("提示"));
35     this->resize(400,200);
36 
37     connect(okBtn,SIGNAL(clicked()),this,SLOT(ok_PushButton_Clicked()));
38     connect(cancelBtn,SIGNAL(clicked()),this,SLOT(cancel_PushButton_Clicked()));
39 }
1 //弹出小窗口关闭
2 void CloseDialog::closeEvent(QCloseEvent *e)
3 {
4     if (!m_flag)//若直接关闭对话框,m_flag置3
5         {
6         m_flag = 3;//保持原状态,不关闭
7     }
8 }
//确定按钮
1
void CloseDialog::ok_PushButton_Clicked() 2 { 3 if (hide->isChecked()) 4 { 5 m_flag = 1;//隐藏到后台 6 } 7 else if (holdOn->isChecked()) 8 { 9 m_flag = 2;//立即关闭 10 } 11 this->close(); 12 } 13 14 //取消按钮 15 void CloseDialog::cancel_PushButton_Clicked() 16 { 17 m_flag = 3;//保持原状态,不关闭 18 this->close(); 19 }
//.h文件
1
class CloseDialog : public QDialog 2 { 3 Q_OBJECT 4 public: 5 CloseDialog(); 6 ~CloseDialog(); 7 8 public: 9 QRadioButton *hide; 10 QRadioButton *holdOn; 11 QPushButton *okBtn; 12 QPushButton *cancelBtn; 13 QCheckBox *closeCbox; 14 int m_flag; //标志,记录隐藏窗口还是关闭 1隐藏到后台,2立即关闭,3不改变,0错误 15 16 protected: 17 void closeEvent(QCloseEvent *e);//窗口关闭时调用 18 public slots: 19 void ok_PushButton_Clicked(); 20 void cancel_PushButton_Clicked(); 21 };
主要就是 closeEvent(QCloseEvent *e)和ignore()函数的使用

界面关闭的事件响应,布布扣,bubuko.com

界面关闭的事件响应

标签:des   style   blog   http   color   使用   

原文地址:http://www.cnblogs.com/doubleeleven/p/3820842.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!