Qt/C++:QTableView上下文菜单和传递信息
我想要 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这方面,与回调和其他信号/槽机制相比,Qt 信号/槽很糟糕。您确实无法附加额外的信息。
不过,您可以做一些事情:
创建一个对象来存储您想要发送的信息,给它一个槽并附加到您想要响应的信号,发出带有信息的新信号,附加到该信号。
使用 Qt 信号映射事物将数据的一些微小变化附加到信号。
您可以在 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:
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.
Use the Qt signal map thingy to attach some small variation of data to a signal.
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.
阅读之前的答案后,我意识到您实际上可以将一些数据附加到发件人上。我在我的代码中这样做。
每个继承自 QObject 的对象都有“setProperty()”。您可以在开始发送信号之前在此处设置一些数据。在我的一个插槽中,我执行以下操作:
这允许我将索引附加到我的操作。
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:
This allows me to attach an index to my action.