Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[New to QT] Possible to refer to UI objects with dot (.) notation?
oblivioncth last edited by oblivioncth
I am just beginning to learn how to use Qt and have a question that involves my preferences (due to GUI work in MATLAB) with editing UI elements via their corresponding functions/slots in the source code. I may be slightly wrong in some of what I say but as I mentioned I am very knew to this C++ extension.
So, if I create a UI element in the code that is not a full window and does not have its own ".ui" file, such as a message box, I can refer to it using dot notation.
QMessageBox Debug; Debug.setText("Test"); Debug.exec();
But it seems to be that this doesn't work when referring to objects within a window and I have to use this notation instead:
//Example "Line Edit" object in mainwindow.cpp ui->LnEditImgPath->setText("Test");
In the above I am trying to set the text of a read-only line edit object that I created in the designer. This works.
Is there any kind of setting/tweak I can use in the Creator/Designer so that I can just do:
As is obvious the above doesn't work. It seems like the main difference is that one is just a lonesome "detached" Message Box, while the other is an object under the type "ui" which is why I guess you have to use that notation.
Ultimately it isn't a huge deal, as I can get used to this new convention over time. However, I am used to MATLAB that uses the dot notation for almost every UI element and I get a little OCD about the lack of consistency between the different types of UI elements in Qt (again it seems that way so far, perhaps there is a way to refer to them the same way consistently, that is why I am asking). What I mean, is that I would also mind less if the Message Box was referred to using the "ui ->...->..." notation. I would just like them to be consistent, and prefer the dot notation method.
Any tips or help is appreciated.
JohanSolo last edited by
A dot is used when you have a class instance, an arrow
->when you have a pointer. Now, for the scope of declared objects should be more than the declaring method, you are forced to use pointers, and therefore to use the dereferencing (arrow) operator.
In Matlab you only have references, this is why you are out of your zone of comfort. As you seem to be unfamiliar with this aspect of C++ (which is not at all Qt-specific), you might first want to study a bit more C++ on its own.
oblivioncth last edited by
Ah ok, so it is due to the organization of C++ not Qt, and like you said, the difference of a class instances and pointers.
It has been long time since I've had to use C++ and I never did anything too advance with it other than a text-based adventure game. I generally am more focused on hardware oriented programing such as with C and Verilog, and MATLAB is the only more software (excluding Arduino interaction) oriented language I have decent experience with.
Thank you for pointing out why there needs to be a difference. I wasn't familar with pointer usage in C++.