Looking to call a function withour user intervention
-
Complete noob, learning as I go, creating an QWidget desktop application by trial and error. My application (program?) is a databased application and will be using SQLite. As I do not want to have to include a data file with the application, I want to create the db file and the necessary tables programmatically. I also want to give the user opportunity to populate the tables with sample data.
I want the program to start, at the startup page, check for the existence if the database, check for the existence of the tables, check to see if the tables are empty and if so, give the user the opportunity to insert the sample data.
Now, I have written a function to do all this, but! I can't get the function to run without having the user click on something. I've move the call to the function call around in different places and doing so have created some awful results! Including having a copy of the program running that can't be accessed until the user logs out!
So, after all this my question is: What can I use to call the function after the program has loaded and settled down waiting for user input but without user input?
Perhaps my question is too long, but I wanted to avoid try to help but have to ask me all the questions I just answered.
-
Complete noob, learning as I go, creating an QWidget desktop application by trial and error. My application (program?) is a databased application and will be using SQLite. As I do not want to have to include a data file with the application, I want to create the db file and the necessary tables programmatically. I also want to give the user opportunity to populate the tables with sample data.
I want the program to start, at the startup page, check for the existence if the database, check for the existence of the tables, check to see if the tables are empty and if so, give the user the opportunity to insert the sample data.
Now, I have written a function to do all this, but! I can't get the function to run without having the user click on something. I've move the call to the function call around in different places and doing so have created some awful results! Including having a copy of the program running that can't be accessed until the user logs out!
So, after all this my question is: What can I use to call the function after the program has loaded and settled down waiting for user input but without user input?
Perhaps my question is too long, but I wanted to avoid try to help but have to ask me all the questions I just answered.
@bart.hollis
You can write your code to execute just before your call toQApplication::exec()
(or whatever runs your main event loop).During that time you can certainly do *non-*interactive things you mention, like looking at the database etc.
When you come to:
I also want to give the user opportunity to populate the tables with sample data.
This is user interaction. You can still put up a
QDialog
(plusQMessageBox
etc.) to ask user a question and act on it, before you have got as far as entering the maim event loop. -
Thank you for your reply.
Ah yes. I had tried that but discarded it as a QMessageBox poping up outside of, or before the main window appears just looks wrong to me. I would like the UI to settle before doing this. -
Thank you for your reply.
Ah yes. I had tried that but discarded it as a QMessageBox poping up outside of, or before the main window appears just looks wrong to me. I would like the UI to settle before doing this.@bart.hollis
If that's what you want, you could set off a one-shotQTimer
(just prior toQApplication::exec()
) to bring up your UI after a fraction of a second.EDIT: Or, maybe
QEvent::Show
inQMainWindow::event(QEvent *event)
(orQWidget::showEvent
) will be raised when your main window has been shown.There are all sorts of suggestions in https://stackoverflow.com/questions/14356121/how-to-call-function-after-window-is-shown
-
Hi,
Some something like:
- "show widgets"
- Start method while widgets are visible
?
-
Thank you for your reply.
Ah yes. I had tried that but discarded it as a QMessageBox poping up outside of, or before the main window appears just looks wrong to me. I would like the UI to settle before doing this.@bart.hollis What's making the main window appear? If you want to be able to tell the main window to either show data or prompt the user about populating it with sample data as soon as it opens, that might be a good place to put the check for the data.
-
@SGaist said in Looking to call a function withour user intervention:
Hi,
Some something like:
- "show widgets"
- Start method while widgets are visible
?
Yup! That's what I want to do, but can't seem to find something, or one of the widgets that will trigger the function. Wish I could get into the actual loop! :)
-
Use a
QTimer::singleShot
with a value of 0 for the delay, then you'll have your GUI running and the method called right after the first round of the event loop. -
Use a
QTimer::singleShot
with a value of 0 for the delay, then you'll have your GUI running and the method called right after the first round of the event loop.@SGaist said in Looking to call a function withour user intervention:
Use a
QTimer::singleShot
with a value of 0 for the delay, then you'll have your GUI running and the method called right after the first round of the event loop.Could you please give me more detail as how to implement this?
-
Same as usual except that you use 0 as timeout value and you call that as the last thing in your constructor.
-
@SGaist said in Looking to call a function withour user intervention:
Use a
QTimer::singleShot
with a value of 0 for the delay, then you'll have your GUI running and the method called right after the first round of the event loop.Could you please give me more detail as how to implement this?
@bart.hollis
I suggested you look at https://stackoverflow.com/a/38749911/489865 or which does actually give code for usingQTimer::singleShot
. -
Same as usual except that you use 0 as timeout value and you call that as the last thing in your constructor.
@SGaist said in Looking to call a function withour user intervention:
Same as usual except that you use 0 as timeout value and you call that as the last thing in your constructor.
That would be the MainWindow constructor?
Then, do I understand correctly that I use the slot mentioned to trigger my function?
From Docs: QTimer::singleShot(200, this, SLOT(updateCaption()));
-
SGaist
Your suggestion works just as I want! Now, I just need to understand WHY! :) -
Now, I need to mark this thread as solved. I know how to do that!
But I should click something that gives points to reputation. I know I saw that somewhere! (mutter, mutter, mutter) I hate when I don't know what I'm doing!Anyway, Thanks SGaist! Very Much!
-
Now, I need to mark this thread as solved. I know how to do that!
But I should click something that gives points to reputation. I know I saw that somewhere! (mutter, mutter, mutter) I hate when I don't know what I'm doing!Anyway, Thanks SGaist! Very Much!
You can upvote a post with the small up arrow right below each post. The down arrow is for downvoting (and should be used in rare cases). The number in between the both shows the current voting for a post and are therefore a sign for it's quality.
Regards
-
AHA! (Not trying to steal your handle!) Thanks!
-
AHA! (Not trying to steal your handle!) Thanks!
@bart.hollis You're welcome :)