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. How to create this simple form by code?
Forum Updated to NodeBB v4.3 + New Features

How to create this simple form by code?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 451 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.
  • AlveinA Offline
    AlveinA Offline
    Alvein
    wrote on last edited by
    #1

    Hi. Look...

    mainwindow.JPG

    Just a QPlainTextEdit and a QStatusBar.

    I don't feel like designing an UI for that. Anyway, I'd like to know how to do it by code, since the results I'm getting don't look like what the Designer creates.

    This is the source of mainwindow.ui:

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>800</width>
        <height>600</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <layout class="QVBoxLayout" name="verticalLayout">
        <item>
         <widget class="QPlainTextEdit" name="plainTextEdit"/>
        </item>
       </layout>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections/>
    </ui>
    

    Question: does this file defines every single thing required to create the form? If yes, I assume that every undefined property will be set to their defaults. Shouldn't this happen when you create the components by code?

    Question: how do I create that form by hand? Is it possible to "follow" the previous dead simple XML instructions and get the same result?

    Thanks for your help. :)

    Chris KawaC JonBJ 2 Replies Last reply
    0
    • AlveinA Alvein

      Hi. Look...

      mainwindow.JPG

      Just a QPlainTextEdit and a QStatusBar.

      I don't feel like designing an UI for that. Anyway, I'd like to know how to do it by code, since the results I'm getting don't look like what the Designer creates.

      This is the source of mainwindow.ui:

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>MainWindow</class>
       <widget class="QMainWindow" name="MainWindow">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>800</width>
          <height>600</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>MainWindow</string>
        </property>
        <widget class="QWidget" name="centralwidget">
         <layout class="QVBoxLayout" name="verticalLayout">
          <item>
           <widget class="QPlainTextEdit" name="plainTextEdit"/>
          </item>
         </layout>
        </widget>
        <widget class="QStatusBar" name="statusbar"/>
       </widget>
       <resources/>
       <connections/>
      </ui>
      

      Question: does this file defines every single thing required to create the form? If yes, I assume that every undefined property will be set to their defaults. Shouldn't this happen when you create the components by code?

      Question: how do I create that form by hand? Is it possible to "follow" the previous dead simple XML instructions and get the same result?

      Thanks for your help. :)

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

      @Alvein said:

      Question: does this file defines every single thing required to create the form? If yes, I assume that every undefined property will be set to their defaults. Shouldn't this happen when you create the components by code?

      This file gets translated directly to C++ and as such it follows the normal C++ rules - a class is constructed and members follow the normal construction rules. All Qt ui classes have their members initialized to some defaults so yes, anything you don't define here will get defaulted.

      Question: how do I create that form by hand? Is it possible to "follow" the previous dead simple XML instructions and get the same result?

      You can pretty much translate it line by line into C++. For example:

      <widget class="QMainWindow" name="MainWindow">
      

      This translates to

      QMainWindow* mw = new QMainWindow();
      mv->setObjectName("MainWindow);
      

      Going further:

      <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>800</width>
          <height>600</height>
         </rect>
      </property>
      

      translates to

      mw->setGeometry(0, 0, 800, 600);
      

      and so on.

      In fact this translation is exactly what the uic tool does when you use that xml in your project so you can just take a look at its output and you'll see exactly the code it generated (it's inside the setupUi() function called in the MainWindow's contructor). It will be very much a line by line translation like I mentioned.

      AlveinA 1 Reply Last reply
      3
      • AlveinA Alvein

        Hi. Look...

        mainwindow.JPG

        Just a QPlainTextEdit and a QStatusBar.

        I don't feel like designing an UI for that. Anyway, I'd like to know how to do it by code, since the results I'm getting don't look like what the Designer creates.

        This is the source of mainwindow.ui:

        <?xml version="1.0" encoding="UTF-8"?>
        <ui version="4.0">
         <class>MainWindow</class>
         <widget class="QMainWindow" name="MainWindow">
          <property name="geometry">
           <rect>
            <x>0</x>
            <y>0</y>
            <width>800</width>
            <height>600</height>
           </rect>
          </property>
          <property name="windowTitle">
           <string>MainWindow</string>
          </property>
          <widget class="QWidget" name="centralwidget">
           <layout class="QVBoxLayout" name="verticalLayout">
            <item>
             <widget class="QPlainTextEdit" name="plainTextEdit"/>
            </item>
           </layout>
          </widget>
          <widget class="QStatusBar" name="statusbar"/>
         </widget>
         <resources/>
         <connections/>
        </ui>
        

        Question: does this file defines every single thing required to create the form? If yes, I assume that every undefined property will be set to their defaults. Shouldn't this happen when you create the components by code?

        Question: how do I create that form by hand? Is it possible to "follow" the previous dead simple XML instructions and get the same result?

        Thanks for your help. :)

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

        @Alvein
        Do you understand that the build process runs https://doc.qt.io/qt-5/uic.html ? That reads through the mainwindow.ui file and produces (I think) a mainwindow.cpp & mainwindow.h. If you want to be sure you get exactly what it produces, look through that code. Yes, most things are explicitly set to some defaults, or left to inherit the Qt widget defaults.

        Having seen what uic generates, you could write & maintain something like that yourself if you want to create a widget without designing it for the .ui file. The example you give could be written in half a dozen explicit statements. If that's what you were asking.

        1 Reply Last reply
        2
        • Chris KawaC Chris Kawa

          @Alvein said:

          Question: does this file defines every single thing required to create the form? If yes, I assume that every undefined property will be set to their defaults. Shouldn't this happen when you create the components by code?

          This file gets translated directly to C++ and as such it follows the normal C++ rules - a class is constructed and members follow the normal construction rules. All Qt ui classes have their members initialized to some defaults so yes, anything you don't define here will get defaulted.

          Question: how do I create that form by hand? Is it possible to "follow" the previous dead simple XML instructions and get the same result?

          You can pretty much translate it line by line into C++. For example:

          <widget class="QMainWindow" name="MainWindow">
          

          This translates to

          QMainWindow* mw = new QMainWindow();
          mv->setObjectName("MainWindow);
          

          Going further:

          <property name="geometry">
             <rect>
              <x>0</x>
              <y>0</y>
              <width>800</width>
              <height>600</height>
             </rect>
          </property>
          

          translates to

          mw->setGeometry(0, 0, 800, 600);
          

          and so on.

          In fact this translation is exactly what the uic tool does when you use that xml in your project so you can just take a look at its output and you'll see exactly the code it generated (it's inside the setupUi() function called in the MainWindow's contructor). It will be very much a line by line translation like I mentioned.

          AlveinA Offline
          AlveinA Offline
          Alvein
          wrote on last edited by
          #4

          @Chris-Kawa @JonB

          Thank you very much, guys.

          The file I had to check was "ui_mainwindow.h".

          Inside, I found the thing I was missing:

          MainWindow->setStatusBar(statusbar);
          

          The rest of instructions are pretty much like the ones I tried.

          But without the previous like, I was trying to just set the status bar at the bottom of the layout, with no success. I still think that there should be a way of doing that without using setStatusBar().

          Cheers!

          JonBJ 1 Reply Last reply
          0
          • AlveinA Alvein

            @Chris-Kawa @JonB

            Thank you very much, guys.

            The file I had to check was "ui_mainwindow.h".

            Inside, I found the thing I was missing:

            MainWindow->setStatusBar(statusbar);
            

            The rest of instructions are pretty much like the ones I tried.

            But without the previous like, I was trying to just set the status bar at the bottom of the layout, with no success. I still think that there should be a way of doing that without using setStatusBar().

            Cheers!

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

            @Alvein
            Yep, that's why looking at the generated code is useful :)

            See the picture at https://doc.qt.io/qt-5/qmainwindow.html#details. This shows how a QMainWindow is laid out, and goes on to explain how you can affect each of the reserved areas. QMainWindow is not "magic", it's simply a QWidget window with a predesigned generic layout for you to follow.

            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