【发布时间】:2020-11-18 12:35:08
【问题描述】:
我创建了一个 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 --> 填充名称 --> 点击客户名称 --> 使用 Row 在 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 中的插槽系统,这就是为什么我在那里对其进行了评论,但我无法从查询中获取数据。这是我使用连接的代码,如果我删除 QSqlQuery reg 它可以工作并且我可以检索行和列,但是我无法访问 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");
}
【问题讨论】:
-
不太清楚你在这里做什么,反正我认为你以错误的方式连接 QTableWidget 信号。我试过这个
connect( ui->tableWidget, SIGNAL(cellClicked(int, int)), this, SLOT(doSomething(int, int))); void MainWindow::doSomething(int row, int column) { qDebug() << "Row " << row << " column " << column; },我可以点击行和列 -
@Salvo 请检查编辑,想法是:创建 QtableWidget --> Populated Names --> 点击 Customer Name --> 使用 Row 在 Db 上查找 Customer Name --> 填充 QlineEdit客户数据