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. Ui::MainWindow *ui; Could it work without use the prefix?
Qt 6.11 is out! See what's new in the release blog

Ui::MainWindow *ui; Could it work without use the prefix?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 323 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.
  • grafenocarbonoG Offline
    grafenocarbonoG Offline
    grafenocarbono
    wrote on last edited by
    #1

    Hi Geeks,

    I have defined the following code:
    ...................
    ;..................
    namespace Ui {
    class MainWindow;
    }
    ..................
    .................
    Ui::MainWindow *ui;
    .....................
    .....................

    Why do I have to use the prefix "Ui" of namespace in the sentence "Ui::MainWindow *ui;"?. Could it work without use the prefix?

    Thanks in advance

    Chris KawaC 1 Reply Last reply
    0
    • grafenocarbonoG grafenocarbono

      Hi Geeks,

      I have defined the following code:
      ...................
      ;..................
      namespace Ui {
      class MainWindow;
      }
      ..................
      .................
      Ui::MainWindow *ui;
      .....................
      .....................

      Why do I have to use the prefix "Ui" of namespace in the sentence "Ui::MainWindow *ui;"?. Could it work without use the prefix?

      Thanks in advance

      Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      @grafenocarbono Project template generates structure like this:

      namespace Ui {
          class MainWindow;
      }
      
      class MainWindow : public QMainWindow
      {
          ...
          Ui::MainWindow *ui;
      }
      

      MainWindow and Ui::MainWindow are two different classes. Ui::MainWindow is the class generated from the .ui file. If you remove the namespace from Ui::MainWindow *ui; you'll get a pointer to self, and that doesn't make sense.

      If you don't want the namespace you can also use the version without it:

      class Ui_MainWindow;
      
      class MainWindow : public QMainWindow
      {
          ...
          Ui_MainWindow *ui;
      }
      

      or rename the widget in the designer to e.g. MyClass and then

      class Ui_MyClass;
      
      class MainWindow : public QMainWindow
      {
          ...
          Ui_MyClass *ui;
      }
      

      or the namespace version.

      JonBJ 1 Reply Last reply
      5
      • Chris KawaC Chris Kawa

        @grafenocarbono Project template generates structure like this:

        namespace Ui {
            class MainWindow;
        }
        
        class MainWindow : public QMainWindow
        {
            ...
            Ui::MainWindow *ui;
        }
        

        MainWindow and Ui::MainWindow are two different classes. Ui::MainWindow is the class generated from the .ui file. If you remove the namespace from Ui::MainWindow *ui; you'll get a pointer to self, and that doesn't make sense.

        If you don't want the namespace you can also use the version without it:

        class Ui_MainWindow;
        
        class MainWindow : public QMainWindow
        {
            ...
            Ui_MainWindow *ui;
        }
        

        or rename the widget in the designer to e.g. MyClass and then

        class Ui_MyClass;
        
        class MainWindow : public QMainWindow
        {
            ...
            Ui_MyClass *ui;
        }
        

        or the namespace version.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Chris-Kawa said in Ui::MainWindow *ui; Could it work without use the prefix?:

        MainWindow and Ui::MainWindow are two different classes

        @grafenocarbono
        As @Chris-Kawa has written, this is a vital observation to get your head around. Designer produces code where all the UI stuff goes into a Ui::MainWindow class and gives you a separate MainWindow class where you write whatever code of your own which can access the widgets in Ui::MainWindow class without stuff there interfering with whatever you declare in your MainWindow class.

        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