qt中怎么实现两个类之间的数据传输,
我想在addinfo的对话框中,添加用户信息。当添加成功后,在main对话框中实时的显示添加的数据。我用的是QtableWidget,
我的想法是想在addinfo.cpp中调用main.cpp的showinfo函数。求教各位大虾们!
代码如下:
addinfo.cpp中:
connect(ui.okpushBtn,SIGNAL(clicked()),this,SLOT(putin()));
QSqlQuery *query = new QSqlQuery(db);
QString str=tr("insert into classmate values ('%1','%2','%3','%4','%5')")
.arg(username).arg(sex).arg(telephone).arg(favority).arg(dream->toPlainText());
if (query->exec(str))
{
QMessageBox::information(this,tr("提示"),tr("已成功插入"));
//showinfo();
accept();
}
main.cpp中:
void maindialog::showinfo()
{
ui.tableWidget->clear(); //tableWidget为控件objectname;
QSqlQuery *query = new QSqlQuery(db);
QString sql= tr("select * from classmate");
QString susername,ssex,stelephone,sfavority,sdream;
QTableWidgetItem *tmpItem;
query->exec(sql);
int i=0;
//ui.tableWidget->setVerticalHeaderItem()
QStringList header;
header<<tr("姓名")<<tr("性别")<<tr("手机号码")<<tr("爱好")<<tr("梦想");
ui.tableWidget->setHorizontalHeaderLabels(header);
while(query->next())
{
susername= query->value(0).toString();
ssex = query->value(1).toString();
stelephone = query->value(2).toString();
sfavority = query->value(3).toString();
sdream = query->value(4).toString();
tmpItem = new QTableWidgetItem(susername);
ui.tableWidget->setItem(i, 0, tmpItem);
tmpItem = new QTableWidgetItem(ssex);
ui.tableWidget->setItem(i, 1, tmpItem);
tmpItem = new QTableWidgetItem(stelephone);
ui.tableWidget->setItem(i, 2, tmpItem);
tmpItem = new QTableWidgetItem(sfavority);
ui.tableWidget->setItem(i, 3, tmpItem);
tmpItem = new QTableWidgetItem(sdream);
ui.tableWidget->setItem(i, 4, tmpItem);
i++;
}
}
[解决办法]
在maindialog中通过connect函数建立信号 - 槽连接,addinfo新建一个信号,自己取名,void maindialog::showinfo()为槽。QMessageBox::information(this,tr("提示"),tr("已成功插入"));之后emit一下你自己的信号就行了。