3

正如标题所说,如果我制作了一个系统托盘图标,该图标可以选择通过那里打开另一个对话框(例如首选项),当我关闭另一个对话框时,当我调用 this>close(); 时整个应用程序都会关闭;使用该首选项对话框。拿这个示例代码:main.cpp:

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

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

    return a.exec();
}

主窗口.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();
}

主窗口.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

首选项.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();
}

首选项.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

图标.qrc:

错误.png

文件error.png在这里:http: //i.imgur.com/beSvX.png

将上述所有文件保存到同一目录并编译为:

qmake -project
qmake *.pro
qmake
make

谢谢你的帮助!

4

1 回答 1

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();
}
于 2012-07-06T08:35:20.467 回答