首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QStatusBar->showMessage()的问题

QStatusBar->showMessage()的问题
EN

Stack Overflow用户
提问于 2011-03-04 15:10:18
回答 2查看 2.2K关注 0票数 2

我正在编写一个Qt应用程序,但是有一个奇怪的错误我搞不清楚;下面是完整的代码: main.cpp

代码语言:javascript
复制
#include "LevelEditor.h"
int main(int argc, char* argv[])
{
    LevelEditor editor(argc, argv);
    editor.go();
    return 0;
}

LevelEditor.h

代码语言:javascript
复制
#ifndef LEVELEDITOR_H
#define LEVELEDITOR_H

#include <QtGui>

class LevelEditor
{
    public:
        LevelEditor(int argc, char* argv[]);
        ~LevelEditor();
        void go();
    protected:
        QApplication* app;
        QMainWindow* main_window;
        QMenuBar* menu_bar;
        QStatusBar* status_bar;
        QWidget* central;
        QMenu* menu_entry[3];
        QFrame* about_frame;

        QList<QAction*> file_actions;
        QList<QAction*> edit_actions;
        QList<QAction*> help_actions;
    private:
};

#endif // LEVELEDITOR_H

LevelEditor.cpp

代码语言:javascript
复制
#include "LevelEditor.h"
#include <QStatusBar>
LevelEditor::LevelEditor(int argc, char* argv[])
{
    //initialise main objects
    app = new QApplication(argc, argv);
    main_window = new QMainWindow();
    menu_bar = main_window->menuBar();
    status_bar = main_window->statusBar();
    central = main_window->centralWidget();
    about_frame = new QFrame();

    //initialise menu entries and actions
    menu_entry[0] = new QMenu();    //file
    menu_entry[1] = new QMenu();    //edit
    menu_entry[2] = new QMenu();    //about

    //creating and connecting events to action
    menu_entry[0]->setTitle("File");
    file_actions.append(new QAction("New", menu_entry[0]));
    file_actions.append(new QAction("Open", menu_entry[0]));
    file_actions.append(new QAction("Save", menu_entry[0]));
    file_actions.append(new QAction("Quit", menu_entry[0]));
    QObject::connect(file_actions.back(), SIGNAL(triggered()), app, SLOT(quit()));
    QObject::connect(file_actions.back(), SIGNAL(hovered()), status_bar, SLOT(showMessage("Quit this App"));
    menu_entry[0]->addActions(file_actions);
    menu_bar->addMenu(menu_entry[0]);

    //edit menu
    menu_entry[1]->setTitle("Edit");
    edit_actions.append(new QAction("Cut", menu_entry[1]));
    edit_actions.append(new QAction("Copy", menu_entry[1]));
    edit_actions.append(new QAction("Paste", menu_entry[1]));
    menu_entry[1]->addActions(edit_actions);
    menu_bar->addMenu(menu_entry[1]);

    //help menu
    help_actions.append(new QAction("About", menu_entry[2]));
    QObject::connect(help_actions.back(), SIGNAL(triggered()), about_frame, SLOT(show()));
    menu_entry[2]->setTitle("Help");
    menu_entry[2]->addActions(help_actions);
    menu_bar->addMenu(menu_entry[2]);

    about_frame->resize(400,300);
}

LevelEditor::~LevelEditor()
{
    //dtor
}

void LevelEditor::go()
{
    //nothing
    main_window->showMaximized();
    menu_bar->show();
    status_bar->show();
    app->exec();
}

这段代码编译得很好,没有错误。无论如何,调试控制台给我一个警告: QObject::connect : NO这样的插槽&QStatusBar::showMessage(“退出此应用程序”)

这个问题似乎与这条线有关:

QObject::connect(file_actions.back(), SIGNAL(hovered()), status_bar, SLOT(showMessage("Quit this App"));

我在"QStatusBar.h“中搜索了showMessage函数,并声明了它,但不能用”with“调用。也不是"->“(即使它是公开的)。也尝试过这个:QObject::connect(file_actions.back(), SIGNAL(hovered()), status_bar, SLOT(showMessage("Quit this App", 0));

这个:QObject::connect(file_actions.back(), SIGNAL(hovered()), status_bar, SLOT(QStatusBar::showMessage("Quit this App"));

但没有用,它就是认不出它的功能。

我是不是漏掉了什么?提前谢谢。

编辑:解决了,我采取艰难的方式显示一个状态文本,而不是使用QAction::setStatusTip,我的错。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-04 15:20:00

您不能将信号连接到具有不同签名的时隙。您甚至没有使用正确的连接语法。插槽部分应是:

代码语言:javascript
复制
SLOT(showMessage(const QString &))

它是告诉元对象系统要发送到插槽的参数的类型(S),而不是发送什么具体数据。

在您的情况下,您无法将没有参数的信号连接到预期的时隙。您可以通过将信号连接到自己的时隙,然后从那里调用QStatusBar::showMessage来实现这一点。

票数 3
EN

Stack Overflow用户

发布于 2011-03-09 11:34:48

您可以使用QSignalMapper来做您想做的事情:

代码语言:javascript
复制
QSignalMapper * mapper = new QSignalMapper(this);
QObject::connect(file_actions.back(), SIGNAL(hovered()), mapper, SLOT(map()));
mapper->setMapping(file_actions.back(), "Quit this app");
connect(mapper, SIGNAL(mapped(const QString &)), statusBar, SLOT(showMessage(const QString &));

使用QSignalMapper可以简单地添加另一个“悬停”消息,而无需为每个消息创建新的插槽。对于所有其他情况,只需使用:

代码语言:javascript
复制
mapper->setMapping(yourAction/Button/Whater, "Your status message");
QObject::connect(yourAction/Button/Whater, SIGNAL(hovered/Other signal()), mapper, SLOT(map()))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5195489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档