gtkmm statusicon 创建后退出

发布于 2024-09-16 06:11:21 字数 1316 浏览 2 评论 0原文

我必须创建一个简单的应用程序,在系统托盘中显示一个图标和一个菜单,您可以从中执行一些操作。 问题是 statusicon 在创建后立即关闭。缺少什么? 我放置了睡眠以确保它已创建。系统托盘中会出现 3 秒钟的内容,即使它不是我设置的图标。

初始化.cc

#include <gtkmm/main.h>
#include "Tray.h"

int main(int argc, char *argv[]) {
    Gtk::Main kit(argc, argv);
    printf("Statuicon starting\n");
    Tray tray;
    printf("Statuicon started\n");
    return 0;
}

托盘.cc

#include "Tray.h"

Tray::Tray() {
    set(Gtk::Stock::OK);

    signal_activate().connect(sigc::mem_fun(*this, &Tray::on_statusicon_activated));
    signal_popup_menu().connect(sigc::mem_fun(*this, &Tray::on_statusicon_popup));

    set_visible(true);

    printf("Statusicon created\n");

    sleep(3);
}

Tray::~Tray() {}

void Tray::on_statusicon_popup(guint button, guint activate_time) {
    printf("popup!");
}

void Tray::on_statusicon_activated() {
    printf("active!");
}

托盘.h

#ifndef GTKMM_TRAY_H
#define GTKMM_TRAY_H
#include <gtkmm.h>
#include <unistd.h>
using namespace std;

class Tray : public Gtk::StatusIcon {
    public:
        Tray();
        ~Tray();

    private:
        virtual void on_statusicon_popup(guint button, guint activate_time);
        virtual void on_statusicon_activated();
};

#endif //GTKMM_TRAY_H

I have to create a simple application that displays an icon in the systray and a menu from which you can do some operations.
the problem is that statusicon is closed immediately after creation. What's missing?
I put the sleep to make sure it was created. for 3 seconds something appears in systray, even if it is not the icon that I set.

Init.cc

#include <gtkmm/main.h>
#include "Tray.h"

int main(int argc, char *argv[]) {
    Gtk::Main kit(argc, argv);
    printf("Statuicon starting\n");
    Tray tray;
    printf("Statuicon started\n");
    return 0;
}

Tray.cc

#include "Tray.h"

Tray::Tray() {
    set(Gtk::Stock::OK);

    signal_activate().connect(sigc::mem_fun(*this, &Tray::on_statusicon_activated));
    signal_popup_menu().connect(sigc::mem_fun(*this, &Tray::on_statusicon_popup));

    set_visible(true);

    printf("Statusicon created\n");

    sleep(3);
}

Tray::~Tray() {}

void Tray::on_statusicon_popup(guint button, guint activate_time) {
    printf("popup!");
}

void Tray::on_statusicon_activated() {
    printf("active!");
}

Tray.h

#ifndef GTKMM_TRAY_H
#define GTKMM_TRAY_H
#include <gtkmm.h>
#include <unistd.h>
using namespace std;

class Tray : public Gtk::StatusIcon {
    public:
        Tray();
        ~Tray();

    private:
        virtual void on_statusicon_popup(guint button, guint activate_time);
        virtual void on_statusicon_activated();
};

#endif //GTKMM_TRAY_H

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌上芳菲 2024-09-23 06:11:21

您根本没有运行主循环,因此无法处理任何输入事件,并且程序在构建托盘后退出。您想要做的是删除睡眠,然后在 main() 函数中,在 return 之前添加以下行:

Gtk::Main::run();

然后,当您希望应用程序退出时(通常是响应某种事件),请调用

Gtk::Main::quit();

You're not running a main loop at all, so no input events can be handled and the program exits after constructing the tray. What you want to do is delete the sleep, and then in your main() function, add the following line right before the return:

Gtk::Main::run();

Then, when you want the application to quit (generally in response to an event of some sort), call

Gtk::Main::quit();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文