自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

QT 上下文菜單內(nèi)存泄露之QMainWindow

移動(dòng)開發(fā)
本文介紹的是QT 上下文菜單內(nèi)存泄露之QMainWindow,根據(jù)不同版本進(jìn)行測(cè)試,并解決問題。先來看內(nèi)容。

QT 上下文菜單內(nèi)存泄露之QMainWindow 是本人要介紹的內(nèi)容,先來看內(nèi)容。創(chuàng)建Qt工程,基于QMainwindow,什么也不做,程序會(huì)自帶一個(gè)上下文菜單。

不斷點(diǎn)擊鼠標(biāo)右鍵,菜單將反復(fù)出現(xiàn),此時(shí)我用任務(wù)管理器查看其內(nèi)存變化,發(fā)現(xiàn)每次不斷增加,請(qǐng)問大家這是Qt內(nèi)存泄漏嗎???我用MFC,CB均沒有發(fā)現(xiàn)類此錯(cuò)誤。

Qt 4.7.0 和 4.7.3下可以重現(xiàn)該問題,在Qt 4.6.3下不存在該問題??梢源_定是Qt的一個(gè)bug。

問題重現(xiàn)

在工具欄或??看翱谥悬c(diǎn)擊右鍵(彈出上下文菜單),多點(diǎn)擊幾次,然后點(diǎn)擊按鈕。觀察控制臺(tái)輸出,可以看到很多個(gè) QMenu 對(duì)象。

  1. #include <QtGui> 
  2. class MainWindow : public QMainWindow  
  3. {  
  4.     Q_OBJECT  
  5. public:  
  6.     explicit MainWindow(QWidget *parent = 0);  
  7. private slots:  
  8.     void onButtonClicked();  
  9. };  
  10. MainWindow::MainWindow(QWidget *parent)  
  11. {  
  12.     addToolBar("ToolBar");  
  13.     addDockWidget(Qt::LeftDockWidgetArea, new QDockWidget("DockWidget"));  
  14.     QPushButton * btn = new QPushButton("dump object tree");  
  15.     setCentralWidget(btn);  
  16.     connect(btn, SIGNAL(clicked()), SLOT(onButtonClicked()));  
  17. }  
  18. void MainWindow::onButtonClicked()  
  19. {  
  20.     dumpObjectTree();  
  21. }  
  22. #include "main.moc"  
  23. int main(int argc, char *argv[])  
  24. {  
  25.     QApplication a(argc, argv);  
  26.     MainWindow w;  
  27.     w.show();  
  28.  
  29.     return a.exec();  

原因

既然是QMainWindow的上下文菜單問題,直接看 contextMenuEvent 事件處理函數(shù)吧。

  1. void QMainWindow::contextMenuEvent(QContextMenuEvent *event)  
  2. {  
  3.     event->ignore();  
  4. ...  
  5.     QMenu *popup = createPopupMenu();  
  6.     if (popup) {  
  7.         if (!popup->isEmpty()) {  
  8.             popup->setAttribute(Qt::WA_DeleteOnClose);  
  9.             popup->popup(event->globalPos());  
  10.             event->accept();  
  11.         } else {  
  12.             delete popup;  
  13.         }  
  14.     }  

看仔細(xì)嘍,這兒設(shè)置了 Qt::WA_DeleteOnClose 屬性。

有什么用?設(shè)置該屬性后,當(dāng)我們調(diào)用該對(duì)象的 close() 成員時(shí),隱藏(hide)窗口同時(shí)會(huì)刪除(delete)該對(duì)象

有什么問題?問題出在,實(shí)際上隱藏菜單時(shí)沒有 調(diào)用菜單的close(),而是 調(diào)用的hide()的成員。

調(diào)用hide()而不是close(),是的該屬性不能發(fā)揮任何作用,進(jìn)而導(dǎo)致內(nèi)存泄露(Qt 之 show,hide,setVisible,setHidden,close 等小結(jié) )。

為了對(duì)比,我們看看Qt4.6.3的源碼部分:

  1. void QMainWindow::contextMenuEvent(QContextMenuEvent *event)  
  2. {  
  3.     event->ignore();  
  4. ...  
  5.     QMenu *popup = createPopupMenu();  
  6.     if (popup && !popup->isEmpty()) {  
  7.         popup->exec(event->globalPos());  
  8.         event->accept();  
  9.     }  
  10.     delete popup;  

而這個(gè),也就是我們的比較理想的答案了。

進(jìn)一步學(xué)習(xí)

前面說了,菜單隱藏時(shí)調(diào)用的是hide() 成員,而不是close() 成員。有神馬依據(jù)??

想想?如何讓菜單隱藏

鼠標(biāo):點(diǎn)擊菜單外區(qū)域

鍵盤:按下Esc鍵等

這樣就比較明朗了,對(duì)吧,直接看這兩個(gè)事件處理函數(shù)

鍵盤的按鍵事件(調(diào)用了hideMenu)

  1. void QMenu::keyPressEvent(QKeyEvent *e)  
  2. {  
  3.     Q_D(QMenu);  
  4.     d->updateActionRects();  
  5.     int key = e->key();  
  6. ...  
  7.     bool key_consumed = false;  
  8.     switch(key) {  
  9.     case Qt::Key_Escape:  
  10.         key_consumed = true;  
  11.         {  
  12.             QPointer<QWidget> caused = d->causedPopup.widget;  
  13.             d->hideMenu(this); // hide after getting causedPopup  
  14.             if (QMenuBar *mb = qobject_cast<QMenuBar*>(caused)) {  
  15.                 mb->d_func()->setCurrentAction(d->menuAction);  
  16.                 mb->d_func()->setKeyboardMode(true);  
  17.             }  
  18.         }  
  19.         break;鼠標(biāo)在菜單區(qū)域外按鍵,調(diào)用了hideUpToMenuBar(進(jìn)而調(diào)用hideMenu)   
  20. void QMenu::mousePressEvent(QMouseEvent *e)  
  21. {  
  22.     Q_D(QMenu);  
  23. ...  
  24.     if (!rect().contains(e->pos())) {  
  25.          if (d->noReplayFor  
  26.              && QRect(d->noReplayFor->mapToGlobal(QPoint()), d->noReplayFor->size()).contains(e->globalPos()))  
  27.              setAttribute(Qt::WA_NoMouseReplay);  
  28.          if (d->eventLoop) // synchronous operation  
  29.              d->syncAction = 0;  
  30.         d->hideUpToMenuBar();  
  31.         return;  
  32.     }  

前面都調(diào)用了hideMenu,從名字也能猜猜它想干什么:

  1. void QMenuPrivate::hideMenu(QMenu *menu, bool justRegister)  
  2. {  
  3. ...  
  4.         menu->hide();  

小結(jié):QT 上下文菜單內(nèi)存泄露QMainWindow 的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2012-08-01 09:58:12

Mountain Li操作系統(tǒng)

2017-05-11 14:00:02

Flask請(qǐng)求上下文應(yīng)用上下文

2012-12-31 10:01:34

SELinuxSELinux安全

2022-09-14 13:13:51

JavaScript上下文

2018-06-17 08:38:17

微軟Windows應(yīng)用程序

2022-09-15 08:01:14

繼承基礎(chǔ)設(shè)施基礎(chǔ)服務(wù)

2023-07-11 10:02:23

2024-09-30 14:10:00

2025-03-18 08:14:05

2017-12-17 17:01:23

限界上下文系統(tǒng)模型

2022-10-28 16:24:33

Context上下文鴻蒙

2020-07-24 10:00:00

JavaScript執(zhí)行上下文前端

2021-07-26 07:47:36

Cpu上下文進(jìn)程

2021-05-09 21:50:48

項(xiàng)目實(shí)踐上下文

2022-10-31 15:34:30

python裝飾器內(nèi)存泄漏

2021-07-20 19:30:05

微軟Windows 11Windows

2010-02-25 17:04:54

WCF實(shí)例上下文

2019-05-06 14:36:48

CPULinux寄存器

2022-04-24 15:37:26

LinuxCPU

2025-04-07 01:02:00

GoAPI語言
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)