About the ui object and what it holds



  • Hey guys , i'm using the newest qt creator , and i'm new to this I got 3 question , if you guys can help me with it ^^

    1- the ui object , when I try to access classes , what does it include ? and can I access stuff without the ui object ?

    2- when I try to do this " ui->pushbutton->settext(...);
    is this going to set all push buttons to the name that I've put in the arguments ?

    3- what's the difference between renaming it in the edit widget and the function above ?



  • HI,
    Welcome to Qt-project forum.
    If using designer the ui object holds all your GUI elements that you created with designer. The MOC compiler will generate a mainwindow_ui.h file in the background that will construct a ui class object.
    In the beginning of your main window class you will see @ui(new Ui::MainWindow)@ which is the same as: Setup the pointer ui to the constructor of the Ui::MainWindow class
    The @ui->setupUi(this)@ will allocate all needed memory for the GUI elements.
    1: Without the ui object you can't access your GUI elements!!
    2: No, it will only set the text of the pushButton with the objectName: pushButton. In designer the next pushButton will get ObjectName: pushButton_2 and so on and on. It is my recommendation to rename them
    3: Renaming the widget in designer is compile time, setting the text in the setText() function is runtime, but the result is the same.



  • Thanks man , so about the pushbutton , it's all depends on the order ? for example if I put two pushbutton , and called settext("ok"), and the next one settext(" not ok ") , the first one will get the name " ok " and second " not ok " ? and we can apply it on things beside settext ?



  • ah , I just tried adding a second one and there was " pushButton_2 " :D

    thank you ^^



  • ;-) Like in any C++ class you may not have two the same identifiers
    What you could do, but that might be a bit to advanced for now is to get the ui pointer and get all children (QObject function) and do a dynamic cast to a QPushButton, if succesfull do the setText function with a counter value. If you really need to do it run time.



  • Thanks man , I'm really upset , when I tried to create a simple program I couldn't have done it , it seems that it still confuses me I just finished learning about signal and slot :(

    is there a website that gives you a simple problems that you can solve ?



  • This forum is a good start if you encounter problems. There are more then enough guys here to help you.
    There is a 'how to learn Qt' doc on this site somewhere if you need some assistance.



    1. Yes.!! U can access the all the widgets or stuffs with out using Ui. but for that u have to do some changes in header file as well as .cpp file.

    2. No. It do not set all push buttons to the name that u've put in the arguments

    3. The difference between renaming and settext() method is that when u use rename . It change name of the object but settext() just use to write any text on it. So u can write same text on two buttons but name of the two buttons must be different.



  • When U will write "Ui->" then it automatically show the name of the all objects.


Log in to reply
 

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