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. How to use string as ui->qpushbutton (typecast)
Forum Updated to NodeBB v4.3 + New Features

How to use string as ui->qpushbutton (typecast)

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 1.7k 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.
  • E Offline
    E Offline
    esconda
    wrote on 14 Mar 2018, 07:11 last edited by
    #1

    Hello everyone,
    I would like to inform you that i created a desktop application based on ui(qwidget) implementation.

    I used many qpushbutton on the gui and i just would like to call it by ui->string name(not q pushbutton name)

    For example,
    Normally i call pushbutton with ui->camera1 which is qpushbutton name specified in mainwindow.ui

    But what i want is,
    Define string as Camera1,Camera2,... and so on instead of ui->Camera1 qpushbutton name.

    But unfortunately "ui->" just recognize qpushbutton names and when i try to call with ui->string name it does not recognize.

    İf i can not use the string name instead of the qpushbutton which is defined, i will have to create a seperate loop for each qpushbutton name.

    İf i call the button with a string name via ui, I will not need to create a loop for each button.

    Is there any method or example to call button with string name(i dont want Qmap method because i have to define a string for only one button).

    Ui->stringname as qpushbutton name.

    S J 2 Replies Last reply 14 Mar 2018, 07:24
    0
    • E esconda
      14 Mar 2018, 07:11

      Hello everyone,
      I would like to inform you that i created a desktop application based on ui(qwidget) implementation.

      I used many qpushbutton on the gui and i just would like to call it by ui->string name(not q pushbutton name)

      For example,
      Normally i call pushbutton with ui->camera1 which is qpushbutton name specified in mainwindow.ui

      But what i want is,
      Define string as Camera1,Camera2,... and so on instead of ui->Camera1 qpushbutton name.

      But unfortunately "ui->" just recognize qpushbutton names and when i try to call with ui->string name it does not recognize.

      İf i can not use the string name instead of the qpushbutton which is defined, i will have to create a seperate loop for each qpushbutton name.

      İf i call the button with a string name via ui, I will not need to create a loop for each button.

      Is there any method or example to call button with string name(i dont want Qmap method because i have to define a string for only one button).

      Ui->stringname as qpushbutton name.

      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 14 Mar 2018, 07:24 last edited by
      #2

      @esconda said in How to use string as ui->qpushbutton (typecast):

      ui->string name(not q pushbutton name)

      This is impossible. The "ui" thingy is a c++ object and by calling ui->something you are calling it's member variable. It's not possible to call it via string like ui->"something", same as it is impossible to call any other method / member by string in such a way.

      However, there is another possibility: give your pushbuttons an objectName (you can set it programmatically or in Qt Designer) and then you can probably use ui->findChild<QPushButton*>("myStringName").

      (Z(:^

      1 Reply Last reply
      3
      • E esconda
        14 Mar 2018, 07:11

        Hello everyone,
        I would like to inform you that i created a desktop application based on ui(qwidget) implementation.

        I used many qpushbutton on the gui and i just would like to call it by ui->string name(not q pushbutton name)

        For example,
        Normally i call pushbutton with ui->camera1 which is qpushbutton name specified in mainwindow.ui

        But what i want is,
        Define string as Camera1,Camera2,... and so on instead of ui->Camera1 qpushbutton name.

        But unfortunately "ui->" just recognize qpushbutton names and when i try to call with ui->string name it does not recognize.

        İf i can not use the string name instead of the qpushbutton which is defined, i will have to create a seperate loop for each qpushbutton name.

        İf i call the button with a string name via ui, I will not need to create a loop for each button.

        Is there any method or example to call button with string name(i dont want Qmap method because i have to define a string for only one button).

        Ui->stringname as qpushbutton name.

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 14 Mar 2018, 07:29 last edited by
        #3

        hi @esconda ,
        I know of only 1 way to archive that:

        QPushButton *btn = ui->findChild<QPushButton *>("Stringname");
        if(btn)
        //btn was found
        else
        //btn was not found
        

        but, that is a heavy function, and the time to execute this increases significantly with the number of items it has to go through

        The arguably better version would be, after setupUi go through all items once, and store the Name and a Pointer to the object in a List and later in your code, go through that one instead


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        J 1 Reply Last reply 14 Mar 2018, 08:46
        3
        • E Offline
          E Offline
          esconda
          wrote on 14 Mar 2018, 07:47 last edited by esconda
          #4

          I am trying to call findchold with ui->findchild but unfortunately findchild is not available inside of "ui".ui does not recognize.

          I think your answers will work correctly and solve my problem.How can i recognize findchild inside of the ui.(is there any lib to define for it)?

          The error is,

          C2662: T Qobject::findChild <QPushButton*> cannot convert "this" pointer from Ui::MainWindow to const QObject &

          İ have mainwindow class and directly connected to ui.İn the constructor i have ui-setupui(this)

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sierdzio
            Moderators
            wrote on 14 Mar 2018, 08:16 last edited by
            #5

            Oh it might be that ui is not a QObject in itself. In such case, try findChild not from ui but from your main window.

            (Z(:^

            1 Reply Last reply
            1
            • E Offline
              E Offline
              esconda
              wrote on 14 Mar 2018, 08:26 last edited by
              #6

              Thank you so much, i really apreciated for your helps.

              Now i use it like,

              MainWindow::findChild<Qpushbutton*> ("Mystringasbuttonname")->setİcon(icon path)

              It works like a charm, you saved my life and time.

              My best regards

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sierdzio
                Moderators
                wrote on 14 Mar 2018, 08:34 last edited by
                #7

                You're welcome, happy coding! :-)

                (Z(:^

                1 Reply Last reply
                0
                • J J.Hilk
                  14 Mar 2018, 07:29

                  hi @esconda ,
                  I know of only 1 way to archive that:

                  QPushButton *btn = ui->findChild<QPushButton *>("Stringname");
                  if(btn)
                  //btn was found
                  else
                  //btn was not found
                  

                  but, that is a heavy function, and the time to execute this increases significantly with the number of items it has to go through

                  The arguably better version would be, after setupUi go through all items once, and store the Name and a Pointer to the object in a List and later in your code, go through that one instead

                  J Offline
                  J Offline
                  JonB
                  wrote on 14 Mar 2018, 08:46 last edited by JonB
                  #8

                  @esconda

                  @J.Hilk said in How to use string as ui->qpushbutton (typecast):

                  but, that is a heavy function, and the time to execute this increases significantly with the number of items it has to go through
                  The arguably better version would be, after setupUi go through all items once, and store the Name and a Pointer to the object in a List and later in your code, go through that one instead.

                  As @J-Hilk says, this is the usual way to code so that you can "look up" an object by a name instead of using the corresponding object directly. Instead of a QList, which you have to search to find a name, use a QHash, and the lookup will be "virtually instantaneous", and will not slow down as the number of buttons/objects gets large, unlike calling findChild() each time.

                  Whether you do that or not, I suggest you make your findChild<Qpushbutton*> (buttonName) (or QHash::find()) code into a function and always call that, so that you can easily identify/potentially modify this implementation choice at a later date.

                  1 Reply Last reply
                  2

                  1/8

                  14 Mar 2018, 07:11

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved