【问题标题】:detecting header click in QTableWidget c++ qt4在QTableWidget c ++ qt4中检测标题点击
【发布时间】: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


    【解决方案1】:

    错误消息将第三个参数指定为MainWindow* const,这很奇怪。如果是const MainWindow*,调用应该可以工作。调用函数是如何声明的?

    【讨论】:

    • 连接发生在主窗口的构建过程中,MainWindow::MainWindow(QWidget *parent)。你问的是这个吗?
    • 鉴于您的编辑,您是否可能忘记了#include
    • 我尝试根据您的建议包含 并且程序现在可以编译而无需转换为 QObject*。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    相关资源
    最近更新 更多