April 16, 2012

guptaprashant1986@gmail guptaprashan..
Lab Rat
22 posts

[Solved] Closing a dialog with a timer.

  hi i am new to the community. please tell me how to start a new thread. as for your reply, thanks a lot. but you could not understand my problem clearly. let me tell you in detail. i am building a mobile examination system. i have first dialog box that displays the instruction before the start of the examination. i have made 25 questions each question to be answered by clicking a radiobutton & pushbutton by which it is possible to navigate to the next page. as soon as the pushbutton is clicked on the “instruction dialog”, a timer is started. a timer may be expired while the candidate is taking the examination. the candidate may have answered all or few questions only. which means that while the candidate is giving the examination & is reading the question on a particular “question dialog”, the timer may be expired. then i want that when timer is exited, all the other open dialogs get closed automatically. i am providing the code for my file here:
  1. #include "mydialog2.h"
  2. #include "ui_mydialog2.h"
  3. #include "mydialog3.h"
  4. #include<QTimer>
  5. #include "mydialog29.h"
  6. #include"timeout.h"
  7. MyDialog2::MyDialog2(QWidget *parent) :
  8.     QDialog(parent),
  9.     ui(new Ui::MyDialog2)
  10. {
  11.     ui->setupUi(this);
  12. }
  13.  
  14. MyDialog2::~MyDialog2()
  15. {
  16.     delete ui;
  17. }
  18.  
  19. void MyDialog2::on_pushButton_2_clicked()
  20. {
  21.     connect(&timer,SIGNAL(timeout()),this,SLOT(MySlot()));
  22.     this->timer.start(10000);
  23.     MyDialog3 mDialog3;
  24.     mDialog3.setModal(true);
  25.     mDialog3.exec();
  26. }
  27. void MyDialog2::MySlot()
  28. {
  29.     this->timer.stop();
  30.     timeout t;
  31.     t.setModal(true);
  32.     t.exec();
  33. }
coding for mydialog 3 is as:
  1. #include "mydialog3.h"
  2. #include "ui_mydialog3.h"
  3. #include "mydialog4.h"
  4. #include<QAbstractButton>
  5. MyDialog3::MyDialog3(QWidget *parent) :
  6.     QDialog(parent),
  7.     ui(new Ui::MyDialog3)
  8. {
  9.     ui->setupUi(this);
  10. }
  11.  
  12. MyDialog3::~MyDialog3()
  13. {
  14.     db.close();
  15.     delete ui;
  16. }
  17.  
  18. void MyDialog3::on_pushButton_clicked()
  19. {
  20.     QSqlDatabase db= QSqlDatabase::addDatabase("QSQLITE");
  21.     db.setDatabaseName("prject3");
  22.     db.open();
  23.     QSqlQuery q;
  24.     q.exec("create table reslt1(quesno char(5),optionclicked char(15),correctans char(3),marks integer(2));");
  25.  
  26.     if(!(ui->radioButton->isChecked()) && !(ui->radioButton_2->isChecked()) && !(ui->radioButton_3->isChecked())&& !(ui->radioButton_4->isChecked()))
  27.     {
  28.         q.exec("insert into reslt1 values('Q1','Unanswered','A',0);");
  29.     }
  30.     else
  31.     {
  32.         if(ui->radioButton->isChecked())
  33.         {
  34.             q.exec("insert into reslt1 values('Q1','A','A',1);");
  35.  
  36.         }
  37.         if(ui->radioButton_2->isChecked())
  38.         {
  39.             q.exec("insert into reslt1 values('Q1','B','A',0);");
  40.         }
  41.         if(ui->radioButton_3->isChecked())
  42.         {
  43.             q.exec("insert into reslt1 values('Q1','C','A',0);");
  44.         }
  45.         if(ui->radioButton_4->isChecked())
  46.         {
  47.             q.exec("insert into reslt1 values('Q1','D','A',0);");
  48.         }
  49.     }
  50.     MyDialog4 mDialog4;
  51.     mDialog4.setModal(true);
  52.     mDialog4.exec();
  53. }
