Using Qt to replace SendMessage macro in Windows

  • Need to replace a SendMessage Windows macro that set the default button in a dialog.

    hwnd = (HWND) field->id;
    if (hwnd)

    But when replacing using:

    QPushButton defaultButton = static_cast<QPushButton>(field->id);

    if (defaultButton)

    a different button is being set as the default. Any way I can exactly replicate the SendMessage call using Qt's default button mechanisms? Thanks.

  • the options setDefault() and setAutoDefault() should do the trick.

    This shouldn't work at all:

    QPushButton defaultButton = static_cast<QPushButton>(field->id);

    maybe this is what you meant:

    QPushButton defaultButton = reinterpret_cast<QPushButton>(field->id);

    assuming field->id is a pointer to a button or related widget.

    In your initial example field->id is cast to HWND. Is this still the case?

    Just a thought. If you are not using pointers and trying to set the default button options maybe this is working but you are setting it on a copy that is lost when the function goes outside of scope?

  • Field is a parameter of the function that contains this code, and id represents the Qt object that is being set as the default button. When using the win32 code, SendMessage sets the 'open' button as the immediate default, but when using the Qt classes as I was doing above, the immediate default is set to the 'close' button. Replacing static_cast with reinterpret_cast didn't do anything.

    Also, another problem is that when I click the 'close' button, the default button changes to the 'open' button, and when Enter is pressed, the 'open file' dispatch window opens up, as if that button had been pressed.

  • Aside from type of casting what about the rest. In your sample you are not using a pointer to an existing button but creating a new one (a copy) with this line:

    QPushButton defaultButton = static_cast<QPushButton>(field->id);

    If yes the copy would have the default set and not the intended one (along with a few other problems I can only guess at).

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    To add to Rondog, you should use qobject_cast when casting QObject derived class.

  • The entire code:

    LUIFChangeDefaultButton_qt(UIF_FORM *fp, UIF_FIELD *field)
    if (!field || !fp)

    QPushButton defaultButton = static_cast<QPushButton>(fp->defaultField->id);

    /* disable old default button */
    if (fp->defaultField && defaultButton)

    /* enable new default button */
    if (field)
    fp->defaultField = field;

    defaultButton = static_cast<QPushButton*>(field->id);

    if (defaultButton)

  • I don't see a problem with this.

    I would make sure that for each button created the auto default is disabled (setAutoDefault(false)) and only the default is set for the initial one intended to be the default.

    The options setDefault(TRUE | FALSE) might be problem depending on how (TRUE | FALSE) is defined. Maybe using just (true | false).

    If default buttons are changing maybe there is some additional code somewhere doing this?

Log in to reply

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