gpt4 book ai didi

qt - QSystemTrayIcon,打开除主窗口以外的其他对话框关闭应用程序

转载 作者:行者123 更新时间:2023-12-04 13:09:01 28 4
gpt4 key购买 nike

正如标题所说,如果我制作一个系统托盘图标,它可以选择通过那里打开另一个对话框(例如首选项),当我关闭另一个对话框时,整个应用程序将在我调用时关闭这个>关闭();从那个首选项对话框。拿这个例子代码: 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

感谢您的帮助!

最佳答案

做个小测试,打开主窗口,不要关闭它。打开首选项窗口并将其关闭。您的应用程序不应该以这种方式退出。现在关闭主窗口,应用程序将退出。发生这种情况是因为 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();
}

关于qt - QSystemTrayIcon,打开除主窗口以外的其他对话框关闭应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11350718/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com