coding for timeout is as:
  1. #include "timeout.h"
  2. #include "ui_timeout.h"
  3. #include"result.h"
  4.  
  5. timeout::timeout(QWidget *parent) :
  6.     QDialog(parent),
  7.     ui(new Ui::timeout)
  8. {
  9.     ui->setupUi(this);    
  10. }
  11.  
  12. timeout::~timeout()
  13. {
  14.     delete ui;
  15. }
  16.  
  17. void timeout::on_pushButton_clicked()
  18. {
  19.     Result r;
  20.     r.setModal(true);
  21.     r.exec();
  22. }
so, say person was on mydialog 3 when timer expires. a message will be displayed that will indicate that timer has expired in form of timeout & clicking the pushbutton opens the result form.

***“What i want is that when result form is opened on click of pushbutton or when timer is stopped in MySlot(), the opened mydialog3 also gets closed automatically”*

**

[Edit: Added @ tags for code; mlong]

15 replies

April 16, 2012

mlong mlong
Mad Scientist
1517 posts

I split the question into its own thread. Please start new threads for new topics.

In response to your question though, every QDialog has an accept(), reject(), and done() slot, any of which can be connected to the timeout() signal of a QTimer.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

April 16, 2012

guptaprashant1986@gmail guptaprashan..
Lab Rat
22 posts

for solving my problem i wanted that all the “Question dialogs” be ,ade child of either timeout dialog or the mydialog2. so that whenever they exit, it is possible to close all the open dialogs.

but i dont know how to make one dialog child/parent of other. please help me o this.

April 17, 2012

veeraps veeraps
Lab Rat
87 posts

While creating the Widget, if you pass in the parent widget as argument, it would be the parent. Is that what you are asking for?

  1. timeout t;
  2. MyDialog3 dlg3(&t); // dlg3's parent would be t

You can use setParent() [qt-project.org] to set the parent of widget independently, would suggest to go through the manual for Note & Warning.

Is this what you asked for?

April 17, 2012

guptaprashant1986@gmail guptaprashan..
Lab Rat
22 posts

thanks brother. thanks a lot. my problem is solved…………….
please tell me the following things:

1) how to pass value from a lineedit in gui to the backend database for storage
2) how to start a new thread in the community
3) how to use setParent() function

April 18, 2012

veeraps veeraps
Lab Rat
87 posts

1) how to pass value from a lineedit in gui to the backend database for storage

lineedit will have a method text() to return whatever entered in lineedit – use this to send to backend database

2) how to start a new thread in the community

Under Forums -> Qt Development -> General and Desktop -> Start new discussion [qt-project.org]

3) how to use setParent() function

Warning: It is very unlikely that you will ever need this function – If you can set the parent while creation, that will be okay.

April 18, 2012

guptaprashant1986@gmail guptaprashan..
Lab Rat
22 posts

thank a lot for the suggestion. but i have already tried this out. i give mu coding for the form below:

  1. #include "mydialog.h"
  2. #include "ui_mydialog.h"
  3. #include <QtCore>
  4. #include <QtGui>
  5. #include <QtSql>
  6. #include"user_data.h"
  7. #include "mydialog30.h"
  8.  
  9. MyDialog::MyDialog(QWidget *parent) :
  10.     QDialog(parent),
  11.     ui(new Ui::MyDialog)
  12. {
  13.     ui->setupUi(this);
  14. }
  15.  
  16. MyDialog::~MyDialog()
  17. {
  18.     db.close();
  19.     delete ui;
  20. }
  21.  
  22. void MyDialog::on_pushButton_5_clicked()
  23. {
  24.     if((ui->lineEdit_13->isModified())&& (ui->lineEdit_14->isModified())&&(ui->lineEdit_15->isModified())&&(ui->lineEdit_16->isModified())&&(ui->lineEdit_17->isModified())&& (ui->lineEdit_18->isModified()))
  25.     {
  26.         QSqlDatabase db= QSqlDatabase::addDatabase("QSQLITE");
  27.         db.setDatabaseName("prject3");
  28.         db.open();
  29.         QSqlQuery q;
  30.         q.exec("create table tab1(name char(10),branch char(5),dob char(6),contact number(10),email_id char(10),address char(10));");
  31.         QString name= ui->lineEdit_14->text();
  32.         QString branch= ui->lineEdit_13->text();
  33.         QString date= ui->lineEdit_15->text();
  34.         QString cnct =ui->lineEdit_16->text();
  35.         QString email= ui->lineEdit_17->text();
  36.         QString addrss= ui->lineEdit_18->text();
  37.         q.exec("insert into tab1 values('ui->lineEdit_14->text()','ui->lineEdit_13->text()','ui->lineEdit_15->text()','ui->lineEdit_16->text()','ui->lineEdit_17->text()','ui->lineEdit_18->text()');");
  38.         user_data d;
  39.         d.exec();
  40.     }
  41.     else
  42.     {
  43.         MyDialog30 m;
  44.         m.exec();
  45.     }
  46. }

