user2090617
user2090617

Reputation: 11

Sharing variable between two buttons in Qt

void xx::on_pushButton_clicked()
{
    QFileDialog dialog(this);
    dialog.setNameFilter(tr("Images (*.png *.gif *.jpg)"));
    dialog.setViewMode(QFileDialog::Detail);

    QString fn = QFileDialog::getOpenFileName(this,
                                              tr("Select Image"),
                                              "e:/",
                                              tr("Images (*.png)"));
    // Do something
}

void xx::on_pushButton_2_clicked()
{
    QString ex= fn; // to be accessed from the above button selection
}

I would like use the filename selected using a button for an action to be established by another button. How can I do it?

Upvotes: 1

Views: 80

Answers (1)

6502
6502

Reputation: 114579

You can simply declare fn to be a data member of class xx.

Given the simplicity of the question really I wonder if you're trying to learn C++ with a try-and-see approach. While this could be a reasonable approach with other languages or environments (e.g. Python) it's a truly TERRIBLE idea with C++ for two reasons:

  1. C++ in a few parts is quite "illogical" for historical reasons and because of committee effect. The only way to learn how it behaves is reading, because logic is not always going to give you the correct answer.

  2. C++ main philosophy is that the programmer makes no errors and when a programmer does the result is not a "runtime error", but "undefined behaviour".

The mix of these two factors makes the try-and-see approach to C++ just suicidal: the language is complex and illogic and it will not tell you when you make mistakes.

You should really pick a good C++ book and read it cover-to-cover first.

Upvotes: 1

Related Questions