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?
Forum Updated to NodeBB v4.3 + New Features

How to access UI from another function?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.3k 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 9 Apr 2018, 06:16 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 9 Apr 2018, 06:39
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 9 Apr 2018, 06:39 last edited by SGaist 4 Sept 2018, 06:40
      #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 9 Apr 2018, 06:40
      2
      • M MasterBlade
        9 Apr 2018, 06:16

        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 9 Apr 2018, 06:39 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
        • S SGaist
          9 Apr 2018, 06:39

          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 9 Apr 2018, 06:40 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
          • S SGaist
            9 Apr 2018, 06:39

            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 9 Apr 2018, 08:57 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 9 Apr 2018, 09:53 last edited by
              #6

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

              J 1 Reply Last reply 9 Apr 2018, 10:11
              0
              • M MasterBlade
                9 Apr 2018, 09:53

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

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 9 Apr 2018, 10:11 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

                1/7

                9 Apr 2018, 06:16

                • Login

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