简体   繁体   English

在QT C ++中的QSystemTrayIcon附近显示QWidget或QWindow

[英]Show QWidget or QWindow near QSystemTrayIcon in QT C++

I have managed to get the QSystemTrayIcon visible similar to this: 我设法让QSystemTrayIcon看起来像这样:

假设VMWare图标是我的QSystemTrayIcon

using the following line of code (with the signal slots working): 使用以下代码行(信号槽工作):

#include "dialog.h"
#include "ui_dialog.h"
#include <QMessageBox>
#include <form.h>

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

    QIcon icon("/Users/JohnnyAppleseed/IMAGE.png");
    m_ptrTrayIcon = new QSystemTrayIcon(icon );
    m_ptrTrayIcon->setToolTip( tr( "Bubble Message" ) );
   // m_ptrTrayIcon->setContextMenu(m_trayIconMenu);
    connect(m_ptrTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
                 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}

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

However, when I try to implement code to show the QWidget/QWindow near the QSystemTrayIcon that I have created, it fails to show up near it. 但是,当我尝试实现代码以显示我创建的QSystemTrayIcon附近的QWidget / QWindow时,它无法显示在它附近。 It also shows up and disappears quickly as well (even if I didn't want it near the QSystemTrayIcon) using this code: 它也显示并快速消失(即使我不希望它靠近QSystemTrayIcon)使用此代码:

void Dialog::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
    form fr;
    fr.setWindowFlags(Qt::Popup);

    fr.show();
}

For the sake of being clear, I would like to show my QWidget/QWindow just like VMWare Fusion's approach (or the clock that is found on Microsoft Windows Vista or later... ) 为了清楚起见,我想展示我的QWidget / QWindow就像VMWare Fusion的方法(或者在Microsoft Windows Vista或更高版本中找到的时钟......

Mac OS X / Linux Mac OS X / Linux 没有说明

Microsoft Windows 微软Windows 在此输入图像描述

Can some one please point out what am I doing wrong? 有人可以指出我做错了什么吗? Thanks! 谢谢!

To make things much simpler, download the project: http://zipshare.net/sv 为了使事情更简单,请下载项目:http: //zipshare.net/sv

UPDATE #1 更新#1

Regarding the QWidget/QWindow flicking issue, vahancho advised me to move the form fr; 关于QWidget / QWindow轻弹问题, vahancho建议我移动form fr; from the void Dialog::iconActivated(QSystemTrayIcon::ActivationReason reason) function to the header of the working window. void Dialog::iconActivated(QSystemTrayIcon::ActivationReason reason)函数到工作窗口的标题。 And it worked successfully all thanks to vahancho . 它成功地完成了所有感谢vahancho The window now shows up, but its not near the QSystemTrayIcon yet :( 窗口现在显示,但它还没有接近QSystemTrayIcon :(

The problem is that you create you form object in the stack and it gets deleted as soon as the execution goes out of you iconActivated() slot. 问题是你在堆栈中创建了表单对象,一旦执行超出你的iconActivated()插槽就会被删除。 That is why it disappears as soon as you see it. 这就是为什么它一看到它就会消失的原因。 To solve the problem you need to create your pop up in the heap. 要解决此问题,您需要在堆中创建弹出窗口。

UPDATE UPDATE

In order to place you dialog near the tray icon you have to determine the tray icon position. 为了将对话框放在托盘图标附近,您必须确定托盘图标位置。 To do that you can use QSystemTrayIcon::geometry() function. 为此,您可以使用QSystemTrayIcon :: geometry()函数。 You code will look like (adjust the coordinates according to your needs): 您的代码看起来像(根据您的需要调整坐标):

QRect rect = m_ptrTrayIcon->geometry();
fr.move(rect.x(), rect.y());
fr.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM