How to write code to run "inside" a form
-
I,
I am using qt creator and know how to make some interface, going to the slots, write actions in a push buttons, chnage labels...
But what i search is how to run a code at the opening of a form.For exemple the cpp file of the form is :
#include "tca_initdlg.h" #include "ui_tca_initdlg.h" TCA_InitDlg::TCA_InitDlg(QWidget *parent) : QDialog(parent), ui(new Ui::TCA_InitDlg) { ui->setupUi(this); } TCA_InitDlg::~TCA_InitDlg() { delete ui; }
Kind of lost because in the few works i was in with cpp the files looked more like this :
#include <iostream> using namespace std; int main() { cout << "hello"; return 0; }
With the code inside a int main(){}
[Edit Added code formatting] Please surround code with ```.
-
As opposed to the linear execution exemplified in your second code snippet Qt widget programming is event driven.
What this means is that there's just a little "starter" sequential code in the main like this:int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
The
exec()
starts what is called an event loop. The app spins in circles waiting for "stuff" to happen. By "stuff" I mean keyboard interaction, mouse moves, window resizing, OS demanding painting the window etc.
What you do then is write code that handles these events, either by overriding the various *event() methods of the widget or by connecting to signals emitted by it.So where you put your code depends on what you actually want to do. You can still run some sequential code in the main before you start the loop. If you want to do something when the window is created then add your code to the widget constructor. If you want to do something when the window is shown then override showEvent() (just remember that it will run multiple times whenever it is shown e.g. minimized and then restored).
-
Ok, i am already working in a main.cpp with something like that :
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); sleep(5); w.hide(); TCA_InitDlg w2; w2.show(); return a.exec(); }
So if i want to execute something when for example TCA_InitDlg is on screen i should use a event() method and put the event() method in the cpp of the form TCA_InitDlg, if i understand well.
Must seems like a easy question but i have near no knowledge when it comes to c/c++ (not my work expertise) and i am asked to port an application from a windows target made with visual studio to a linux target with qt creator, and i have not worked on the code from the first version... Not really simple for me.
-
Yeah, it's hard to switch your thinking, but keep in mind that you should stop thinking linearly e.g "I first do X, then Y and then Z". Instead try to think in these terms: "when X happens I respond to it with Y. When W happens I respond with Z".
Also consider any kind of blocking bad e.g.
sleep(5);
will freeze your ui. Sleeping is not something you will see often in UI programming with Qt and is almost always a bad choice.As to "execute something when for example TCA_InitDlg is on screen" what do you mean by that? Do you want to execute something the moment it is shown (remember the event driven model) or do some continuous (e.g. a loop) calculation while the widget is visible?
-
Hi wowy,
to add to Chris Kawa´s posts and answer the question from your first post :
"But what i search is how to run a code at the opening of a form."
With form i guess u mean the dialog or widget u want to show. This is done in the constructor of your class. For your example you would just add code after
ui->setupUi(this);
. This gets called every time you construct aTCA_InitDlg
.