【问题标题】:Click on a Row to show info from client, Excell type Qt table单击一行以显示来自客户端的信息,Excell 类型的 Qt 表
【发布时间】: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客户数据

标签: c++ sqlite qt


【解决方案1】:

您不必创建与数据库的第二个连接。我建议您查看the addDatabase method from QSqlDatabase。它说:

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

数据库连接由connectionName引用。

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

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

所以基本上这意味着您需要使用 addDatabase 创建一个数据库连接(并且您已经在 Produc_Lista 中完成了此操作)但是您应该将 QSqlDatabase mData 的定义移动到您的头文件中,以便它可以在其他函数。

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

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

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

我还建议您切换到the new signal slot syntax

【讨论】:

  • 实际上你的方法比我的好,我删除了程序内对数据库的所有调用,只留下一个连接,然后检索我需要的列/行并显示在盒子上,我的问题是试图在我的函数上调用查询,你帮了我很多,现在它就像一个魅力,谢谢
猜你喜欢
  • 1970-01-01
  • 2021-03-01
  • 1970-01-01
  • 2018-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多