Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]QDialog, Mac OS, setWindowFlags
Qt 6.12 LTS is out! See what's new in the release blog

[Solved]QDialog, Mac OS, setWindowFlags

Scheduled Pinned Locked Moved General and Desktop
17 Posts 4 Posters 13.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.
  • G Offline
    G Offline
    gaojinhsu
    wrote on last edited by
    #1

    QDialog on Mac OS disenables the close button and the minimize button, and enables the maximize button by default. I just wanna enable the close button only, but I find that setWindowFlags method is useless.How can I get a dialog with a close button only?

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

      Easiest way would be to create your own personal QWidget that mimics a QDialog and set the window flags as you want :)

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gaojinhsu
        wrote on last edited by
        #3

        [quote author="b1gsnak3" date="1368443092"]Easiest way would be to create your own personal QWidget that mimics a QDialog and set the window flags as you want :)[/quote]
        the dialog result is useful,so are the other dialog attributes.A method can solve that problem on QDialog Class would be better.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          toptan
          wrote on last edited by
          #4

          Here is the code I use to fix this issue. You will have to invoke call native Cocoa api.

          @
          #import <AppKit/NSView.h>
          #import <AppKit/NSWindow.h>

          #include "util.h"

          void fixModalDialogClose(QDialog *dialog, bool resizable)
          {
          NSView *view = (NSView *)dialog->winId();
          NSWindow *window = [view window];
          if (resizable)
          [window setStyleMask:NSClosableWindowMask | NSTitledWindowMask | NSResizableWindowMask];
          else
          [window setStyleMask:NSClosableWindowMask | NSTitledWindowMask];
          }
          @

          File has to have .mm extension, in this case it is named osxutils.mm. Bellow are the piece of .pro file

          @
          OBJECTIVE_SOURCES += osxutil.mm
          @

          I use this to enable close button on modal dialogs. The dialog is not resizable unless you pass true as second parameter.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gaojinhsu
            wrote on last edited by
            #5

            [quote author="toptan" date="1368466162"]Here is the code I use to fix this issue. You will have to invoke call native Cocoa api.

            @
            #import <AppKit/NSView.h>
            #import <AppKit/NSWindow.h>

            #include "util.h"

            void fixModalDialogClose(QDialog *dialog, bool resizable)
            {
            NSView *view = (NSView *)dialog->winId();
            NSWindow *window = [view window];
            if (resizable)
            [window setStyleMask:NSClosableWindowMask | NSTitledWindowMask | NSResizableWindowMask];
            else
            [window setStyleMask:NSClosableWindowMask | NSTitledWindowMask];
            }
            @

            File has to have .mm extension, in this case it is named osxutils.mm. Bellow are the piece of .pro file

            @
            OBJECTIVE_SOURCES += osxutil.mm
            @

            I use this to enable close button on modal dialogs. The dialog is not resizable unless you pass true as second parameter.[/quote]
            where and how to use fixModalDialogClose function?

            1 Reply Last reply
            0
            • G Offline
              G Offline
              gaojinhsu
              wrote on last edited by
              #6

              i am not familiar with Mac OS platform...

              1 Reply Last reply
              0
              • T Offline
                T Offline
                toptan
                wrote on last edited by
                #7

                Well, this is the only way to achieve what you want on Mac...

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  gaojinhsu
                  wrote on last edited by
                  #8

                  Could you tell me more about where I should call your function and how to "include" osxutil.mm ?
                  [quote author="toptan" date="1368515186"]Well, this is the only way to achieve what you want on Mac...[/quote]

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    toptan
                    wrote on last edited by
                    #9
                    1. Create file called osxutil.h and paste this code to it
                      @
                      #ifndef UTIL_H
                      #define UTIL_H

                    #include <QDialog>

                    void fixModalDialogClose(QDialog *dialog, bool resizable);

                    #endif // UTIL_H
                    @

                    1. Create file osxutil.mm and paste this in it
                      @
                      #import <AppKit/NSView.h>
                      #import <AppKit/NSWindow.h>

                    #include "util.h"

                    void fixModalDialogClose(QDialog *dialog, bool resizable)
                    {
                    NSView *view = (NSView *)dialog->winId();
                    NSWindow *window = [view window];
                    if (resizable)
                    [window setStyleMask:NSClosableWindowMask | NSTitledWindowMask | NSResizableWindowMask];
                    else
                    [window setStyleMask:NSClosableWindowMask | NSTitledWindowMask];
                    }
                    @

                    1. Open the project file (.pro) and add util.h into HEADERS and osxutil.mm into OBJECTIVE_SOURCES. Something like
                      @
                      HEADERS += util.h
                      OBJECTIVE_SOURCES += osutil.mm
                      @

                    2. Here is the small example how I use fixModalDialogClose
                      @
                      m_aboutDialog = new MyAboutDialog();
                      m_aboutDialog->show();
                      fixModalDialogClose(m_aboutDialog, false);
                      connect(m_aboutDialog, SIGNAL(finished(int)), this, SLOT(onAboutDialogFinished(int)));
                      @

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      gaojinhsu
                      wrote on last edited by
                      #10

                      i do what u ask me to do, but nothing different happens to my dialog...awful!

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        gaojinhsu
                        wrote on last edited by
                        #11

                        i do what u ask me to do, but nothing different happens to my dialog…awful!
                        [quote author="toptan" date="1368516008"]1. Create file called osxutil.h and paste this code to it
                        @
                        #ifndef UTIL_H
                        #define UTIL_H

                        #include <QDialog>

                        void fixModalDialogClose(QDialog *dialog, bool resizable);

                        #endif // UTIL_H
                        @

                        1. Create file osxutil.mm and paste this in it
                          @
                          #import <AppKit/NSView.h>
                          #import <AppKit/NSWindow.h>

                        #include "util.h"

                        void fixModalDialogClose(QDialog *dialog, bool resizable)
                        {
                        NSView *view = (NSView *)dialog->winId();
                        NSWindow *window = [view window];
                        if (resizable)
                        [window setStyleMask:NSClosableWindowMask | NSTitledWindowMask | NSResizableWindowMask];
                        else
                        [window setStyleMask:NSClosableWindowMask | NSTitledWindowMask];
                        }
                        @

                        1. Open the project file (.pro) and add util.h into HEADERS and osxutil.mm into OBJECTIVE_SOURCES. Something like
                          @
                          HEADERS += util.h
                          OBJECTIVE_SOURCES += osutil.mm
                          @

                        2. Here is the small example how I use fixModalDialogClose
                          @
                          m_aboutDialog = new MyAboutDialog();
                          m_aboutDialog->show();
                          fixModalDialogClose(m_aboutDialog, false);
                          connect(m_aboutDialog, SIGNAL(finished(int)), this, SLOT(onAboutDialogFinished(int)));
                          @

                        [/quote]

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          toptan
                          wrote on last edited by
                          #12

                          The above code will not work with dialogs exec() call since it is blocking. Can you post you code here?

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            gaojinhsu
                            wrote on last edited by
                            #13

                            main.cpp:
                            @#include "accessdlg.h"
                            #include "osxutil.h"

                            int main(int argc, char *argv[])
                            {
                            QApplication a(argc, argv);
                            AccessDlg accDlg(true);
                            fixModalDialogClose(&accDlg, false);
                            accDlg.show();
                            return a.exec();
                            }
                            @
                            osxutil.h:
                            @
                            #ifndef UTIL_H
                            #define UTIL_H

                            #include <QDialog>

                            void fixModalDialogClose(QDialog *dialog, bool resizable);

                            #endif // UTIL_H
                            @
                            osxutil.mm:
                            @#import <AppKit/NSView.h>
                            #import <AppKit/NSWindow.h>

                            #include "osxutil.h"

                            void fixModalDialogClose(QDialog *dialog, bool resizable)
                            {
                            NSView view = (NSView)dialog->winId();
                            NSWindow *window = [view window];
                            if(resizable)
                            [window setStyleMask:NSClosableWindowMask | NSTitledWindowMask | NSResizableWindowMask];
                            else
                            [window setStyleMask:NSClosableWindowMask | NSTitledWindowMask];
                            }
                            @
                            .pro file:
                            @OBJECTIVE_SOURCES += osxutil.mm
                            HEADERS += osxutil.h
                            @
                            [quote author="toptan" date="1368522370"]The above code will not work with dialogs exec() call since it is blocking. Can you post you code here?[/quote]

                            1 Reply Last reply
                            0
                            • T Offline
                              T Offline
                              toptan
                              wrote on last edited by
                              #14

                              You overlooked my example :) You should call fixModalDialogClose() after show().

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                gaojinhsu
                                wrote on last edited by
                                #15

                                Bingo! btw,can i remove the other two disabled buttons?
                                [quote author="toptan" date="1368523275"]You overlooked my example :) You should call fixModalDialogClose() after show().[/quote]

                                1 Reply Last reply
                                0
                                • T Offline
                                  T Offline
                                  toptan
                                  wrote on last edited by
                                  #16

                                  I guess you could. Look at Apple's documentation for NSWindow and play with setStyleMask in fixModalDialogClose method.
                                  Please mark thread as solved by editing first post and prepending [SOLVED] to thread subject.

                                  1 Reply Last reply
                                  0
                                  • napajejenunedk0N Offline
                                    napajejenunedk0N Offline
                                    napajejenunedk0
                                    wrote on last edited by
                                    #17

                                    bq. The above code will not work with dialogs exec() call since it is blocking. Can you post you code here?

                                    It works. But you need to call both show() and exec():

                                    QDialog dlg;
                                    dlg.show();
                                    fixModalDialogClose( & dlg, false );
                                    dlg.exec();

                                    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
                                    • Unsolved