QfileDialog::Getopenfilename does not open the second time and after
-
Hello,
It's 2020 and what better way to spend time during the outbreak than to learn the magics of Qt.
This is my first software on Qt.
Problem:
short version:
static QString dialogResult = QFileDialog::getOpenFileName(this,"", "", "Images (*.png *.jpg)");
does NOT do anything after it was used once.long version:
I have this code connected to a menu:static QString dialogResult = QFileDialog::getOpenFileName(this,"", "", "Images (*.png *.jpg)"); if (dialogResult != "") { loadedPixMap.load(dialogResult); ui->labelImage->setScaledContents(true); ui->labelImage->setPixmap(loadedPixMap); int redTotal = 0; int blueTotal = 0; int greenTotal = 0; QImage image = loadedPixMap.toImage(); for (int i = 0; i < image.width(); i++) { for (int j = 0; j < image.height(); j++) { int r = 0, g = 0, b = 0; image.pixelColor(i, j).getRgb(&r, &g, &b); // Simple example to do almost nothing. } } int width = image.width(); ui->labelTest->setText(QString("example: %1").arg(width)); }1- I click on the menu, the code gets called
2- I get an open dialog
3- I select an image
4- I hit OK
5- It runs the code, shows an image and then updates the text.
6- I click on the same menu again.
7- problem nothing happens!!!!!!What am I doing wrong?
Please avoid posting replies such as "I never had that problem" or "It works fine for me!".
I'm using:
1- Windows 10 pro
2- Qt Creator 4.12 (Based on 5.14.2, MSVC 2017 32-bit) built on Apr 22 2020.
3- Compiling the code in both debug and release for MinGW 64-bit.!
4- config buildIf anyone actually experienced this and knows a solution I would really appreciate it.
Thank you.
-
Remove that
static.Static local variables
Variables declared at block scope with the specifier static or thread_local (since C++11) have static or thread (since C++11) storage duration but are initialized the first time control passes through their declaration (unless their initialization is zero- or constant-initialization, which can be performed before the block is first entered). On all further calls, the declaration is skipped.
So with the
statickeyword,getOpenFileNamewill only be called at the first time. -
Remove that
static.Static local variables
Variables declared at block scope with the specifier static or thread_local (since C++11) have static or thread (since C++11) storage duration but are initialized the first time control passes through their declaration (unless their initialization is zero- or constant-initialization, which can be performed before the block is first entered). On all further calls, the declaration is skipped.
So with the
statickeyword,getOpenFileNamewill only be called at the first time.@Bonnie said in QfileDialog::Getopenfilename does not open the second time and after:
Remove that
static.Static local variables
Variables declared at block scope with the specifier static or thread_local (since C++11) have static or thread (since C++11) storage duration but are initialized the first time control passes through their declaration (unless their initialization is zero- or constant-initialization, which can be performed before the block is first entered). On all further calls, the declaration is skipped.
So with the
statickeyword,getOpenFileNamewill only be called at the first time.Thanks! I just realized that I should not have copy pasted that line of code without looking at it closer.
Cheers.