Qt 中连接的自定义上下文菜单

问题描述 投票:0回答:1

我在

QListWidget
中连接自定义菜单时遇到问题,connect 函数返回 false。这是代码:

我有一些名为

MainWindow
的主窗口类。在它的构造函数中我有这一行

connect(ui->notesWidget, SIGNAL(customContextMenuRequested(QPoint &)), 
        this, SLOT(contextMenuforNotesArea(QPoint &)));

其中提到

notesWidget
QListWidget

ContextMenuforNotesArea(QPoint &)
定义如下

class MainWindow : public QMainWindow
{
public slots:
    void contextMenuforNotesArea(QPoint &pos);
};

void MainWindow::contextMenuforNotesArea(const QPoint &pos){
    QMessageBox::information(this, "a", "the function has been finally called");
    QMenu contextMenu(tr("Context menu"), this);
    contextMenu.addAction(new QAction(tr("Hello"), this));
    contextMenu.exec(mapToGlobal(pos));
}

我还通过表单设计器将

contextMenu
中的属性
listWidget
更改为
customContextMenu

qt contextmenu
1个回答
0
投票

在头文件中按如下方式更改 SLOT 签名

void contextMenuforNotesArea(const QPoint &pos);

还要确保,当您在构造函数中进行连接时,是在调用

setupUi

之后进行的
© www.soinside.com 2019 - 2024. All rights reserved.