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. Problem using the if...else structure in main.cpp
Qt 6.11 is out! See what's new in the release blog

Problem using the if...else structure in main.cpp

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.5k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Im trying to use the if else structure to control which form is shown before the main window. The app first checks if a file named add.txt is present then shows thecorrect form. Here's my code:

    @
    //a continuation of the main.cpp

    if (0 == access ("C:/Microsoft/add.txt",0))

    (MainWindow z;
    z.show();
    }

    else {
    window2 y;
    y.show();
    }

    return a.exec();
    }
    @

    However, the window just blinks and runs in the background with no UI. How do I make it run correctly by showing the UI??

    [EDIT: code formatting, please wrap in @-tags, Volker]

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Both z and y are declared inside the block where you call show on them. Once you leave that block they are going out and are destroyed.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mlong
        wrote on last edited by
        #3

        I would rewrite it as something like:
        @
        //a continuation of the main.cpp

        MainWindow *z = 0;
        window2 *y = 0;

        if (0 == access ("C:/Microsoft/add.txt",0))
        z = new MainWindow();
        z->show();
        } else {
        y = new window2();
        y->show();
        }

        return a.exec();
        }
        @

        Brain to terminal; ymmv

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply
        0

        • Login

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