0

Essentially I want to trigger some code after the my QFileDiag has 1 or more files selected and is accepted ("open" button clicked), the problem is that I can't seem to actually trigger my code in the slot.

Here is the code in my main widget

file_select_diag = new QFileDiag(this)

connect(file_select_diag, &QFileDialog::fileSelected, this,
&MainWidget::connect_test);

auto files = file_select_diag->getOpenFileName(
        this,
        tr("test"),
        QDir::homePath(),
        tr("text (*.txt)");
void MainWidget::connect_test(QString str)
{
    cout << str.toStdString();
}

And here is the header declaration

{
    Q_OBJECT

public:
    explicit MainWidget(QWidget *parent = 0); //Constructor
    ~MainWidget();                            // Destructor

private slots:
    void connect_test(QString str);
    void connect_test2(); //like above but cout << "HIT" << end;


private:
    QFileDialog *file_select_diag;

I've tried connecting to both connect_test and connect_test2, when I run my app and select files, hit open, nothing happens.

  • 2
    Note that [`QFileDialog::getOpenFileName`](https://doc.qt.io/qt-5/qfiledialog.html#getOpenFileName) is a `static` member of `QFileDialog` so the call `file_select_diag->getOpenFileName(...)` effectively creates a `QFileDialog` instance independent of `file_select_diag`and calls `getOpenFileName` against that. – G.M. Jul 22 '21 at 21:14
  • If the question is solved, please answer you own question instead of putting the solution in the question – Tzig Jul 23 '21 at 07:37

1 Answers1

0

Solution (copied from G.M.'s comment below)

Note that QFileDialog::getOpenFileName is a static member of QFileDialog so the call file_select_diag->getOpenFileName(...) effectively creates a QFileDialog instance independent of file_select_diagand calls getOpenFileName against that.

So effectively the two approaches here are either go entirely with the static method getOpenFileName and do not initialize file_select_diag or go entirely for the instance approach, configure the file_select_diag then use file_select_diag->show(), in which case the signal will work.