Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QfileDialog::Getopenfilename does not open the second time and after
QtWS25 Last Chance

QfileDialog::Getopenfilename does not open the second time and after

Scheduled Pinned Locked Moved Solved General and Desktop
qimageqpixmapqfiledialog
3 Posts 2 Posters 791 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aramaz
    wrote on last edited by
    #1

    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!!!!!!

    Similar issue since 2014

    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 build

    If anyone actually experienced this and knows a solution I would really appreciate it.

    Thank you.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      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 static keyword, getOpenFileName will only be called at the first time.

      A 1 Reply Last reply
      3
      • B Bonnie

        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 static keyword, getOpenFileName will only be called at the first time.

        A Offline
        A Offline
        aramaz
        wrote on last edited by
        #3

        @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 static keyword, getOpenFileName will 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.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved