【问题标题】:Why doesn't Qt see my slot?为什么 Qt 看不到我的插槽?
【发布时间】:2013-12-25 07:50:06
【问题描述】:

我正在尝试使用以下代码将上下文菜单添加到树中:

void MainWindow::FileTreeContextMenu(const QPoint& pos)
{
    QPoint globalPos = ui->fileTree->viewport()->mapToGlobal(pos);
    QMenu menu;
    menu.addAction("New Group");

    QAction* selectedItem = menu.exec(globalPos);
}

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

    ui->fileTree->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->fileTree,
            SIGNAL(customContextMenuRequested(const QPoint&)),
            this,
            SLOT(FileTreeContextMenu(const QPoint&)));
}

但是,当我运行它时,我收到以下错误:

QObject::connect: No such slot MainWindow::FileTreeContextMenu(const QPoint&) 
  in ..\src\Fixer\mainwindow.cpp:23
QObject::connect:  (sender name:   'fileTree')
QObject::connect:  (receiver name: 'MainWindow')

我做错了什么?

【问题讨论】:

  • 您是否将该函数声明为类 MainWindow 中的插槽?
  • 请显示标题。您应该在那里有 Q_SLOT 或插槽。您也可能会错过标题中的 Q_OBJECT 宏...

标签: c++ qt qobject qtcore qt-signals


【解决方案1】:

我做错了什么?

如果您忽略将插槽声明为真实插槽,则可能会发生这种情况。您可以为此使用Q_SLOTS 宏,也可以只使用slots。在您的情况下,后者更合适,因为它只是一个应用程序和一个主窗口。

您还需要确保不要忘记在头文件中使用Q_OBJECT 宏。

【讨论】:

    猜你喜欢
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多