【发布时间】: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