QSignalMapper和原始Sender() [英] QSignalMapper and original Sender()

查看:601
本文介绍了QSignalMapper和原始Sender()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表中有一堆 QComboBox es。因此,我知道哪个触发了我重新映射信号来编码表格单元格位置(如选择QComboBox在QTableWidget



(为什么Qt不只是发送单元格激活信号,所以你可以使用相同的当前行/列机制,编辑我不知道。)



但这删除了原始发件人小部件的所有知识。在插槽中调用 QComboBox * combo =(QComboBox *)sender()失败,可能是因为 sender() QSignalMapper



我可以使用编码的行/列来查找 QComboBox 在表窗口小部件,但似乎错了。有更正确的方法吗?



例如:

  // in table creator 
_signalMapper = new QSignalMapper(this);

//每个单元格
QComboBox * combo = new QComboBox();
connect(combo,SIGNAL(currentIndexChanged(int)),_signalMapper,SLOT(map()));
_signalMapper-> setMapping(combo,row);

//最后
connect(_signalMapper,SIGNAL(mapped(int)),this,SLOT(changedType(int)));

// slot
void myDlg :: changedType(int row)
{
QComboBox * combo =(QComboBox *)sender //这不工作!
}

EDIT :添加为将来搜索:

解决方案

为什么不将QComboBox的信号直接连接到您的插槽?

  QComboBox * combo = ... 
connect(combo,SIGNAL(currentIndexChanged(int)),this, (changedType(int)));

然后在您的插槽中,您可以使用sender()方法来检索已更改的QComboBox。

  void myDlg :: changedType(int row)
{
QComboBox * combo = qobject_cast< QComboBox * > sender();

if(combo!= 0){
//其余代码
}
}

或者,要使用QSignalMapper方法,您只需要更改插槽以使用您设置的映射:

  void myDlg :: changedType(int row)
{
QComboBox * combo = qobject_cast< QComboBox *>(_ signalMapper-> mapping

if(combo!= 0){
//其余代码
}
}


I have a bunch of QComboBoxes in a table. So that I know which one was triggered I remap the signal to encode the table cell location (as described in Selecting QComboBox in QTableWidget)

(Why Qt doesn't just send the cell activated signal first so you can use the same current row/column mechanism as any other cell edit I don't know.)

But this removes all knowledge of the original sender widget. Calling QComboBox* combo = (QComboBox* )sender() in the slot fails, presumably because sender() is now the QSignalMapper.

I can use the encoded row/column to lookup the QComboBox in the table widget but that seems wrong. Is there a more correct way to do it?

e.g.:

// in table creator
_signalMapper = new QSignalMapper(this);

 // for each cell
    QComboBox* combo = new QComboBox();
    connect(combo, SIGNAL(currentIndexChanged(int)), _signalMapper, SLOT(map()));
    _signalMapper->setMapping(combo, row);

   // and finally       
   connect(_signalMapper, SIGNAL(mapped(int)),this, SLOT(changedType(int)));

 // slot
 void myDlg::changedType(int row)
 {      
        QComboBox* combo = (QComboBox* )sender(); // this doesn't work !!
 }

EDIT: Added for future search: there is a new book "Advanced Qt Programming" by Mark Summerfield that explains how to do this sort of thing.

解决方案

Why not connect the QComboBox's signal straight to your slot?

QComboBox *combo = ...
connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(changedType(int)));

And then in your slot you can use the sender() method to retrieve the QComboBox that was changed.

void myDlg::changedType(int row)
{
    QComboBox *combo = qobject_cast<QComboBox *> sender();

    if(combo != 0){
        // rest of code
    }
}

Alternatively, to use the QSignalMapper method you would just need to change your slot to use the mapping you set up:

void myDlg::changedType(int row)
{
    QComboBox *combo = qobject_cast<QComboBox *>(_signalMapper->mapping(row));

    if(combo != 0){
        // rest of code
    }
}

这篇关于QSignalMapper和原始Sender()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