【问题标题】:Signals not being detected for QTabWidget未检测到 QTabWidget 的信号
【发布时间】:2012-04-02 13:38:00
【问题描述】:

我的 QTabWidget 实现没有检测到它的 tabCloseRequested()currentChanged() 信号。

TileSheetManager::TileSheetManager(QWidget *parent)
: QTabWidget(parent)
{
    int w = WIDTH;
    int h = HEIGHT;

    this->setMinimumSize(w, h);
    this->setMaximumSize(w, h);

    setTabsClosable(true);
    setTabShape(QTabWidget::Rounded);

    connect(this, SIGNAL(tabCloseRequested(int index)), this, SLOT(closeTileWidget(int index)));
    connect(this, SIGNAL(currentChanged(int index)), this, SLOT(tabChanged(int index)));
}

qDebug() 不适合我,所以我为此使用 QMessageBox

void TileSheetManager::closeTileWidget(int index)
{
   QMessageBox msgBox;
   msgBox.setText("Tab " + QString::number(index) + " removed!");
   msgBox.exec();

   TileWidget *t = (TileWidget *) widget(index) ;
   t->deleteLater();
   removeTab(index);
}

void TileSheetManager::tabChanged(int index)
{   
    QMessageBox msgBox;
    msgBox.setText("Tab was Changed!");
    msgBox.exec();

    TileWidget *t;

    for(int i = 0; i < count(); i++)
    {
        t = (TileWidget *) widget(i) ;
        t->resetSetBrush();
    }
} 

选项卡没有关闭,选定的画笔没有被重置,我也没有收到任何消息,所以我得出的结论是信号没有被拾取。这很奇怪,因为我在以前的项目中使用过类似的代码,在这种情况下它可以工作。

【问题讨论】:

    标签: c++ qt qtabwidget


    【解决方案1】:

    不要connect 函数中使用变量名:

    注意signal和slots参数不能包含 任何变量名,只有类型。

    连接应该是

    connect(this, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTileWidget(int)));
    connect(this, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int)));
    

    【讨论】:

    • 谢谢!有效。不过,我的另一个信号槽连接(使用 slot 参数中的变量名)正在工作。你知道为什么吗?再次感谢。
    • 我有同样的问题,我在连接中没有变量名,仍然没有接收到信号......有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多