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. Check if a widget with focus is a Button or a Slider?

Check if a widget with focus is a Button or a Slider?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.7k Views
  • 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
    abdul_samad
    wrote on last edited by
    #1

    Hi I have developed a Simple Gui using Buttons(Push Buttons & Tool Buttons) and Sliders (Dial and slider). The focus keeps on moving from one widget to another during execution. Is there a way to check that the widget which currently has the focus is a button or slider? I was thinking that all the buttons are derived from QAbstractButton and sliders from QAbstractSlider so there should be a function to check which class the current widget belongs to?
    Thanks for any kind of help I am new around Qt and would appreciate any kind of help being provided!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Slawomir_Piernikowski
      wrote on last edited by Slawomir_Piernikowski
      #2

      Hi
      You can check in "if" statement in your function:
      .......
      bool itIsButton = false;
      bool itIsSlider = false;

      if(ui->PushButton->hasFocus())
      {
      return itIsButton = true;
      }
      else if(ui->slider->hasFocus())
      {
      return ItIsSlider = true;
      }
      .......
      and then you check the state of itIsButton and itIsSlider.
      You can also do other operation in the "if" statements instead of returning the values.

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

        @Slawomir_Piernikowski yes you are correct. but there are multiple button i.e. PushButton, PushButton2 and slo multiple sliders so I have to write if statement for each of the widget seprately. I was hoping for a much simpler solution that would involve less if statement such as one if for all kind of buttons and another if for all kind of sliders. Do you think that's possible?

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Following should help you.

          1. QApplication::focusWidget - It will give you focus widget reference.
          2. Obj->metaObject()->className() will give you class name.
          3. You can utilise the objectName as well.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          3
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @dheerendra said in Check if a widget with focus is a Button or a Slider?:

            Obj->metaObject()->className() will give you class name.

            or just use qobject_cast<> to check if it is from the expected type.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            4
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              @abdul_samad how did the issue go?. Are you able to solve this issue ? If the issue is resolved make the issue to SOLVED state so that it helps others.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              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