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. Using Qt to replace SendMessage macro in Windows
Forum Updated to NodeBB v4.3 + New Features

Using Qt to replace SendMessage macro in Windows

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.1k 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.
  • J Offline
    J Offline
    joeydonovan4
    wrote on last edited by
    #1

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

    hwnd = (HWND) field->id;
    if (hwnd)
    SendMessage(hwnd, BM_SETSTYLE, (WPARAM) LOWORD(BS_DEFPUSHBUTTON), MAKELPARAM(TRUE, 0));

    But when replacing using:

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

    if (defaultButton)
    defaultButton->setDefault(TRUE);

    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.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rondog
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • J Offline
        J Offline
        joeydonovan4
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rondog
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi and welcome to devnet,

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • J Offline
              J Offline
              joeydonovan4
              wrote on last edited by
              #6

              The entire code:

              @void
              LUIFChangeDefaultButton_qt(UIF_FORM *fp, UIF_FIELD *field)
              {
              if (!field || !fp)
              return;

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

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

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

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

              if (defaultButton)
              defaultButton->setDefault(TRUE);
              }
              }@

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Rondog
                wrote on last edited by
                #7

                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?

                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