[Solved] Accessing UI components from another file
-
Declaring things public is not the way to solve these issues. You're breaking encapsulation, and that will come back to haunt you. It means that you are producing dependencies between components of your application that should be independent, ultimately leading to spaghetti code.
Why does another class need direct access to the UI components of the mainwindow?
-
Thank you Andre for your reply.
I do this for simplicity reasons. As I am a beginner in qt (I come from c++builder), I learn from this "simple" example. In my application, I will have one main window and at least three different forms with some widgets in them. One form has to access another forms components: pass / read value to/from Ui components. I have read tons of threads in many forums but still cant find my way.
Thanks in advance for any help. -
How come no one is recommending "signals and slots":http://developer.qt.nokia.com/doc/qt-4.8/signalsandslots.html? :)
Use signals and slots instead of breaking your encapsulation, just add slots for the functionality you want accessible through your main window, and connect those to the signals of the classes that need to use that functionality. This way your forms don't have to know and be exposed naked to each other, just the slot interface of your mainwindow.
-
[quote author="ddriver" date="1330360312"]How come no one is recommending "signals and slots":http://developer.qt.nokia.com/doc/qt-4.8/signalsandslots.html? :) .[/quote]
Because it is important to first understand the actual problem, before suggesting a suggestion. As the problem is not clear yet, it is a bit early to suggest that signals & slots can be a fix for it. -
[quote author="andry_gasy" date="1330360010"]Thank you Andre for your reply.
I do this for simplicity reasons. As I am a beginner in qt (I come from c++builder), I learn from this "simple" example. In my application, I will have one main window and at least three different forms with some widgets in them. One form has to access another forms components: pass / read value to/from Ui components. I have read tons of threads in many forums but still cant find my way.
Thanks in advance for any help.[/quote]Well, the point is, exposing the inner workings of a class to other classes does not make your applications simpler. It will make them more complicated in the end. Think of C++ classes as relatively independent parts of a complex organization. They provide a service to other classes. It is very, very important to be clear about what service that is exactly, but not to bother the users of the class with details on how it performs that service. That is: you have to design an API for each of your classes that gives a clear image of what the class can do, but that not exposes how it does that.
What you are suggesting to do, is to expose the guts of your mainwindow class to everyone. That goes directly against the idea above. Instead, try to think of what service your mainwindow class should provide to the rest of your application, and create and use that. That will make sure that if you later on decide to reorganize your mainwindow, the rest of your code will still work as long as you keep the API on the surface of your mainwindow class constant.
BTW: this is basic OOP stuff, nothing Qt specific.
-
Thank you all for your reply.
I went through the link ddriver was suggesting and was thinking about all Andre's lecture :)
Well, for a C++builder user like me, it seems quite complicated. I totally agree with Andre but for me gui classes must be some kind of "public" classes for other classes in the project. All other classes must have access to ui components because they have to "show" their results to the user. For small project (few visual components), signals and slots are still ok, but for many forms with many widgets in them, it gets messy.
As i said, I will have many forms with many widgets. I put all the calculation stuff in separate files. According to results of these calculations, tasks as following will be done on the gui: changing contents of text/line edits, labels, adding rows to tables, updating tables/combo boxes contents,... So again I think to allow these calculation files to have direct access to ui components would be the easiest way.
My question is then still the same: is there a way to make thing like
@form->ui->lineEdit->setText("some text");
@from a file outside the form?
Thanks a lot for trying to help. I really appreciate. -
Last attempt by me: I would really, really not allow direct access to the widgets by classes that don't own them. Your calculator classes should not need to know how their results are used. Instead, they could just announce their results via a signal, and you can connect those signals to the relevant slots to set the values on the widgets. Perhaps you can even make that connection to the widgets directly, if the mainWindow has access to the calculator instances.
GUI classes do not form the exception for having to provide a good, clear API.
-
Really sorry for the very late reply as I was completely absorbed by the job.
Thanks again for all your help. I totally agree with Andre for "his" approach. I've had time to learn about signal and slot. I will do it the "right" way when time allows me but for the moment, I just used the "dirty" way: setting the Ui::Mainwindow as public and it's working well for my small project.
All the best! -
Sorry for replying the very old thread, just adding the information missing for Qt newbies like me was some time ago, who came down here in hunting for "proper" fast access to form elements.
The cannonical form for "fast, just clean" access to Qt form elements "out of box" can be:
(It took looong time to me to elaborate this "simple" line of code. IMHO it should be printed ready somewhere up in the Qt intro examples.)w.findChild<QTextBrowser*>("FormElement")->setText("TEST");
where
- w is the MainWindow object
- QTextBrowser is type of the element you want to access
- "FormElement" is the name of the element you want to access
(You can omitt it from parameter list if there is only one element of the given type. findChild will return it by default.) - setText is the method you want to call.
-
@andry_gasy
Can you please tell me how you have resolved this problem, I am also facing same issue -
@Savita-shinde
Hello and welcome.@andry_gasy posted 10 years ago, so they are not going to be here now! I don't know what your issue is that this thread has not solved, but I suggest you open your own topic describing your question if you want to ask about it.