【发布时间】:2011-04-18 20:21:43
【问题描述】:
我的程序中有一个 QTableWidget,用于显示结果,我试图检测用户何时单击垂直标题之一。为此,我试图将信号 sectionDoubleClicked(int) 连接到我的函数 hheaderclicked(int)。当我尝试编译代码时,我得到一个关于没有匹配函数的编译时错误。我的代码基于 here 帖子中的代码
编译错误:
mainwindow.cpp:138: error: no matching function for call to âMainWindow::connect(QHeaderView*, const char [27], MainWindow* const, const char [21])â
/usr/lib64/qt4/include/QtCore/qobject.h:181: note: candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
/usr/lib64/qt4/include/QtCore/qobject.h:282: note: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
我的代码:
QObject::connect(ui->table_results->horizontalHeader(),SIGNAL(sectionDoubleClicked(int)),
this,SLOT(hheaderclicked(int)));
编辑: 通过执行以下操作,我能够让我的代码工作:
QObject::connect((QObject*)ui->table_results->verticalHeader(),SIGNAL(sectionClicked(int)),this,SLOT(hheaderclicked(int)));
有人可以解释为什么我必须将 QHeaderView* 转换为 QObject* 才能使其正常工作,我不必转换任何其他 QObject::connect 调用,它们都可以正常工作。例如这个工作正常:
QObject::connect(ui->button_start,SIGNAL(clicked()),this,SLOT(scanstart()));
是不是因为这个在编译时连接到一个已知的对象,而我的另一个连接到一个直到运行时才知道的对象?
【问题讨论】:
标签: c++ qt4 signals-slots qtablewidget