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 do I acces UI components inside a function which I called with a slot function?

How do I acces UI components inside a function which I called with a slot function?

Scheduled Pinned Locked Moved Solved General and Desktop
qt5ui objectguidesign editorslots
7 Posts 3 Posters 1.6k 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.
  • R Offline
    R Offline
    Robin_Khn
    wrote on 27 Jun 2020, 12:27 last edited by
    #1

    So I have this slot-function called "on_pushButtonNext_clicked()", which is executed when a certain button in my GUI is clicked. And in that slot-function I call a normal function called "add_immediate(...)". And in that function I want to acces a text browser on the ui. But it doesnt work. Through some other thread I read that I needed to make that "add_immediate(...)" function a member of the MainWindow - class in order to gain access to its private member "ui", which I need to acces the components on my GUI. Turns out, this doesn't work either, I get the error: C2065: "ui": undeclared identifier.
    But why do I get this error? The function is a member function, so I should have access to it.
    Here is my code (don't bother with the parameters off the function, they don't matter here):

    the slot-function:

    void MainWindow::on_pushButtonNext_clicked(){
    //...
    switch(i_reg[0]) {
    //...
    case ('3'): add_immediate (ram, i_reg,regfile);
    break;
    //...
    }}

    the "add_immediate"-function:

    void MainWindow::add_immediate ( char ram[MAX_A][4], char i_reg[4], int regfile[8] ){
    //...
    ui->reg_a->insertPlainText(QString::number(i0));
    //here i get the C2065 error, stating that ui is an undeclared identifier; reg_a is a textbrowser widget
    }

    Can anyone help me out here?

    J 1 Reply Last reply 27 Jun 2020, 13:12
    0
    • R Robin_Khn
      27 Jun 2020, 13:13

      @mrjj yes, i do have that exact class. And calling the ui with MainWindow::ui doesn't work either, because ui is not static. So I really wonder why the ui is not getting recognized...

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 27 Jun 2020, 13:16 last edited by mrjj
      #5

      @Robin_Khn

      Well if you have Ui::MainWindow *ui, it must work. maybe its qmake that gives you issues.
      Try go to your build folder and delete everything and then rebuild all.

      The syntax is ui-> as you say its not static.

      So please try totally clean and rebuild and if its still not working we have to find out what differs from normal.
      You are using Creator as IDE and mingw compiler ?

      1 Reply Last reply
      3
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 27 Jun 2020, 13:03 last edited by mrjj
        #2

        Hi and welcome to the forums.
        For a default GUI project, anything you place on the UI form is accessible via
        ui->name

        So is this a normal project and you do have the ui struct ?

        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        ....
        private:
            Ui::MainWindow *ui; <<<< this one
        
        R 1 Reply Last reply 27 Jun 2020, 13:13
        1
        • R Robin_Khn
          27 Jun 2020, 12:27

          So I have this slot-function called "on_pushButtonNext_clicked()", which is executed when a certain button in my GUI is clicked. And in that slot-function I call a normal function called "add_immediate(...)". And in that function I want to acces a text browser on the ui. But it doesnt work. Through some other thread I read that I needed to make that "add_immediate(...)" function a member of the MainWindow - class in order to gain access to its private member "ui", which I need to acces the components on my GUI. Turns out, this doesn't work either, I get the error: C2065: "ui": undeclared identifier.
          But why do I get this error? The function is a member function, so I should have access to it.
          Here is my code (don't bother with the parameters off the function, they don't matter here):

          the slot-function:

          void MainWindow::on_pushButtonNext_clicked(){
          //...
          switch(i_reg[0]) {
          //...
          case ('3'): add_immediate (ram, i_reg,regfile);
          break;
          //...
          }}

          the "add_immediate"-function:

          void MainWindow::add_immediate ( char ram[MAX_A][4], char i_reg[4], int regfile[8] ){
          //...
          ui->reg_a->insertPlainText(QString::number(i0));
          //here i get the C2065 error, stating that ui is an undeclared identifier; reg_a is a textbrowser widget
          }

          Can anyone help me out here?

          J Offline
          J Offline
          JonB
          wrote on 27 Jun 2020, 13:12 last edited by JonB
          #3

          @Robin_Khn
          As @mrjj says. My guess: you didn't design this in Creator at all, so you don't have a ui variable anywhere? Which Creator would give you, and the post you looked at was for a Creator project?

          R 1 Reply Last reply 27 Jun 2020, 13:24
          1
          • M mrjj
            27 Jun 2020, 13:03

            Hi and welcome to the forums.
            For a default GUI project, anything you place on the UI form is accessible via
            ui->name

            So is this a normal project and you do have the ui struct ?

            class MainWindow : public QMainWindow
            {
                Q_OBJECT
            ....
            private:
                Ui::MainWindow *ui; <<<< this one
            
            R Offline
            R Offline
            Robin_Khn
            wrote on 27 Jun 2020, 13:13 last edited by
            #4

            @mrjj yes, i do have that exact class. And calling the ui with MainWindow::ui doesn't work either, because ui is not static. So I really wonder why the ui is not getting recognized...

            M 1 Reply Last reply 27 Jun 2020, 13:16
            0
            • R Robin_Khn
              27 Jun 2020, 13:13

              @mrjj yes, i do have that exact class. And calling the ui with MainWindow::ui doesn't work either, because ui is not static. So I really wonder why the ui is not getting recognized...

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 27 Jun 2020, 13:16 last edited by mrjj
              #5

              @Robin_Khn

              Well if you have Ui::MainWindow *ui, it must work. maybe its qmake that gives you issues.
              Try go to your build folder and delete everything and then rebuild all.

              The syntax is ui-> as you say its not static.

              So please try totally clean and rebuild and if its still not working we have to find out what differs from normal.
              You are using Creator as IDE and mingw compiler ?

              1 Reply Last reply
              3
              • J JonB
                27 Jun 2020, 13:12

                @Robin_Khn
                As @mrjj says. My guess: you didn't design this in Creator at all, so you don't have a ui variable anywhere? Which Creator would give you, and the post you looked at was for a Creator project?

                R Offline
                R Offline
                Robin_Khn
                wrote on 27 Jun 2020, 13:24 last edited by
                #6

                @mrjj thx a lot, cleaning and rebuilding solved my problem.

                M 1 Reply Last reply 27 Jun 2020, 13:31
                1
                • R Robin_Khn
                  27 Jun 2020, 13:24

                  @mrjj thx a lot, cleaning and rebuilding solved my problem.

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 27 Jun 2020, 13:31 last edited by mrjj
                  #7

                  @Robin_Khn
                  Ok super.
                  So its just qmake/moc that is a little slow sometimes.

                  So when you add something to the UI from and go back to the code, you can press ctrl+shift+b (build file) it knows the new Widget right away.

                  1 Reply Last reply
                  1

                  3/7

                  27 Jun 2020, 13:12

                  • Login

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