我大部分都知道如何在 C++ 中格式化函数,除了我以前从未见过的这种情况。
我目前有这个:
void MainWindow::on_viewEmployees_clicked()
{
QDebug debugTxt(QtDebugMsg);
DatabaseControl myDBControl; //this is the header file that I want to use
QSqlQuery qry;
bool ok;
int rows = myDBControl.getNumEmployees();
ui->table->setRowCount(rows);
int count = 0;
int currentCol = 0;
while(count < rows){
qry.prepare("SELECT * FROM employees ORDER BY lastname LIMIT :f1, :f2");
qry.bindValue(":f1", count);
qry.bindValue(":f2", count+1);
qry.exec();
qry.next();
currentCol = 0;
while(currentCol < ui->table->columnCount()){
QTableWidgetItem *setdes = new QTableWidgetItem;
setdes->setText(qry.value(currentCol).toString());
ui->table->setItem(count, currentCol-1, setdes);
currentCol++;
}
count++;
}
debugTxt << "Table successfully populated";
ui->lblResult->setText("[+] Table successfully populated");
}
当然,它可以编译一切。它做的正是它应该做的——扫描 SQLite 数据库的内容并通过 QTableWidget 输出它,按员工姓氏排序,只要你点击“viewEmployees”按钮。没有问题。
但是我想把它转换成一个函数......比如说,DatabaseControl.refreshTableView()。
我将其复制/粘贴到 DatabaseControl 类中,并且我知道将所有 DatabaseControl 引用更改为“this”,但是我不知道如何处理“ui->”位,因为 ui-> 在我的 mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
结果,我无法更改 MainWindow 中的任何 UI 元素(据我所知)。我想要做的是能够通过 DatabaseControl 类编辑这些 UI 元素。更具体地说,我想通过 DatabaseControl 编辑该 QTableWidget 的内容。