开发者

Problems with QStatusBar->showMessage()

开发者 https://www.devze.com 2023-02-15 00:40 出处:网络
I\'m writing a Qt GUI Application, but there\'s a strange error i can\'t figure out; Here\'s the whole code:

I'm writing a Qt GUI Application, but there's a strange error i can't figure out; Here's the whole code: main.cpp

#include "LevelEditor.h"
int main(int argc, char* argv[])
{
    LevelEditor editor(argc, argv);
    editor.go();
    return 0;
}

LevelEditor.h

#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

#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();
}

This code compiles fine without errors. Anyway, the debug console gives me a warning QObject::connect : NO such slot &QStatusBar::showMessage("Quit 开发者_开发百科this App")

The problem seems related to this line:

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

I've searched in "QStatusBar.h" for the showMessage function and it is declared, but can't be called neither with "." nor "->" (even if it's public). Also tried this: QObject::connect(file_actions.back(), SIGNAL(hovered()), status_bar, SLOT(showMessage("Quit this App", 0));

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

But to no avail, it just won't recognise the function.

Am i missing something here? Thanks in advance.

EDIT: Solved, i was taking the hard way to show a status text instead of using QAction::setStatusTip, my bad.


You can't connect a signal to a slot with different signature. And you are not even using the proper connect syntax. The SLOT part should be:

SLOT(showMessage(const QString &))

It's to tell the meta object system what type(s) of parameters to send to the slot, not what concrete data to send.

In your case, you can't connect a signal with no parameter to a slot that expects one. You can achieve that by connecting the signal to your own slot and then call QStatusBar::showMessage from there.


You could use QSignalMapper to do what you want:

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 &));

Using QSignalMapper allows you, to simply add another "hovered" messages without creating new slots for each. Simply for all other cases just use:

mapper->setMapping(yourAction/Button/Whater, "Your status message");
QObject::connect(yourAction/Button/Whater, SIGNAL(hovered/Other signal()), mapper, SLOT(map()))
0

精彩评论

暂无评论...
验证码 换一张
取 消