单击行以显示来自客户端的信息,Excell类型的Qt表



我制作了一个Excel类型(QTableWidget(窗口,在其中显示来自客户端的所有名称,每次单击一行,就会在几个QLine Edit对象上显示关于客户端的所有信息(名字、姓氏、地址等(。我使用行号来了解被点击的客户端,并从Sqlite数据库中获取其信息

这里的问题是,当用户";点击";在一个特定的行中,我使用cellClicked()信号来触发一个单独的函数并传递Sql查询,但它不能工作

QObject::connect(widget_PP.Productores_Lista ,SIGNAL(cellClicked(int,int)) ,this ,SLOT(mostrar(int,int,registros)));

我试着在需要的函数中使用一个简单的while,并使用currentRow(),但它也不起作用,它挂起了程序,所以我在这里搞砸了一些大事。这是我的密码。

这是我的代码的主要思想

创建QtableWidget-->填充名称-->单击"客户名称"-->使用行在Db上查找客户名称-->用客户数据填充QlineEdit

void Pantalla_Principal::Produc_Lista(){
QSqlDatabase mData=QSqlDatabase::addDatabase("QSQLITE"); // pones el tipo de driver de la db, en este caso QMYSQL
mData.setDatabaseName(name_db);
mData.open();
QSqlQuery registros;
registros.exec("SELECT * FROM productor");
Guardar( registros.executedQuery(), registros.lastError().text(),"Lista de Productores");
widget_PP.Productores_Lista->clear();
widget_PP.Productores_Lista->setColumnCount(1);
widget_PP.Productores_Lista->setColumnWidth(0,350);
widget_PP.Productores_Lista->setRowCount(0);

while (registros.next()){
widget_PP.Productores_Lista->setRowCount(widget_PP.Productores_Lista->rowCount() + 1);
int filas = widget_PP.Productores_Lista->rowCount() - 1;
QTableWidgetItem *obj0 = new QTableWidgetItem;
obj0->setText(registros.value("Productor_Nombre").toString());
widget_PP.Productores_Lista->setItem(filas,0,obj0);
}

while (widget_PP.Productores_Lista->rowCount() > 0){
int fila = widget_PP.Productores_Lista->currentRow();
registros.exec("SELECT Productor_Nombre FROM productor");
widget_PP.Nombre_Lista_Prod->setText(registros.value(fila).toString());
Guardar(registros.executedQuery(), registros.lastError().text(), "Productor_Nombre");

registros.exec("SELECT Productor_Apellidos FROM productor");
widget_PP.Apellidos_Lista_Prod->setText(registros.value(fila).toString());
Guardar(registros.executedQuery(), registros.lastError().text(), "Productor_Apellidos");

registros.exec("SELECT Predio FROM productor");
widget_PP.Predio_Lista_Prod->setText(registros.value(fila).toString());
Guardar(registros.executedQuery(), registros.lastError().text(), "Predio");
registros.exec("SELECT Ubicacion FROM productor");
widget_PP.Ciudad_Lista_Prod->setText(registros.value(fila).toString());
Guardar(registros.executedQuery(), registros.lastError().text(), "Ubicacion");
registros.exec("SELECT Productor_Num FROM productor");
widget_PP.No_Cliente_Spin->setValue(registros.value(fila).toInt());
Guardar(registros.executedQuery(), registros.lastError().text(), "Productor_Num");

}    

// QObject::connect(widget_PP.Productores_Lista ,SIGNAL(cellClicked(int,int)) ,this ,SLOT(mostrar(int,int,registros)));
// the last connect use an slot that have the second while from the code above
mData.close(); 
}

注意:我一开始确实使用了qt中的slot系统,这就是为什么我在那里对它进行了评论,但我无法从查询中获取数据。这是我使用connect的代码,如果我删除QSqlQuery reg,它就可以工作,我可以检索Row和Column,但我失去了对Db的访问,我不确定我是否必须在内创建第二个数据库连接

void Pantalla_Principal::mostrar(int fila,int columna, QSqlQuery reg){
//int colum = columna; 
fila++;
reg.exec("SELECT Productor_Nombre FROM productor");
widget_PP.Nombre_Lista_Prod->setText(QVariant(fila).toString());
Guardar(reg.executedQuery(), reg.lastError().text(), "Productor_Nombre");
reg.exec("SELECT Productor_Apellidos FROM productor");
widget_PP.Apellidos_Lista_Prod->setText(reg.value(fila).toString());
Guardar(reg.executedQuery(), reg.lastError().text(), "Productor_Apellidos");
reg.exec("SELECT Predio FROM productor");
widget_PP.Predio_Lista_Prod->setText(reg.value(fila).toString());
Guardar(reg.executedQuery(), reg.lastError().text(), "Predio");
reg.exec("SELECT Ubicacion FROM productor");
widget_PP.Ciudad_Lista_Prod->setText(reg.value(fila).toString());
Guardar(reg.executedQuery(), reg.lastError().text(), "Ubicacion");
reg.exec("SELECT Productor_Num FROM productor");
widget_PP.No_Cliente_Spin->setValue(reg.value(fila).toInt());
//widget_PP.No_Cliente_Spin->setValue(fila);
Guardar(reg.executedQuery(), reg.lastError().text(), "Productor_Num");

}

您不必创建到DB的第二个连接。我建议您检查QSqlDatabase中的addDatabase方法。上面写着:

使用驱动程序类型和连接名称connectionName将数据库添加到数据库连接列表中。如果已经存在名为connectionName的数据库连接,则会删除该连接。

数据库连接由connectionName引用。

然后从QSqlQuery::QSqlQuery(QSqlDatabase db(:

使用数据库db构建一个QSqlQuery对象。如果数据库无效,则将使用应用程序的默认数据库。

所以基本上这意味着你需要使用addDatabase创建数据库连接(你已经在Produc_Lista中完成了(,但你应该将QSqlDatabase mData的定义移动到你的头文件中,这样它就可以在其他函数中访问。

然后在你的插槽mostrar中,你应该做这样的事情:

void Pantalla_Principal::mostrar(int fila, int columna){
QSqlQuery reg(mData);
... //the rest of your function
}

如果你只有一个数据库连接,你甚至可以避免将数据库连接传递给QSqlQuery,因为它使用默认的数据库连接,在你的情况下,这是你唯一创建的数据库连接。

我还建议您切换到新的信号槽语法。

最新更新