简体   繁体   English

将对象传递给 QT 中的函数会导致分段错误

[英]Passing object to a function in QT causes Segmentation Fault

Thanks in advance for your time.在此先感谢您的时间。 I'm still new to the coding world, so excuse me if I ask something silly or obvious.我还是编码世界的新手,所以如果我问一些愚蠢或明显的问题,请原谅。

I'm coding a small program with QT for manipulating data of a data base.我正在用 QT 编写一个小程序来处理数据库的数据。 To represent the data I'm using QTableViews.为了表示我使用 QTableViews 的数据。 I have a few of them with the same configuration, so I made a function to configure them:我有几个配置相同,所以我做了一个函数来配置它们:

QT 5.12
void MainWindow::configureTableView(QSqlTableModel *model, QTableView *table, QString DBTable)
{
    //Pacients table.
    model = new QSqlTableModel(this);
    model->setTable(DBTable);
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->select();
    table->setModel(model);
    table->setSortingEnabled(true);
    table->setCornerButtonEnabled(true);
    table->hideColumn(0);
}

I have some QSqlTableModel defined in the mainwindow.h as:我在 mainwindow.h 中定义了一些 QSqlTableModel:

 QSqlTableModel *PatientsTable;
...

I call the function with:我用以下方法调用该函数:

configureTableView(PatientsTable, ui->ClientsTabTableView, "Pacientes");

Program starts and shows OK, but as soon as I try to do anything with the view like set a filter程序启动并显示正常,但是一旦我尝试对视图执行任何操作,例如设置过滤器

PatientsTable->setFilter(Search)

where Search is a QString configured by other function based on user input, the program crashes and QT tells me it received a signal from the operative system: SIGSEGV (segmentation fault).其中 Search 是由其他函数根据用户输入配置的 QString,程序崩溃并且 QT 告诉我它收到了来自操作系统的信号:SIGSEGV(分段错误)。

Now when is was coding all of this, at some point I had:现在什么时候编码所有这些,在某些时候我有:

void MainWindow::configureTableView(QTableView *table, QString DBTable)

    PatientsTable = new QSqlTableModel(this);
    PatientsTable->setTable(DBTable);
    PatientsTable->setEditStrategy(QSqlTableModel::OnManualSubmit);
    PatientsTable->select();
    table->setModel(PatientsTable);
    table->setSortingEnabled(true);
    table->setCornerButtonEnabled(true);
    table->hideColumn(0);

which works with no issues at all.这完全没有问题。

What am I missing?我错过了什么? I've been digging for a while now and the code and explanations I found on Internet aren't working.我已经挖掘了一段时间,我在互联网上找到的代码和解释不起作用。 Thanks again for your time!感谢你的宝贵时间!

The problem is that your function does not modify the PatientsTable variable.问题是您的函数不会修改PatientsTable变量。 Just passing a pointer to a function does not let you modify the pointer itself (only what it's pointing to).仅将指针传递给函数不会让您修改指针本身(仅它指向的内容)。 Simple solution, pass the pointer by reference.简单的解决方案,通过引用传递指针。

void MainWindow::configureTableView(QSqlTableModel *&model, QTableView *table, QString DBTable)

The alternative (better in my view) would be to return the pointer from the function另一种选择(在我看来更好)是从函数返回指针

QSqlTableModel *MainWindow::configureTableView(QTableView *table, QString DBTable)
{
    QSqlTableModel *model = new QSqlTableModel(this);
    ...
    return model;
}


PatientsTable = configureTableView(ui->ClientsTabTableView, "Pacientes");

It's a very common beginner misunderstanding.这是一个非常常见的初学者误解。 Pass pointers to modify what is being pointed to.传递指针以修改所指向的内容。 The pointer itself cannot be modified.指针本身不能修改。 In this regard pointers are just like any other kind of variable.在这方面,指针就像任何其他类型的变量一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM