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

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



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


  • Lifetime Qt Champion

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


  • Lifetime Qt Champion

    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
    


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



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


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

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