Qt/C++:QTableView上下文菜单和传递信息

发布于 2024-10-06 05:27:01 字数 908 浏览 0 评论 0原文

我想要 QTableView 中的单元格上有一个上下文菜单,所以首先我连接:

connect(ui->tblTimesheet,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(sheetContextMenu(const QPoint &)));

该连接的插槽如下:

void wndMyWindow::sheetContextMenu(const QPoint &pos){
    QMenu *menu = new QMenu;
    QModelIndex cell = ui->tblTimesheet->indexAt(pos);
    // Make sure the right click occured on a cell!
    if(cell.isValid()){
        QString myid=cell.sibling(cell.row(),0).data().toString();
        menu->addAction("Remove item", this, SLOT(sheetRemoveItem()));
        menu->exec(ui->tblTimesheet->mapToGlobal(pos));
    }
}

它创建菜单并在菜单中放置一个操作,在单击该操作时调用一个函数。但是,我想将变量 myid 传递到第二个槽中。该插槽列在下面:

void wndMyWindow::sheetRemoveItem(){
    qDebug("Sure I'm here, but what info do I have?");
    return;
}

我不太确定如何做到这一点,有人可以帮忙吗?

I want a contextmenu on the cells in my QTableView, so first I connected:

connect(ui->tblTimesheet,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(sheetContextMenu(const QPoint &)));

And the slot for that connect is below:

void wndMyWindow::sheetContextMenu(const QPoint &pos){
    QMenu *menu = new QMenu;
    QModelIndex cell = ui->tblTimesheet->indexAt(pos);
    // Make sure the right click occured on a cell!
    if(cell.isValid()){
        QString myid=cell.sibling(cell.row(),0).data().toString();
        menu->addAction("Remove item", this, SLOT(sheetRemoveItem()));
        menu->exec(ui->tblTimesheet->mapToGlobal(pos));
    }
}

Which creates the menu and puts an action in the menu which calls a function when that action is clicked. However, I want to pass through the variable myid into the second slot. That slot is listed below:

void wndMyWindow::sheetRemoveItem(){
    qDebug("Sure I'm here, but what info do I have?");
    return;
}

I'm not quite sure how to do this, can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情何以堪。 2024-10-13 05:27:01

在这方面,与回调和其他信号/槽机制相比,Qt 信号/槽很糟糕。您确实无法附加额外的信息。

不过,您可以做一些事情:

  1. 创建一个对象来存储您想要发送的信息,给它一个槽并附加到您想要响应的信号,发出带有信息的新信号,附加到该信号。

  2. 使用 Qt 信号映射事物将数据的一些微小变化附加到信号。

  3. 您可以在 Qt 插槽中获取发送者。这可能是您需要的信息。 (请参阅有关信号/槽的 Qt 文档)。

如果这些方法都不适合你,那么很遗憾,你就完蛋了。我正在研究一种自动创建 #1 并附加到 boost::signal 的方法,这会更强大,但它需要大量的艰苦工作,因为 Qt 与 C++ 预处理器和模板不兼容。

祝你好运。

In this regard, Qt signals/slots suck compared to callbacks and other signal/slot mechanisms. You really can't attach extra information.

Couple things you can do though:

  1. Create an object that stores the information you want to send, give it a slot and attach to the signal you want to respond to, emit a new signal with the information, attach to that signal.

  2. Use the Qt signal map thingy to attach some small variation of data to a signal.

  3. You can get the sender in a Qt slot. This may be the information you need. (see the Qt docs on signals/slots).

If none of those methods work for you, sorry to say but, you're pretty much fscked. I'm working on a method to auto-create #1 and attach to a boost::signal, which would be more powerful, but it's going to require a lot of hard work because Qt is incompatible with the C++ preprocessor and with templates.

Good luck.

缺⑴份安定 2024-10-13 05:27:01

阅读之前的答案后,我意识到您实际上可以将一些数据附加到发件人上。我在我的代码中这样做。

每个继承自 QObject 的对象都有“setProperty()”。您可以在开始发送信号之前在此处设置一些数据。在我的一个插槽中,我执行以下操作:

qobject_cast<QAction*>(sender())->property("index").toInt()

这允许我将索引附加到我的操作。

After reading the previous answer, I realized that you can actually attach some data on to the sender. I do it in my code.

Every objects that inherits from QObject has "setProperty()". Here you can set some data before it starts sending signals. In one of my slots I do the following:

qobject_cast<QAction*>(sender())->property("index").toInt()

This allows me to attach an index to my action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文