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. Function for setting button text
Forum Updated to NodeBB v4.3 + New Features

Function for setting button text

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.8k Views 2 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.
  • Q Offline
    Q Offline
    QT_fan
    wrote on last edited by
    #1

    I am using the following code to set the text on a button:

    ui->MyButton_1->setText("ON");

    I would like to create a function to do this with two arguments; a string for the text and the button name. No problem for the text but how can I pass the button name to the function?
    The function would look like this:

    void MainWindow::SetButtonText(QString S, ????)
    {
    ????->setText(S);
    }
    The call would be like this:
    setText("ON" ,????);

    Any advice would be much appreciated.

    K 1 Reply Last reply
    0
    • Q QT_fan

      I am using the following code to set the text on a button:

      ui->MyButton_1->setText("ON");

      I would like to create a function to do this with two arguments; a string for the text and the button name. No problem for the text but how can I pass the button name to the function?
      The function would look like this:

      void MainWindow::SetButtonText(QString S, ????)
      {
      ????->setText(S);
      }
      The call would be like this:
      setText("ON" ,????);

      Any advice would be much appreciated.

      K Offline
      K Offline
      koahnig
      wrote on last edited by koahnig
      #2

      @QT_fan

      void setPBText ( const QString & txt, QPushButton * pb)
      {
            pb->setText ( txt );
      }
      

      Could be used as in

      setPBText ( "ON", ui->MyButton_1 );
      

      Please check it out

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      2
      • Q Offline
        Q Offline
        QT_fan
        wrote on last edited by
        #3

        Thanks for your response. I tried it and got this error message:

        C:\Qt\Apps\Practice_4\mainwindow.cpp:32: error: undefined reference to `MainWindow::setPBText(QString const&, QPushButton*)'

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

          Hi,

          Did you add that function to your QMainWindow subclass ?

          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
          1
          • Q Offline
            Q Offline
            QT_fan
            wrote on last edited by
            #5

            I placed the function in the mainWindow file with a prototype in the header file. I did this since this is where Qt designer put the button slot function. Wrong place? Anyway, I moved it to the header file and it now works! Once again, thank you for your assistance.

            K 1 Reply Last reply
            0
            • Q QT_fan

              I placed the function in the mainWindow file with a prototype in the header file. I did this since this is where Qt designer put the button slot function. Wrong place? Anyway, I moved it to the header file and it now works! Once again, thank you for your assistance.

              K Offline
              K Offline
              koahnig
              wrote on last edited by koahnig
              #6

              @QT_fan

              The short routine I have suggested is a plain function. In a header would need to have something like

              #ifndef SET_PB_TEXT_INCLUDED 
              #define SET_PB_TEXT_INCLUDED  
              
              #include <QString>
              #include <QPushButton>
              
              void setPBText ( const QString & txt, QPushButton * pb);
              #endif 
              

              Certainly you can also include it also in a header file and in a class definition as you have done. However, you would have to add the scope of the class when adding the source somewhere. In your case the source snippet would be more like:

              void MainWindow::setPBText ( const QString & txt, QPushButton * pb)
              {
                    pb->setText ( txt );
              }
              

              No offence, but I suggest that you have also a look to some C++ basics. Without knowing some C/C++ basics you are making your life harder than it needs to be with Qt.

              PS: all source is brain to keyboard and requires some testing and checking.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              3

              • Login

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