【问题标题】:Whats wrong with this QT Signal in my PopUp?我的 PopUp 中的这个 QT 信号有什么问题?
【发布时间】:2012-12-13 11:10:55
【问题描述】:

我实现了一个颜色选择器菜单,基于BlackDal ColorPicker

In order to send a signal, when a color has been selected, i added:

class RColorPicker : public QPushButton
{
    Q_OBJECT
signals:
   void selected( QColor color);
 ...

void RColorPicker::on_popup_selected( QColor color )
{
   _selectedColor = color;
   repaint();
   emit selected( color );

我在其他地方连接到该信号,例如

fillColorButton     = new RColorPicker()
connect(fillColorButton    , SIGNAL(selected(QColor)), this,  SLOT(fillColorButtonTriggered(QColor)) );

当从 PopUpMenu 中选择一种颜色时,此方法有效,但如果我在 PopUp 中选择“更多...”,然后从调用的 QColorDialog 中选择一种颜色,则不会调用连接的插槽 fillColorButtonTriggered。 Instaed,在我的 MDI 应用程序中,一个不同的文档成为活动窗口。虽然它应该:

   void RColorPickerPopup::mousePressEvent ( QMouseEvent *event )
   { 
     ...
     QColorDialog *dialog = new QColorDialog( this );
     if( dialog->exec() )
     {
        hoverColor = dialog->selectedColor();
        delete dialog;
        emit selected( hoverColor );
        this->close();

如果我用原生 Windows ChooseColor 对话框替换 QColorDialog,则不会出现问题。

谁有提示,这里有什么问题?

【问题讨论】:

    标签: qt signals-slots qcolordialog


    【解决方案1】:

    我终于自己找到了答案: 问题是,我使用的 Qt MDI 示例具有这样的功能:

    MdiChild *MainWin::activeMdiChild()
    {
        if (QMdiSubWindow *activeSubWindow = mdiArea->activeSubWindow())
            return qobject_cast<MdiChild *>(activeSubWindow);
        return 0;
    }
    

    问题是

    如果 MDI 区域之外的小部件是活动窗口,则没有子窗口 将被激活

    (see Qt Documentation)

    显然,只要我显示一个模态 Qt 对话框,就没有 MDI 子窗口不再具有焦点 - 如果我使用本机 Windows 对话框,情况似乎并非如此。当我尝试设置所选元素的颜色时,我调用了 MainWin::activeMdiChild,如果 Qt 对话框位于顶部,则返回 NULL(是的,我应该检查是否返回 NULL,此时这是意外的)。

    解决方案是用 currentSubWindow 替换 activeSubWindow。

    MdiChild *MainWin::activeMdiChild()
    {
        if (QMdiSubWindow *activeSubWindow = mdiArea->currentSubWindow())
            return qobject_cast<MdiChild *>(activeSubWindow);
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-29
      相关资源
      最近更新 更多