简体   繁体   English

QT C++ 访问另一个类的类成员

[英]QT C++ access to a Class Member of another Class

i really dont understand the following behavior:我真的不明白以下行为:

3 Code snippets to explain 3 代码片段解释

sqlconnection.h sqlconnection.h

class SqlConnection
{
public:
SqlConnection();
QSqlDatabase db;
} ;

sqlconnection.cpp sqlconnection.cpp

SqlConnection::SqlConnection()
{ if (!db.open()){ //no different with or without that
  db = QSqlDatabase::addDatabase("QODBC");
  ...
}

artikelverwaltung.cpp artikelverwaltung.cpp

Artikelverwaltung::Artikelverwaltung(){

 SqlConnection().db.open();
 ...(code to do works fine)
}
void Artikelverwaltung::on_pushButton_2_clicked()
{

SqlConnection().db.close(); // "HERE"
}

"HERE" it cycles the full Contructor of the Class again, but why it don't just close the connection? “这里”它再次循环整个类的构造函数,但为什么它不只是关闭连接? (this is the Main question about and how to fix that.) (这是关于以及如何解决这个问题的主要问题。)

I can't even access "db" from another Method inside the Class but outside of the Constructor, like:我什至无法从类内部但构造函数外部的另一个方法访问“db”,例如:

sqlconnection.cpp: sqlconnection.cpp:

void closeDB() {db.close();} is not possible.` void closeDB() {db.close();}是不可能的。`

What i am doing wrong?我做错了什么?

Thank you guys感谢你们

Hello and welcome to Stackoverflow!您好,欢迎来到 Stackoverflow!

If you call SqlConnection().db.close();如果调用SqlConnection().db.close(); , what you are actually doing is creating a new instance of your SqlConnection class and then calling close() on its variable db. ,您实际上在做的是创建 SqlConnection 类的新实例,然后在其变量 db 上调用 close()。 This is not the same one you opened earlier.这不是您之前打开的那个。

What you want to do is inside of your Artikelverwaltung class:你想要做的是在你的 Artikelverwaltung 课程中:

SqlConnection sqlConnection = new SqlConnection();

and then later然后稍后

sqlConnection.db.open();

and afterwards之后

sqlConnection.db.close();

The reason why you cant add that closeDB() method is that you need to either put the method inside of the SqlConnection class or you need to operate on an instance of the class.不能添加closeDB()方法的原因是您需要将该方法放在SqlConnection类中,或者您需要对该类的实例进行操作。

You cannot operate on a class like this without instanciating it (creating an instance of it).如果不实例化它(创建它的实例),就不能对这样的类进行操作。

You should look at class instantiation to understand how the concept works.您应该查看类实例化以了解该概念的工作原理。

If you call SqlConnection() you are creating a new instance of the class (you can look at it like a copy of the class).如果您调用SqlConnection()您正在创建该类的一个新实例(您可以将其视为该类的副本)。
If you call SqlConnection() somewhere else you are simply creating a new instance.如果您在其他地方调用SqlConnection() ,您只是在创建一个新实例。
These instances do NOT share the variables or the contents in them.这些实例不共享变量或其中的内容。 They are completely independent.他们是完全独立的。

You cannot access a member of a class (like your db variable) without creating an instance of the class first and then accessing the member of that instance.如果不首先创建类的实例然后访问该实例的成员,则无法访问类的成员(如您的 db 变量)。

In general, you do not need to use any wrapper class to hold database connections in Qt.通常,您不需要使用任何包装类来保存 Qt 中的数据库连接。 You should create a database connection once using QSqlDatabase::addDatabase and open it.您应该使用QSqlDatabase::addDatabase创建一次数据库连接并打开它。 After that you can get a database connection anywhere by calling QSqlDatabase::database static method.之后,您可以通过调用QSqlDatabase::database静态方法在任何地方获得数据库连接。 So to close the database connection you should simply call QSqlDatabase::database().close() .因此,要关闭数据库连接,您只需调用QSqlDatabase::database().close()即可。

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

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