我正在编写一个 Qt GUI 应用程序,但有一个我无法弄清楚的奇怪错误; 这是整个代码: main.cpp
#include "LevelEditor.h"
int main(int argc, char* argv[])
{
LevelEditor editor(argc, argv);
editor.go();
return 0;
}
关卡编辑器.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
关卡编辑器.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();
}
这段代码编译良好,没有错误。 无论如何,调试控制台给了我一个警告 QObject::connect :没有这样的槽 &QStatusBar::showMessage("退出此应用程序")
问题似乎与此行有关:
QObject::connect(file_actions.back(), SIGNAL(hovered()), status_bar, SLOT(showMessage("退出此应用程序"));
我在“QStatusBar.h”中搜索了 showMessage 函数,它已被声明,但不能用“.”来调用。也不是“->”(即使它是公开的)。还尝试过这个:
QObject::connect(file_actions.back(), SIGNAL(hovered()), status_bar, SLOT(showMessage("退出此应用程序", 0));
还有这个:
QObject::connect(file_actions.back(), SIGNAL(hovered()), status_bar, SLOT(QStatusBar::showMessage("退出此应用程序"));
但没有用,它只是无法识别该功能。
我在这里错过了什么吗? 提前致谢。
编辑:解决了,我采取了困难的方式来显示状态文本,而不是使用 QAction::setStatusTip,我的错。
最佳答案
您无法将信号连接到具有不同签名的插槽。而且您甚至没有使用正确的连接语法。 SLOT 部分应该是:
SLOT(showMessage(const QString &))
它是告诉元对象系统将什么类型参数发送到槽,而不是发送什么具体数据。
在您的情况下,您无法将没有参数的信号连接到需要参数的插槽。您可以通过将信号连接到您自己的插槽,然后从那里调用 QStatusBar::showMessage 来实现这一点。
关于c++ - QStatusBar->showMessage() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5195489/