Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to reference code created QPushButton?



  • 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?



  • @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).



  • @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



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



  • 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???



  • @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!



  • @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);
    }
    ...
    


  • 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);
    


  • @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.


Log in to reply