Link from my program and .ui file



  • Dear all:
    I'm a newer in QT. I have a simple question. I added a item into my .ui file, why i can't use it's name of object in my program, do I miss something??Thanks for any answer.


  • Moderators

    How did you try to use it?
    uic generated objects are accessible through the ui member, for example: if you create a label called myLabel in the editor it's accessible through ui.myLabel



  • Firstly, thanks for your answer.Let me say detailed, I have "main.cpp", "mainwindow.cpp", "mainwindow.h
    ,"mainwindow.ui" now and I add a button called "pushButton" is located on [objectName] in "mainwindow.ui". Now, I want to access this button, but when I try to find name of this button in "main.cpp", I don't know what I should type, I can't find it in reference of typing.Can you give me some suggestions !? Thanks.


  • Lifetime Qt Champion

    Hi,

    You won't find in main.cpp, the ui is related to your mainwindow.* files, it's there that you will code for it.

    In main.cpp you will essentially instantiate and show a MainWindow.

    Hope it helps



  • Thanks. So If I want to describe some behavior of this button, like singal and slot, which file I can do that?? I found Items in .ui file are defined in "ui_mainwindow.h", should I write behavior of this button in this file!?


  • Moderators

    No, the ui_mainwindow.h is generated by uic, and any changes you make there will be lost after recompiling.
    All your class related code should be in mainwindow.h and .cpp.
    If you want to expose some ui elements to outside classes create getters for them in your mainwindow class eg.
    @
    QLabel* getMyLabel() { return ui.myLabel; }
    @



  • Thank you :)). I am successful.But I think It's a little inconvenient that compare to C# in .net framework.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.