[Solved]QDialog, Mac OS, setWindowFlags



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



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



  • [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.



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



  • i am not familiar with Mac OS platform...



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



  • 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. 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)));
      @



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



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



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



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



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



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



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



  • 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();


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.