2012-07-05 76 views
3

正如標題所說,如果我使系統托盤圖標有一個選項來打開其他對話框(例如,首選項),那麼當關閉此對話框時,整個應用程序當我打電話給 this> close();從這個偏好對話框。 藉此示例代碼: main.cpp中:QSystemTrayIcon,打開除主窗口之外的其他對話框關閉應用程序

#include <QtGui/QApplication> 
#include "mainwindow.h" 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    MainWindow w; 
    w.show(); 

    return a.exec(); 
} 

mainwindow.cpp

#include "mainwindow.h" 
#include "ui_mainwindow.h" 


MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    trayIcon = new QSystemTrayIcon(this); 
    trayIcon->setIcon(QIcon(":/icons/error.png")); 
    //replace 'error' with 'video' and recompile. The indicator isn't shown! 
    trayIcon->setToolTip("Test"); 
    QMenu *changer_menu = new QMenu; 
    Show_action = new QAction(tr("S&how"),this); 
    Show_action->setIconVisibleInMenu(true); 
    connect(Show_action, SIGNAL(triggered()), this, SLOT(show_me())); 
    changer_menu->addAction(Show_action); 
    changer_menu->addSeparator(); 

    Preferences_action = new QAction(tr("Preferences"), this); 
    Preferences_action->setIconVisibleInMenu(true); 
    connect(Preferences_action, SIGNAL(triggered()), this, SLOT(showpref())); 
    changer_menu->addAction(Preferences_action); 

    Quit_action = new QAction(tr("&Quit"), this); 
    Quit_action->setIconVisibleInMenu(true); 
    connect(Quit_action, SIGNAL(triggered()), this, SLOT(quit_me())); 
    changer_menu->addAction(Quit_action); 
    trayIcon->setContextMenu(changer_menu); 
} 

void MainWindow::showpref(){ 
    pref=new Preferences(this); 
    pref->exec(); 
} 

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

void MainWindow::on_pushButton_clicked() 
{ 
    trayIcon->show(); 
    this->hide(); 
} 

void MainWindow::show_me(){ 
    this->show(); 
} 

void MainWindow::quit_me(){ 
    this->close(); 
} 

mainwindow.h:

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 

#include <QMainWindow> 
#include <QSystemTrayIcon> 

#include "preferences.h" 

namespace Ui { 
class MainWindow; 
} 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    explicit MainWindow(QWidget *parent = 0); 
    ~MainWindow(); 

private slots: 
    void on_pushButton_clicked(); 
    void show_me(); 
    void quit_me(); 
    void showpref(); 

private: 
    Ui::MainWindow *ui; 
    QSystemTrayIcon *trayIcon; 
    QAction *Show_action; 
    QAction *Preferences_action; 
    QAction *Quit_action; 
    Preferences *pref; 
}; 

#endif // MAINWINDOW_H 

preferences.cpp:

#include "preferences.h" 
#include "ui_preferences.h" 

Preferences::Preferences(QWidget *parent) : 
    QDialog(parent), 
    ui(new Ui::Preferences) 
{ 
    ui->setupUi(this); 
} 

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

void Preferences::on_pushButton_clicked() 
{ 
    /HERE THE WHOLE PROGRAM CLOSES. I WANT ONLY THE PREFERENCES DIALOG TO CLOSE, THE INDICATOR TO STAY 
    close(); 
} 

preferences.h:

#ifndef PREFERENCES_H 
#define PREFERENCES_H 

#include <QDialog> 

namespace Ui { 
class Preferences; 
} 

class Preferences : public QDialog 
{ 
    Q_OBJECT 

public: 
    explicit Preferences(QWidget *parent = 0); 
    ~Preferences(); 

private slots: 
    void on_pushButton_clicked(); 

private: 
    Ui::Preferences *ui; 
}; 

#endif // PREFERENCES_H 

icons.qrc:

error.png

文件error.png這裏: http://i.imgur.com/beSvX.png

上述所有文件保留到相同的目錄並編譯爲:

qmake -project 
qmake *.pro 
qmake 
make 

感謝您的幫助!

+0

它關閉或崩潰?你是否通過調試器來運行它? – cmannett85 2012-07-06 07:00:32

回答

5

做一個小測試,打開主窗口,不要關閉它。打開首選項窗口並關閉它。你的應用程序不應該以這種方式退出。現在關閉主窗口,應用程序將退出。發生這種情況是因爲QApplication屬性「quitOnLastWindowClosed」默認設置爲true。您應該致電

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    a.setQuitOnLastWindowClosed(false); 
    MainWindow w; 
    w.show(); 

    return a.exec(); 
} 

另請注意,您每次需要顯示首選項時都會從MainWindow泄漏內存,同時創建首選項的新實例。你可以這樣做:

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow), 
    pref(NULL) 
{ 
    // snap, your code goes here 
} 

void MainWindow::showpref(){ 
    if(! pref) 
     pref=new Preferences(this); 

    pref->exec(); 
} 
+0

但用戶沒有關閉主窗口 - 它被隱藏。 – cmannett85 2012-07-06 08:45:44

+0

它沒有被顯示,所以沒有窗口左(可見),所以這是最後一個窗口關閉 – 2012-07-06 08:46:48

+0

但是,如果這是真的,不會只是最小化窗口關閉它? – cmannett85 2012-07-06 09:01:33

相關問題