PDA

View Full Version : update form



hamidarr
24th June 2011, 14:56
hi everyone how to update form without member show? Because i ever call member show it show another form

wysota
24th June 2011, 15:18
My guess is you are creating a new object instead of reusing the original one.

hamidarr
24th June 2011, 15:33
i have form1 and form2. when i click button of form1 form2 shown and then i click ok on form2 i create an object of form1 and calling member show.

wysota
24th June 2011, 16:03
So why are you suprised another form pops up when you show it?

hamidarr
24th June 2011, 16:31
thanks for reply . i said before i want to to update form without method show. in form 1 i have tablewidget and label when i show form2 the user enter id when the user click on okbutton in form2 i want in form1 show the information of user but i can'nt see the information until i calling member show.can you give me a way for update form1.

Santosh Reddy
24th June 2011, 17:54
i have form1 and form2. when i click button of form1 form2 shown and then i click ok on form2 i create an object of form1 and calling member show.
You should not be creating object of form1 when you click on form 2.

Try to reliaze this sequence,

1. Create Form1 object, let say From1Obj1
2. show From1Obj1
3. user clicks on From1Obj
4. create Form2 object, let say From2Obj1
5. user clicks on From2Obj1
6. show From1Obj1 (simple repeat sept 2 only. Don't do setp 1, if you do step 1, you will end up having From1Obj2 and which is different from From1Obj1)
7. Now how does form2 send information (user enter id) to form1 ?

wysota
24th June 2011, 18:40
form2 should be a dialog executed using QDialog::exec() from within form1. After the dialog is closed, you can query its results and update form1.

hamidarr
25th June 2011, 14:41
sorry for late
here is my code:
student.h



#ifndef STUDENT_H
#define STUDENT_H
#include <QWidget>
#include "ui_student.h"
class Student : public QWidget , public Ui::Student
{
Q_OBJECT

public:
Student(QWidget *parent = 0);
~Student();
bool opendb();
void view(int sh_d);
};

#endif

studend.cpp


#include "student.h"
#include "ui_student.h"
#include <QSqlRecord>
#include <QSqlError>
extern int id;


Student::Student(QWidget *parent) :
QWidget(parent)

{

setupUi(this);
createaction();
}

Student::~Student()
{

}


void Student::createaction()
{
connect(Sbutton,SIGNAL(clicked()),this,SLOT(search student())) ;


}

void Student::searchstudent()
{

StudentSearch *s = new StudentSearch;
s->show();

}

void Student::viewinfo(int sh_d)
{




{


if(opendb())
{


QSqlQuery query(QSqlDatabase::database("con"));
QSqlRecord rec;
query.setForwardOnly(true);
query.exec(QString("{CALL information(%1)}").arg(sh_d));
if(!query.isActive())
{

QMessageBox::warning(this, tr("Error"),
query.lastError().text()+"Please Contact to Programer");


}
else
{

while(query.next())
{

rec = query.record();

N_label->setText(rec.value(0).toString());
F_label->setText(rec.value(1).toString());
C_label->setText(rec.value(2).toString());
M_label->setText(rec.value(3).toString());
D_label->setText(rec.value(4).toString());
SH_label->setText(rec.value(5).toString());


}
else
{
QMessageBox::critical(0, QObject::tr("Database Error"),
"can'nt open database");
}



}



}





}



}

bool Student::opendb()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC","con");
db.setHostName("pishtazanedu.ir");

db.setDatabaseName("pop;DBQ=University.mdf");
db.setUserName("hamidarrb");
db.setPassword("ham!dArr_12973864");

if(!db.open())

return false;



return true;
}




StudentSearch.h


#ifndef STUDENTSEARCH_H
#define STUDENTSEARCH_H

#include <QWidget>
#include <QtGui>
#include "student.h"

#include "ui_studentsearch.h"


class StudentSearch : public QWidget , public Ui::StudentSearch
{
Q_OBJECT

public:
StudentSearch(QWidget *parent = 0);
~StudentSearch();
void createaction();
private slots:
void search();


};

#endif



studentsearch.cpp

#include "studentsearch.h"
#include "student.h"

extern int id;
StudentSearch::StudentSearch(QWidget *parent)
:QWidget(parent)

{

setupUi(this);
createaction();


}
StudentSearch::~StudentSearch()
{

}

void StudentSearch::createaction()
{
connect(okbutton,SIGNAL(clicked()),this,SLOT(searc h()));
connect(cancelbutton,SIGNAL(clicked()),this,SLOT(c lose());
}

void StudentSearch::searchstudent()
{
id = addlineEdit->text().toInt();
Student *s = new Student;//my problem is here
s->viewinfo(id) //i call member viewinfo but student form do'nt show anything
// if i here i call s->show; it show anothe form and i can see
//the information of student
StudentSearch::close();


}

Rachol
25th June 2011, 15:27
Shouldn't you hide your passwords at least?

Okay, this is not a real solution, neither a good one, but it should work and is definitely fastest way to make your code work.


void Student::searchstudent()
{

StudentSearch *s = new StudentSearch();
if(id = s->exec())
viewinfo(id);
}

In your student search class:


void StudentSearch::searchstudent()
{
id = addlineEdit->text().toInt();
setResult(id);
StudentSearch::close();
}

hamidarr
25th June 2011, 16:15
Thanks.it's work.

ChrisW67
25th June 2011, 21:22
void Student::searchstudent()
{

StudentSearch *s = new StudentSearch;
s->show();

}


OK. Here you open a new StudentSearch widget in the same fashion you would open a modeless dialog. You do this if you want the search window to stay open and allow user interaction with the student record window.




void StudentSearch::searchstudent()
{
id = addlineEdit->text().toInt();
Student *s = new Student;//my problem is here
s->viewinfo(id) //i call member viewinfo but student form do'nt show anything
// if i here i call s->show; it show anothe form and i can see
//the information of student
StudentSearch::close();
}


At line 4 you create a new instance of the Student window and prepare it to show a particular student [5]. This new instance does not have any visible effect until/unless you call show() when a new window is shown. This is not surprising but also clearly not what you wanted to happen.

You have two options:

Open the StudentSearch widget as a modal dialog. When the user clicks OK you use a public method of the StudentSearch class to access the selected value, and then display the student. This all happens in Student::searchstudent(). Wysota suggested this earlier.
Open the StudentSearch as a modeless dialog. When the user selects a student you emit a signal (carrying the student number) that is connected to a slot you need to write in the Student class. This slot should display the student record passed in.

Which you choose is up to you. StudentSearch should probably be a QDialog derivative in either case.