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 reference code created QPushButton?
QtWS25 Last Chance

How to reference code created QPushButton?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 715 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.
  • L Offline
    L Offline
    legitnameyo
    wrote on last edited by
    #1

    I've created a button like this

    QPushButton *btn = new QPushButton(ui->frame_btn);
    

    now, how do I reference this? I usually do

    ui->btn->move(x, y);
    

    but there is no btn in ui, there is no btn in frame_btn, there is no btn in ui->frame_btn->btn. Where is it and how do I reference it?

    JonBJ Pablo J. RoginaP 2 Replies Last reply
    0
    • L legitnameyo

      I've created a button like this

      QPushButton *btn = new QPushButton(ui->frame_btn);
      

      now, how do I reference this? I usually do

      ui->btn->move(x, y);
      

      but there is no btn in ui, there is no btn in frame_btn, there is no btn in ui->frame_btn->btn. Where is it and how do I reference it?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @legitnameyo
      If you haven't added it into ui, your code as shown is just creating a variable (local to function by the look of it) so you'd just reference it via plain btn (assuming you're in the function where you've created it).

      1 Reply Last reply
      1
      • L legitnameyo

        I've created a button like this

        QPushButton *btn = new QPushButton(ui->frame_btn);
        

        now, how do I reference this? I usually do

        ui->btn->move(x, y);
        

        but there is no btn in ui, there is no btn in frame_btn, there is no btn in ui->frame_btn->btn. Where is it and how do I reference it?

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @legitnameyo said in How to reference code created QPushButton?:

        Where is it
        it'll live in the scope of where you create it (i.e. class, method). You need to provide a bigger code snippet to be sure where you're creating it

        and how do I reference it

        btn->move(x, y);
        

        you've just created a pointer to a QPushButton

        there is no btn in frame_btn

        No, frame_btn is just the parent object of your btn object

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        1
        • L Offline
          L Offline
          legitnameyo
          wrote on last edited by
          #4

          I created it where basically everything is located, in the main widget. I am referencing it from another function.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            legitnameyo
            wrote on last edited by legitnameyo
            #5

            This is what I want to do

            test::test(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::test)
            {
                ui->setupUi(this);
                QPushButton *btn = new QPushButton(ui->frame_btn);
            }
            
            void test::move_btn() {
                ui->frame_btn->btn->move(25, 25); // there is no "btn" in ui->frame_btn, gives error
            }
            

            is there a way to reference the button???

            Cobra91151C Pablo J. RoginaP 2 Replies Last reply
            0
            • L legitnameyo

              This is what I want to do

              test::test(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::test)
              {
                  ui->setupUi(this);
                  QPushButton *btn = new QPushButton(ui->frame_btn);
              }
              
              void test::move_btn() {
                  ui->frame_btn->btn->move(25, 25); // there is no "btn" in ui->frame_btn, gives error
              }
              

              is there a way to reference the button???

              Cobra91151C Offline
              Cobra91151C Offline
              Cobra91151
              wrote on last edited by Cobra91151
              #6

              @legitnameyo

              Hi! You can add QPushButton *btn to your header file, for example - test.h (toprivate section). Then you can use your button everywhere in your class. But don't forget this is a pointer, so you should call btn->deleteLater() or delete btn in the destructor to free resources, so no memory leak occur in your application.

              Also note by using Forms it creates the namespace - ui which handles the access/destruction of your widgets, added on the graphical form.

              Happy coding!

              1 Reply Last reply
              1
              • L legitnameyo

                This is what I want to do

                test::test(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::test)
                {
                    ui->setupUi(this);
                    QPushButton *btn = new QPushButton(ui->frame_btn);
                }
                
                void test::move_btn() {
                    ui->frame_btn->btn->move(25, 25); // there is no "btn" in ui->frame_btn, gives error
                }
                

                is there a way to reference the button???

                Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on last edited by Pablo J. Rogina
                #7

                @legitnameyo

                is there a way to reference the button???

                as mentioned by @Cobra91151 make it a member of your class test (BTW, it's a common practice to name your Classes using uppercase

                you should call btn->deleteLater() or delete btn in the destructor to free resources

                No need to do that you if you make the class the parent of the button, i.e. (assuming btn is a member of class Test

                test.h

                ...
                private:
                QPushButton *btn;
                ...
                

                test.cpp

                ...
                Test::Test(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::test)
                {
                    ui->setupUi(this);
                    btn = new QPushButton(this);
                }
                ...
                

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                2
                • L Offline
                  L Offline
                  legitnameyo
                  wrote on last edited by
                  #8

                  Doing what @Pablo-J-Rogina said made it possible to reference it, but when I do and try to move it, the program crashes:

                  The program has unexpectedly finished.
                  The process was ended forcefully.
                  

                  I referenced it as

                  btn->move(25, 25);
                  
                  Cobra91151C 1 Reply Last reply
                  0
                  • L legitnameyo

                    Doing what @Pablo-J-Rogina said made it possible to reference it, but when I do and try to move it, the program crashes:

                    The program has unexpectedly finished.
                    The process was ended forcefully.
                    

                    I referenced it as

                    btn->move(25, 25);
                    
                    Cobra91151C Offline
                    Cobra91151C Offline
                    Cobra91151
                    wrote on last edited by
                    #9

                    @legitnameyo

                    I think your program crashes because it initializes two pointers. Remove QPushButton * from your constructor. It should be:

                    test.h

                    private:
                    QPushButton *btn;
                    

                    test.cpp

                    Test::Test(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::test)
                    {
                        ui->setupUi(this);
                        btn = new QPushButton(this);
                    }
                    

                    Also try to attach the debugger and run your program, it should detect the issue.

                    1 Reply Last reply
                    2

                    • Login

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