here the problem is that the input is getting accepted perfectly well on using text() function of line edit. but when i am passing it into the database & displaying the output, the output is not being displayed. i am posting here the coding for my form showing the output also here:
  1. #include "user_data.h"
  2. #include "ui_user_data.h"
  3. #include"mydialog28.h"
  4. user_data::user_data(QWidget *parent) :
  5.     QDialog(parent),
  6.     ui(new Ui::user_data)
  7. {
  8.     ui->setupUi(this);
  9. }
  10.  
  11. user_data::~user_data()
  12. {
  13.     db.close();
  14.     delete ui;
  15. }
  16.  
  17. void user_data::on_pushButton_clicked()
  18. {
  19.     MyDialog28 m;
  20.     m.exec();
  21. }
  22.  
  23. void user_data::on_pushButton_2_clicked()
  24. {
  25.  
  26. }
  27.  
  28. void user_data::on_pushButton_3_clicked()
  29. {
  30.     QSqlDatabase db= QSqlDatabase::addDatabase("QSQLITE");
  31.     db.setDatabaseName("prject3");
  32.     db.open();
  33.     this->model= new QSqlQueryModel();
  34.     model->setQuery("select * from tab1");
  35.     ui->tableView->setModel(model);
  36. }
please help me out on how i can store a value of text entered through line edit into my backend database.

[Edit: @ tags; mlong]

April 18, 2012

mlong mlong
Mad Scientist
1517 posts

When posting code, please wrap it in @ tags to preserve its formatting.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

April 18, 2012

guptaprashant1986@gmail guptaprashan..
Lab Rat
22 posts

sure sir. i will. please tell the solution to my problem.

April 18, 2012

mlong mlong
Mad Scientist
1517 posts

please tell the solution to my problem

I don’t have an answer, myself, but I promise that making demands such as that is considered rude and will not help you get an answer faster.

Please see http://www.catb.org/~esr/faqs/smart-questions.html for advice on how to get the best results. Thanks.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

April 18, 2012

guptaprashant1986@gmail guptaprashan..
Lab Rat
22 posts

sorry sir for my bit of impatient behavior. i had absolutely no intention like this. i have actually tried out the suggestions posted. they actually did not work.

April 19, 2012

veeraps veeraps
Lab Rat
87 posts

Try the below

  1.         QString name= ui->lineEdit_14->text();
  2.         QString branch= ui->lineEdit_13->text();
  3.         QString date= ui->lineEdit_15->text();
  4.         QString cnct =ui->lineEdit_16->text();
  5.         QString email= ui->lineEdit_17->text();
  6.         QString addrss= ui->lineEdit_18->text();
  7.         QString query = QString("INSERT INTO 'tab1' VALUES('%1','%2','%3','%4','%5','%6')").arg(name).arg(branch).arg(date).arg(cnct).arg(email).arg(addrss);
  8.         q.exec( query );

I would strongly suggest you to go through the documentation for QString [qt-project.org]. Infact you can click on the source code [highlighted with GREEN color] to go to the documentation page.

April 19, 2012

guptaprashant1986@gmail guptaprashan..
Lab Rat
22 posts

thank you sir. the code has worked. i was actually having a problem that i was trying to connect to the database using same connection that i have used ahead in my project. i had to change the connection also. now the code works fine.

April 19, 2012

veeraps veeraps
Lab Rat
87 posts

Appreciate if you could edit the Topic and add [SOLVED] in the front to say that this thread is completed.

April 19, 2012

guptaprashant1986@gmail guptaprashan..
Lab Rat
22 posts

sure. i do it now

April 20, 2012

gdouglas7 gdouglas7
Lab Rat
19 posts

I always tried so very helpful

 Signature 

Qt Developer
Student of Information System

 
  ‹‹ QtSslSocket      How can I change my output ››

You must log in to post a reply. Not a member yet? Register here!