[solved] Modify form through main.cpp
hi,
i’m sorry if my english is not good enough but i’m italian.
I just started learning QT Qt and so, when i opened Qt Creator for the first time, i created a new QT Qt Gui Application.
I added in the form a QPushButton, and if i build the program it runs correctly, but i don’t understand how to call the QPushButton (whose name is Btest) in the main.cpp.
4 replies
Hi,
Welcome to the forum,
For your question , its not a good idea to access the MainWindow(form) components in main.cpp as you can easily access ui components in your mainwindow class and do the required operations.
But in order to access ui components in main.cpp you can try something like.
main.cpp
- int main(int argc, char *argv[])
- {
- MainWindow window;
- objectList = window.children(); //returns a list of child objects.
- qDebug()<<obj->objectName();
- {
- //do something
- }
- }
- window.show();
- return app.exec();
- }
Other way is :
- #include "ui_MainWindow.h"
- int main(int argc, char *argv[])
- {
- Ui::MainWindow window;
- window.Btest->setText("Hello World");
- //do something.......
- return a.exec();
- }
But I still recommend to keep the main.cpp clean.
I’m not quite sure what you mean by call, but
- if you want to get a pointer to the button in
MainWindowuseui->Btest - if you want to get a pointer to the button in
mainusemainWindow.findChild<QPushButton*>(“Btest”) - if you want to react on the push button beeing clicked either use
connect(…)or create a slot calledon_Btest_clicked()
You must log in to post a reply. Not a member yet? Register here!





