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 access UI from another function?

How to access UI from another function?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.4k 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.
  • M Offline
    M Offline
    MasterBlade
    wrote on last edited by
    #1

    Hello I'm new to Qt. I created a project that called another function in mainwindow. It looks like this

    void Gameplay::on_startgame_clicked()
    {
    Game_Initialize();

    }

    However when I tried to access ui in the new function like the following. It said that ui is not registered. What should I do? Many thanks!

    void Game_Initialize(){
    ui->deg->setText("D: ");
    }

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

      Hi and welcome to devnet,

      Your Game_Initialized method is just a free function. Make it a member of your Gameplay class. Or since the method only updates the content of your UI, write that code directly in on_startgame_clicked.

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

      A M 2 Replies Last reply
      2
      • M MasterBlade

        Hello I'm new to Qt. I created a project that called another function in mainwindow. It looks like this

        void Gameplay::on_startgame_clicked()
        {
        Game_Initialize();

        }

        However when I tried to access ui in the new function like the following. It said that ui is not registered. What should I do? Many thanks!

        void Game_Initialize(){
        ui->deg->setText("D: ");
        }

        A Offline
        A Offline
        ambershark
        wrote on last edited by
        #3

        @MasterBlade From the code you showed, the Game_Initialize() function does not look like it is part of the Gameplay class which means it has no idea what ui is.

        So that's either a typo and you meant to do void Gameplay::Game_Initialize() or it's outside the class intentionally, in which case you will need to pass the ui pointer to it. However be aware that would be a really bad way to use Qt. Hopefully it's the former and you meant it to be in the class.

        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

        1 Reply Last reply
        2
        • SGaistS SGaist

          Hi and welcome to devnet,

          Your Game_Initialized method is just a free function. Make it a member of your Gameplay class. Or since the method only updates the content of your UI, write that code directly in on_startgame_clicked.

          A Offline
          A Offline
          ambershark
          wrote on last edited by
          #4

          @SGaist said in How to access UI from another function?:

          Hi and welcome to devnet,

          Your Game_Initialized method is just a free function. Make it a member of your Gameplay class.

          Oops @SGaist beat me, lol.

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          1 Reply Last reply
          1
          • SGaistS SGaist

            Hi and welcome to devnet,

            Your Game_Initialized method is just a free function. Make it a member of your Gameplay class. Or since the method only updates the content of your UI, write that code directly in on_startgame_clicked.

            M Offline
            M Offline
            MasterBlade
            wrote on last edited by
            #5

            @SGaist

            @SGaist said in How to access UI from another function?:

            Hi and welcome to devnet,

            Your Game_Initialized method is just a free function. Make it a member of your Gameplay class. Or since the method only updates the content of your UI, write that code directly in on_startgame_clicked.

            Problem Solved. Thanks very much for the help!

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MasterBlade
              wrote on last edited by
              #6

              How can I mark this as solved? Sorry I'm new here.

              jsulmJ 1 Reply Last reply
              0
              • M MasterBlade

                How can I mark this as solved? Sorry I'm new here.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @MasterBlade said in How to access UI from another function?:

                How can I mark this as solved?

                Bottom right corner "Topic Tools"

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                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