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. When may use QMainWindow instead of QDialog ?
Forum Updated to NodeBB v4.3 + New Features

When may use QMainWindow instead of QDialog ?

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

    Example BasicLayouts uses QDialog. This dialog is main application window.
    If I change QDIalog to QMainWindow, all widgets are invisible.
    When is better using QMainWindow over QDialog?

    JonBJ 1 Reply Last reply
    0
    • A AndrzejB

      Example BasicLayouts uses QDialog. This dialog is main application window.
      If I change QDIalog to QMainWindow, all widgets are invisible.
      When is better using QMainWindow over QDialog?

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

      @AndrzejB said in When may use QMainWindow instead of QDialog ?:

      When is better using QMainWindow over QDialog?

      Neither is "better", it spends what you facilities you want. QDialog is plain, QMainWindow has a number of "areas" reserved on it for menus, toolbars etc. as per the diagram at https://doc.qt.io/qt-5/qmainwindow.html#details. If you do not want any of those QMainWindow is overkill.

      If I change QDIalog to QMainWindow, all widgets are invisible.

      Then your code is wrong. For a QMainWindow as per Creating Main Window Components you must use

      Note: Creating a main window without a central widget is not supported. You must have a central widget even if it is just a placeholder.
      You set the central widget with setCentralWidget().

      1 Reply Last reply
      1
      • A Offline
        A Offline
        AndrzejB
        wrote on last edited by
        #3

        setCentralWidget must be pointer or object?
        Two situations:

        • pointer: setCentralWidget(edit);
        • object: setCentralWidget(&edit);

        Object is deleted when mainform is deleted by C++.
        object pointed by pointer is deleted by Qt library when mainform is deleted? What about place address object, not pointer?

        JonBJ 1 Reply Last reply
        0
        • A AndrzejB

          setCentralWidget must be pointer or object?
          Two situations:

          • pointer: setCentralWidget(edit);
          • object: setCentralWidget(&edit);

          Object is deleted when mainform is deleted by C++.
          object pointed by pointer is deleted by Qt library when mainform is deleted? What about place address object, not pointer?

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

          @AndrzejB said in When may use QMainWindow instead of QDialog ?:

          pointer: setCentralWidget(edit);

          new => simple, safe.

          object: setCentralWidget(&edit);

          Usually OK, because edit variable goes out of scope and so is de-parented from QMainWindow before that gets destroyed.

          I prefer new. Note that situation is no different for setCentralWidget() than any other time you place a widget on a widget, nothing special.

          M 1 Reply Last reply
          0
          • JonBJ JonB

            @AndrzejB said in When may use QMainWindow instead of QDialog ?:

            pointer: setCentralWidget(edit);

            new => simple, safe.

            object: setCentralWidget(&edit);

            Usually OK, because edit variable goes out of scope and so is de-parented from QMainWindow before that gets destroyed.

            I prefer new. Note that situation is no different for setCentralWidget() than any other time you place a widget on a widget, nothing special.

            M Offline
            M Offline
            mpergand
            wrote on last edited by mpergand
            #5

            @JonB said in When may use QMainWindow instead of QDialog ?:

            Usually OK, because edit variable goes out of scope and so is de-parented from QMainWindow before that gets destroyed.

            If QMainWindow is set with WA_DeleteOnClose it will clash for sure when the window is closed.

            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