Hi,
I'm assuming you are sub-classing a QDialog its has some widgets (for example lineEdits) and the insertion happens once the user close the dialog.
Lets say for example that mydialog has two lines edits: Code (lineEdit) and Name (lineEdit_2)
declare two private members in mydialog:
in the .h
...
private:
...
private:
QString m_code;
QString m_name
To copy to clipboard, switch view to plain text mode
declare two public functions to get the name and code:
in the .h
public:
public:
QString getCode(){return m_code;}
QString getName(){return m_name;}
To copy to clipboard, switch view to plain text mode
I guessing you have a "OK" or "Close" button, so move the data from the edits to the private members when closing
in the .cpp
m_code = ui->lineEdit->text();
m_name = ui->lineEdit_2->text();
m_code = ui->lineEdit->text();
m_name = ui->lineEdit_2->text();
To copy to clipboard, switch view to plain text mode
Now another window is the one calling the dialog right?
//Creates the dialog, execute it and wait until close
mydialog mydlg;
mydlg.exec();
//Insert the data. I'm old fashion so I prefer to write SQL code and the execute it....but QT has many classes to handle inserts in a more automatic way.
QSqlQuery qry
(mydb
);
//mydb has to be created previously. Is a QSQLDatabase... See help if you don't know how to open and database
sql = "INSERT INTO mytable (code,name) VALUES ('" + mydlg.getCode() + "','" + mydlg.getName() + "')"; //Creates the sql insert
qry.exec(sql); //Executes the insert
//Creates the dialog, execute it and wait until close
mydialog mydlg;
mydlg.exec();
//Insert the data. I'm old fashion so I prefer to write SQL code and the execute it....but QT has many classes to handle inserts in a more automatic way.
QString sql;
QSqlQuery qry(mydb); //mydb has to be created previously. Is a QSQLDatabase... See help if you don't know how to open and database
sql = "INSERT INTO mytable (code,name) VALUES ('" + mydlg.getCode() + "','" + mydlg.getName() + "')"; //Creates the sql insert
qry.exec(sql); //Executes the insert
To copy to clipboard, switch view to plain text mode
And that's it.
Good luck.
Bookmarks