QFileDialog中的后退和前进按钮没有信号

时间:2015-02-16 07:07:11

标签: c++ qt

我正在使用QFileDialog :: DontUseNativeDialog。现在我遇到了信号问题。这是我的示例代码:

class A : public QFileDialog
{
    A(){
       setOption(QFileDialog::DontUseNativeDialog);
       connect(this, SIGNAL(directoryEntered(const Qstring), this, SLOT(foo(const QString)));
    }

    foo(const QString path){
    QDir dir(path);
    // Code...
   }
};

现在当我使用DontUseNativeDialog选项时,我在对话框的右上角有三个导航按钮,它们是:

 1. Back
 2. Forward
 3. Parent Directory

当我按下Parent Directory按钮时,会触发信号directoryEntered(const QString)。但是在后退和前进按钮的情况下它不起作用。 我可以使用任何不同的信号吗?请帮忙。 谢谢。

1 个答案:

答案 0 :(得分:3)

QFileDialog和Qt4.8.x有同样的问题

解决方案是手动发出按钮点击信号:

class FileDialog : public QFileDialog {

public:    
    FileDialog() : QFileDialog() {
        this->fixHistoryButtons();
    }

private:
    void fixHistoryButtons() {
        QToolButton backButton = this->findChild<QToolButton *>("backButton");
        QToolButton forwardButton = this->findChild<QToolButton *>("forwardButton");
        this->connect(backButton, SIGNAL(clicked()), SLOT(backOrForwardButtonClicked()));
        this->connect(forwardButton, SIGNAL(clicked()), SLOT(backOrForwardButtonClicked()));
    }
private slots:
    void backOrForwardButtonClicked() {
        /* this->directory() should be save, since we should be called after QFileDialog has done it's thing */
        emit this->directoryChanged( this->directory().absolutePath() );
    }
};